Rau*_*wal 2 java annotations reflections
我正在尝试使用谷歌反射扫描类中的字段以获取我的自定义注释.我不知道为什么,但结果总是空集.
测试类
public class AnnotationParser {
public void parse() {
String packagename = "com.security.rest.client";
final Reflections reflections = new Reflections(packagename);
Set<Field> subTypes = reflections.getFieldsAnnotatedWith(Wire.class);
for (Field field : subTypes) {
System.out.println("Class : " + field.getName());
}
}
public static void main(String[] args) {
new AnnotationParser().parse();
}
}
Run Code Online (Sandbox Code Playgroud)
带注释的类
public class AuthenticationClient {
@Wire
public KerberosAPI kerberosAPI;
@Wire
public KerberosSessionManager kerberosSessionManager;
}
Run Code Online (Sandbox Code Playgroud)
自定义注释
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Wire {
}
Run Code Online (Sandbox Code Playgroud)
如果需要任何其他信息,请告诉我.
默认情况下不扫描字段注释.您应该将FieldAnnotationScanner添加到扫描仪列表中,例如:
new Reflections("com.security.rest.client", new FieldAnnotationsScanner())
Run Code Online (Sandbox Code Playgroud)