我如何只用反射扫描一堂课

时间:2018-12-12 19:09:21

标签: java class

我有两个类FooFooBar,它们都有一些带注释的字段。我只想扫描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对象以便仅扫描一个类?

1 个答案:

答案 0 :(得分:0)

过滤结果有效

Set<Field> fields = new Reflections("my.package.Foo", new FieldAnnotationsScanner())
    .getFieldsAnnotatedWith(MyAnnatation.class)
    .stream().filter(field -> field.getDeclaringClass().equals(Foo.class))
    .collect(Collectors.toSet());
相关问题