繁体   English   中英

Java如何在加载类时检查注释的类型

[英]Java how to check type of annotation when loading class

  • 我想加载类(使用运行时注释),并检查注释的类型。
  • 注释与字段有关。
  • 该类不在类路径中,也不在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-加载带注释的类,但是没有得到解决方案。

那我在做什么错呢? (为什么我看不到“示例注释”)? 谢谢

您正在将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");
}

注意:未经本地测试,仅通过阅读即可。

采用

 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 {

}

暂无
暂无

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

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