I am trying to dynamically create and load a class in my application. The class has annotations on one of the member fields ("id"). Now I can compile and load the class easily. I can see the compiled class in the directory, and when I open it in Eclipse, I can see the member field has the annotation. But when I try to read this annotation, using field.getAnnotations(), the following things happen:
1) When I run the application as a stand alone, I can get the annotation "Key".
2) When I run the application on tomcat server, the getAnnotations() method returns empty list. There is no "Key" annotation.
Just to note that I have RetentionPolicy.RUNTIME in my Key Class. How can I get the annotation when I run it on server ? What could be the possible things to look for, to resolve the issue ?
PS : Some code
@Override
public Class<?> createIndexClass(IndexDefinationData createIndex) {
IndexDtoCreator creator = new IndexDtoCreator();
IndexGenerationResult result = creator.process(createIndex); // creates the source file
Class<?> clazz = (activateIndexClass(result)); //compiles and loads the class
try {
java.lang.reflect.Field x = clazz.getDeclaredField("id");
for (Annotation y : x.getAnnotations()) {
System.out.println(y.annotationType() + " " + y.toString());
}
System.out.println(x.isAnnotationPresent(Key.class));
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return clazz;
}
//class loader
private Class<?> loadClass(String folderName, String fqcn) throws MalformedURLException, ClassNotFoundException {
final URL url = Paths.get(folderName).toUri().toURL();
final ClassLoader loader = new URLClassLoader(new URL[] { url });
return loader.loadClass(fqcn);
}