使用Google Reflections为注释创建Java扫描类
我正在尝试使用google reflections扫描类中的字段以获取自定义注释。我不知道为什么,但结果总是空的 测试类使用Google Reflections为注释创建Java扫描类,java,annotations,reflections,Java,Annotations,Reflections,我正在尝试使用google reflections扫描类中的字段以获取自定义注释。我不知道为什么,但结果总是空的 测试类 public class AnnotationParser { public void parse() { String packagename = "com.security.rest.client"; final Reflections reflections = new Reflections(packagename);
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();
}
}
public class AuthenticationClient {
@Wire
public KerberosAPI kerberosAPI;
@Wire
public KerberosSessionManager kerberosSessionManager;
}
自定义注释
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Wire {
}
如果需要任何其他信息,请告诉我。默认情况下不扫描字段批注。 您应该将FieldAnnotationScanner添加到扫描仪列表中,例如:
new Reflections("com.security.rest.client", new FieldAnnotationsScanner())