0
  • I want to load class (which use run time annotation), and check the type of the annotation.
  • The annotations are related to the fields.
  • The class is not in the classpath and not in eclipse project

    • For this issue I needed to compile the class with it's annotation classes.
  • I loaded the class using this link:

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

After loading the class, I'm trying to get for each field it's annotations and check the annotation type:

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");
                }
            }

        }

I expect to see the print "Example annotation" but I didnt get it.

The test.class and Example.class have the following java source code:

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

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

public @interface Example
{

    int size();

}

The 2 classes where saved on different partition, and I compiled them with javac (and got 2 .class files)

I read: 1. Java - loading annotated classes But didnt get the solution.

So what Am I doing wrong ? (why cany I see "Example annotation") ? Thanks

Community
  • 1
  • 1
user3668129
  • 4,318
  • 6
  • 45
  • 87

2 Answers2

0

You are comparing an Annotation type to a Class type.

Try

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");
            }
        }

    }

Alternatively, you could use the instanceOf operator:

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

Note: Not tested locally, simply from reading.

Bryan Davis
  • 134
  • 6
0

Use

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

Update:

The following test passes:

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 {

}
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101