【问题标题】:Java how to check type of annotation when loading classJava如何在加载类时检查注释类型
【发布时间】:2014-11-26 19:26:33
【问题描述】:
  • 我想加载类(使用运行时注解),并检查注解的类型。
  • 注释与字段相关。
  • 该类不在类路径中,也不在 eclipse 项目中

    • 对于这个问题,我需要用它的注解类来编译这个类。
  • 我使用这个链接加载了这个类:

http://www.java2s.com/Tutorial/Java/0125__Reflection/URLclassloader.htm

加载类后,我尝试获取每个字段的注释并检查注释类型:

Field[] fields = obj.getClass().getFields();
        for (Field field : fields) {
            Annotation[] ann = field.getAnnotations();
            for (Annotation annotation : ann) {
                if (annotation.equals(Example.class)) {
                    System.out.println("Example annotation");
                }
            }

        }

我希望看到打印的“示例注释”,但我没有得到它。

test.class和Example.class有如下java源码:

public class Test
{
    @Example (size = 5)
    public int x;

    @Example (size = 3)
    public int y;
}

public @interface Example
{

    int size();

}

保存在不同分区的 2 个类,我用 javac 编译它们(并得到 2 个 .class 文件)

我读到: 1.Java - loading annotated classes 但没有得到解决方案。

那么我做错了什么? (为什么我能看到“示例注释”)? 谢谢

【问题讨论】:

    标签: java reflection annotations


    【解决方案1】:

    您将Annotation 类型与Class 类型进行比较。

    试试

    Field[] fields = obj.getClass().getFields();
        for (Field field : fields) {
            Annotation[] ann = field.getAnnotations();
            for (Annotation annotation : ann) {
                if (annotation.class.equals(Example.class)) {
                    System.out.println("Example annotation");
                }
            }
    
        }
    

    或者,您可以使用instanceOf 运算符:

    if (annotation instanceOf package.Example) {
         System.out.println("Example annotation");
    }
    

    注意:未在本地测试,仅来自阅读。

    【讨论】:

      【解决方案2】:

      使用

       if (annotation.annotationType().equals(Example.class)) {
              System.out.println("Example annotation");
       }
      

      更新:

      以下测试通过:

      package annotation.test;
      
      import org.junit.Test;
      
      import java.lang.annotation.Annotation;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.reflect.Field;
      import java.util.ArrayList;
      import java.util.List;
      
      import junit.framework.Assert;
      
      public class AnnotationTest {
      
          @Test
          public void TestAnnotationType() {
              Object obj = new ClassWithAnnotation();
      
              List<String> fieldList = new ArrayList<String>();
      
              // need to use declared fields as some are private
              Field[] fields = obj.getClass().getDeclaredFields(); 
              for (Field field : fields) {
                  field.setAccessible(true);
                  System.out.println("processing field " + field);
                  Annotation[] ann = field.getAnnotations();
                  for (Annotation annotation : ann) {
                      if (annotation.annotationType().equals(Example1.class)) {
                          System.out.println(field);
                          fieldList.add(field.getName());
                      }
                  }
              }
      
              Assert.assertTrue(fieldList.contains("string1"));
              Assert.assertTrue(fieldList.contains("string2"));
              Assert.assertFalse(fieldList.contains("string3"));
              Assert.assertFalse(fieldList.contains("string4"));
              Assert.assertFalse(fieldList.contains("string5"));
          }
      }
      
      class ClassWithAnnotation {
      
          @Example1
          private String string1;
      
          @Example1
          public String string2;
      
          @Example2
          private String string3;
      
          private String string4;
          private String string5;
      
      }
      
      @Retention(RetentionPolicy.RUNTIME)
      @interface Example1 {
      
      }
      
      @Retention(RetentionPolicy.RUNTIME)
      @interface Example2 {
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-24
        • 1970-01-01
        • 2012-08-14
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        相关资源
        最近更新 更多