0

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);
    }
codeKNIGHT
  • 63
  • 1
  • 8
  • the way you are running your application inside the container can interfere. Like, using EJB's or something. Despite tomcat don't work with EJB, is just an example. Show some code that you have done, to we can see what you are doing. – G Bisconcini Mar 17 '15 at 04:37
  • @ThufirHawat : Thanks for the reply. I have added code some code. The class is being compiled properly. So I have not added code for that. Just the loader – codeKNIGHT Mar 17 '15 at 04:47
  • this works? "java.lang.reflect.Field x = clazz.getDeclaredField("id");" assert you don't get a NULL in x. – G Bisconcini Mar 17 '15 at 05:02
  • I done some thing similar to this, I don't had to load the class, because when you call it, it loads automatically. see if you really have to load, and use reflection to test the class loaded is what you want – G Bisconcini Mar 17 '15 at 05:09
  • Obviosuly if I use a pre loaded class, it works. – codeKNIGHT Mar 17 '15 at 05:11
  • also, see the location of the class inside the container, and the classpath of the container. This should work, there is some thing in the runtime, in the environment of the container that is different of the standalone – G Bisconcini Mar 17 '15 at 05:12
  • I have almost certain that the class loader doesn't are loading well. I think the problem is here " Class> clazz = (activateIndexClass(result)); ", at the runtime of your container, you are loading something strange. I done this, depends of the container. Using jboss, at runtime, is difficult to know where the path of the class – G Bisconcini Mar 17 '15 at 05:20
  • I use Class.forName() instead of ClassLoader , also. its more simple because you do not have to have a new ClassLoader, you can use the existing one running – G Bisconcini Mar 17 '15 at 05:23

0 Answers0