我有两个类Foo
和FooBar
,它们都有一些带注释的字段。我只想扫描Foo
而不是FooBar
中的注释字段。我当前正尝试从中使用org.reflections.Reflections
https://github.com/ronmamo/reflections我有以下内容:
Set<Field> fields = new Reflections("my.package.Foo", new FieldAnnotationsScanner())
.getFieldsAnnotatedWith(MyAnnatation.class);
但是,这也会拾取FooBar
中的字段,因为它以相同的前缀开头。如何构造Reflections
对象以便只扫描一个类?
过滤结果有效:
Set<Field> fields = new Reflections("my.package.Foo", new FieldAnnotationsScanner())
.getFieldsAnnotatedWith(MyAnnatation.class)
.stream().filter(field -> field.getDeclaringClass().equals(Foo.class))
.collect(Collectors.toSet());