2013-07-22 64 views
1

我正在尝试使用Google反射为我的自定义注释扫描类内的字段。我不知道为什么,但结果总是空的。使用Google Reflections进行注释的Java扫描类

测试类

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 { 

} 

请让我知道是否需要其他信息。

回答

4

默认情况下不扫描字段注释。 您应该将FieldAnnotationScanner添加到扫描仪列表中,如下所示:

new Reflections("com.security.rest.client", new FieldAnnotationsScanner())