如何使用反射只扫描一个类



我有两个类FooFooBar,它们都有一些带注释的字段。我只想扫描Foo而不是FooBar中的注释字段。我当前正尝试从中使用org.reflections.Reflectionshttps://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());

相关内容