7 Replies Latest reply on Dec 10, 2005 4:08 PM by Shigeru Chiba

    Bug in Annotation (Take 2) ?

    Gonçalo Luiz Newbie

      Hello,
      First of all I must say that I have little experience with javassist (I begun using it last week).

      What I need to to is to process a couple of classes searching for any method containing a specific custom annotation (say pacote.Anotacao).

      In the class i declare the method like this (in the Content class):

       @pacote.Anotacao
       public String getDescription()
       {
       return description;
       }
      


      With javassist I do the following:
      
       CtClass cc = cp.get(className);
       CtMethod[] methods = cc.getDeclaredMethods();
       for (int i = 0; i < methods.length; i++)
       {
       CtMethod m = methods;
       Object[] annotations = m.getAnnotations();
       }
      


      The problem is that m.getAnnotations() rises a java.lang.ClassNotFoundException when I'm processing the annotated method (see above).

      I've inspected javassist's code but I failed to spot what was wrong (I only noticed that the error rises from a Class.forName(...,...,...) as expected given the thrown exception.

      The pojnt is that this very same class (pacote.Anotacao) is processed in the same way the class Content is and it loads sucessfully.

      Note: I'm lauching the javassist's code from an Ant task, fact that I think its irrelevant for this case.

      Below I show you the stack trace hopping that it helps you helping me :)

       [mytask] java.lang.ClassNotFoundException: pacote.Anotacao
       [mytask] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
       [mytask] at java.security.AccessController.doPrivileged(Native Method)
       [mytask] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
       [mytask] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
       [mytask] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
       [mytask] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
       [mytask] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
       [mytask] at java.lang.Class.forName0(Native Method)
       [mytask] at java.lang.Class.forName(Class.java:242)
       [mytask] at javassist.bytecode.annotation.MemberValue.loadClass(MemberValue.java:51)
       [mytask] at javassist.bytecode.annotation.Annotation.toAnnotationType(Annotation.java:281)
       [mytask] at javassist.CtClassType.toAnnotationType(CtClassType.java:404)
       [mytask] at javassist.CtBehavior.getAnnotations(CtBehavior.java:112)
       [mytask] at injector.Inject.inject(Inject.java:79)
       [mytask] at antTask.InjectAccessControl.execute(InjectAccessControl.java:56)
       [mytask] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
       [mytask] at org.apache.tools.ant.Task.perform(Task.java:364)
       [mytask] at org.apache.tools.ant.Target.execute(Target.java:341)
       [mytask] at org.apache.tools.ant.Target.performTasks(Target.java:369)
       [mytask] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
       [mytask] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
       [mytask] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
       [mytask] at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
       [mytask] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
       [mytask] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
       [mytask] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
      


        • 1. Re: Bug in Annotation (Take 2) ? [version]
          Gonçalo Luiz Newbie

          I forgot to mention that I'm using javassist version 3.1RC2 which dates from September 7th 2005 (as mentioned in the project's page).

          Thank you once again.[/url]

          • 2. Re: Bug in Annotation (Take 2) ? [additional stack trace]
            Gonçalo Luiz Newbie

            An aditional stack trace originated from calling the same task from command line (rather than from Eclipse IDE)

             [mytask] java.lang.ClassNotFoundException: pacote.Anotacao
             [mytask] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
             [mytask] at java.security.AccessController.doPrivileged(Native Method)
             [mytask] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
             [mytask] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
             [mytask] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
             [mytask] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
             [mytask] at java.lang.Class.forName0(Native Method)
             [mytask] at java.lang.Class.forName(Class.java:242)
             [mytask] at javassist.bytecode.annotation.MemberValue.loadClass(MemberValue.java:51)
             [mytask] at javassist.bytecode.annotation.Annotation.toAnnotationType(Annotation.java:281)
             [mytask] at javassist.CtClassType.toAnnotationType(CtClassType.java:404)
             [mytask] at javassist.CtBehavior.getAnnotations(CtBehavior.java:112)
             [mytask] at injector.Inject.inject(Inject.java:79)
             [mytask] at antTask.InjectAccessControl.execute(InjectAccessControl.java:56)
             [mytask] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
             [mytask] at org.apache.tools.ant.Task.perform(Task.java:364)
             [mytask] at org.apache.tools.ant.Target.execute(Target.java:341)
             [mytask] at org.apache.tools.ant.Target.performTasks(Target.java:369)
             [mytask] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
             [mytask] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
             [mytask] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
             [mytask] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
             [mytask] at org.apache.tools.ant.Main.runBuild(Main.java:668)
             [mytask] at org.apache.tools.ant.Main.startAnt(Main.java:187)
             [mytask] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
             [mytask] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
            
            


            • 3. Re: Bug in Annotation (Take 2) ?
              Shigeru Chiba Expert

              I think the exception was thrown because the @pacote.Anotacao interface is not accessible from your code using Javassist. Can you check your code?

              • 4. Re: Bug in Annotation (Take 2) ?
                Gonçalo Luiz Newbie

                 

                "chiba" wrote:
                I think the exception was thrown because the @pacote.Anotacao interface is not accessible from your code using Javassist. Can you check your code?


                Hello, Chiba.

                Indeed my pacote.Anotacao is not accessible from my code using Javassist. However since its available from "regular" java code, I'd like to know the differences between "regular" java accessibility and "via javassist" accessibility.

                Thank you once again.

                • 5. Re: Bug in Annotation (Take 2) ?
                  Shigeru Chiba Expert

                  Hi,

                  I'd like to know the differences between "regular" java accessibility and "via javassist" accessibility.


                  There should be no difference since Javassist code simply calls
                  Class#forName() for accessing a class. Does pacote.Anotacao
                  really exist under a directory included in CLASSPATH?

                  Another check is to directly call Class#forName("pacote.Anotacao")
                  in your code. Does the call successfully return a Class object for that class?


                  • 6. Re: Bug in Annotation (Take 2) ?
                    Gonçalo Luiz Newbie

                     

                    "chiba" wrote:
                    Hi,

                    I'd like to know the differences between "regular" java accessibility and "via javassist" accessibility.


                    There should be no difference since Javassist code simply calls
                    Class#forName() for accessing a class. Does pacote.Anotacao
                    really exist under a directory included in CLASSPATH?

                    Another check is to directly call Class#forName("pacote.Anotacao")
                    in your code. Does the call successfully return a Class object for that class?


                    Hello,
                    Yes the Class#forName("pacote.Anotacao") successfully returns the correct object.

                    Its wierd but it happens :(

                    • 7. Re: Bug in Annotation (Take 2) ?
                      Shigeru Chiba Expert

                      Maybe javassist.jar has been loaded by a different class
                      loader from your application code. Can you edit the following line to use a different class loader?

                      in toAnnotationType() in CtClassType

                      ClassLoader cl = Thread.currentThread().getContextClassLoader();

                      For example, it might have to be changed to:

                      ClassLoader cl = <your app. class>.class.getClassLoader();