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