- 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