简体   繁体   English

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

[英]How do I get only scan just one class with Reflections

I have two classes Foo and FooBar both of which has some annotated fields. 我有两个FooFooBar类,它们都有一些带注释的字段。 I would like to scan just Foo and not FooBar for the annotated fields. 我只想扫描Foo而不扫描FooBar来查看带注释的字段。 I am currently trying to use org.reflections.Reflections from https://github.com/ronmamo/reflections I have the following: 我目前正在尝试使用来自https://github.com/ronmamo/reflections的 org.reflections.Reflections我有以下内容:

Set<Field> fields = new Reflections("my.package.Foo", new FieldAnnotationsScanner())
    .getFieldsAnnotatedWith(MyAnnatation.class);

However, this will also pick up fields in FooBar as it starts with the same prefix. 但是,这也会在FooBar拾取以相同前缀开头的字段。 How can I construct the Reflections object so as to just scan the one class? 如何构造Reflections对象以便仅扫描一个类?

Filtering the results works: 过滤结果有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM