QDox
  1. QDox
  2. QDOX-255

package annotations can't be obtained

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      linux, openjdk 1.7.0_65
    • Number of attachments :
      0

      Description

      I'm using this code to obtain packages annotation from a package and it always return an empty list:

      javaField.getDeclaringClass().getPackage().getAnnotations()

      I have a package-info.java file in the package that contain this:

      @Anot
      package net.jcs.jboilerdowntest;
      import net.jcs.annotation.Anot;
      

      A also tried this with the same result:

      @net.jcs.annotation.Anot
      package net.jcs.jboilerdowntest;
      

        Activity

        Robert Scholte made changes -
        Field Original Value New Value
        Description I'm using this code to obtain packages annotation from a package and it always return an empty list:

        javaField.getDeclaringClass().getPackage().getAnnotations()

        I have a package-info.java file in the package that contain this:

        @Anot
        package net.jcs.jboilerdowntest;
        import net.jcs.annotation.Anot;

        A also tried this with the same result:

        @net.jcs.annotation.Anot
        package net.jcs.jboilerdowntest;

        I'm using this code to obtain packages annotation from a package and it always return an empty list:

        {{javaField.getDeclaringClass().getPackage().getAnnotations()}}

        I have a package-info.java file in the package that contain this:
        {code}
        @Anot
        package net.jcs.jboilerdowntest;
        import net.jcs.annotation.Anot;
        {code}
        A also tried this with the same result:
        {code}
        @net.jcs.annotation.Anot
        package net.jcs.jboilerdowntest;
        {code}
        Hide
        Robert Scholte added a comment -
            public void testPackageAnnotation()
            {
                String source = "@Anot\r\n" + 
                    "package net.jcs.jboilerdowntest;\r\n" + 
                    "import net.jcs.annotation.Anot;";
                
                JavaProjectBuilder builder = new JavaProjectBuilder();
                JavaPackage pckg = builder.addSource( new StringReader( source) ).getPackage();
                assertEquals( "net.jcs.jboilerdowntest", pckg.getName() );
                assertEquals( 1, pckg.getAnnotations().size() );
            }
        

        This succeeds. Most important difference is that I don't search the package based on a field. The package of a JavaClass and of the package-info are two different instances, that's why you got an empty list. However, I see there is still some work to do here, because I can't get enough details of the Anot.

        Show
        Robert Scholte added a comment - public void testPackageAnnotation() { String source = "@Anot\r\n" + " package net.jcs.jboilerdowntest;\r\n" + " import net.jcs.annotation.Anot;" ; JavaProjectBuilder builder = new JavaProjectBuilder(); JavaPackage pckg = builder.addSource( new StringReader( source) ).getPackage(); assertEquals( "net.jcs.jboilerdowntest" , pckg.getName() ); assertEquals( 1, pckg.getAnnotations().size() ); } This succeeds. Most important difference is that I don't search the package based on a field. The package of a JavaClass and of the package-info are two different instances, that's why you got an empty list. However, I see there is still some work to do here, because I can't get enough details of the Anot .
        Hide
        Jonatan Cloutier added a comment - - edited

        Ok, I tried like this:

        JavaPackage packageByName = builder.getPackageByName(javaField.getDeclaringClass().getPackage().getName());
        JavaAnnotation javaAnnotation = packageByName.getAnnotations();
        

        and this give me the annotation back or well, at least there is a result in the list. But as you pointed out, we can't get most info from it.

        java.lang.NullPointerException
                at com.thoughtworks.qdox.model.impl.DefaultJavaType.resolveRealClass(DefaultJavaType.java:281)
                at com.thoughtworks.qdox.model.impl.DefaultJavaType.getCanonicalName(DefaultJavaType.java:894)
        

        As 'another' work around (in my specific case) I can use:

        javaAnnotation.getType().getFullyQualifiedName();
        

        Another thing, I really think that getting a package from a class, from a field should give me the same data than if I get the package directly. it's the same to me if I get a class from one of it's field or directly from it's className, the class representation should be exactly the same (and I suppose it's already the case for classes.)

        Thank you for looking into this.

        Show
        Jonatan Cloutier added a comment - - edited Ok, I tried like this: JavaPackage packageByName = builder.getPackageByName(javaField.getDeclaringClass().getPackage().getName()); JavaAnnotation javaAnnotation = packageByName.getAnnotations(); and this give me the annotation back or well, at least there is a result in the list. But as you pointed out, we can't get most info from it. java.lang.NullPointerException at com.thoughtworks.qdox.model.impl.DefaultJavaType.resolveRealClass(DefaultJavaType.java:281) at com.thoughtworks.qdox.model.impl.DefaultJavaType.getCanonicalName(DefaultJavaType.java:894) As 'another' work around (in my specific case) I can use: javaAnnotation.getType().getFullyQualifiedName(); Another thing, I really think that getting a package from a class, from a field should give me the same data than if I get the package directly. it's the same to me if I get a class from one of it's field or directly from it's className, the class representation should be exactly the same (and I suppose it's already the case for classes.) Thank you for looking into this.
        Hide
        Robert Scholte added a comment -

        For you it might be the same, but how to distinguish the following:

        package-info.java
        @Info
        package com.foo.bar
        
        MyClass.java
        @Info
        package com.foo.bar
        
        public class MyClass {
          private String someField
        }
        

        You wanted to get the first one, but used the code of the second one.

        Show
        Robert Scholte added a comment - For you it might be the same, but how to distinguish the following: package-info.java @Info package com.foo.bar MyClass.java @Info package com.foo.bar public class MyClass { private String someField } You wanted to get the first one, but used the code of the second one.
        Hide
        Jonatan Cloutier added a comment -

        I didn't know you could set an annotation in a class package definition, afaik, that's probably a very bad idea, since the annotation is still applied for the whole package, meaning every class in that package, not only the current file. To get the matter worst, you can't annotate a package more than once.

        The jls is clear about that: http://docs.oracle.com/javase/specs/jls/se5.0/html/packages.html#26626 or in java 8 http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4

        So in any case, whatever how I get it, from whatever class I get it, I should always get exactly the same packages definitions.

        Do you understand the same thing as me?

        Show
        Jonatan Cloutier added a comment - I didn't know you could set an annotation in a class package definition, afaik, that's probably a very bad idea, since the annotation is still applied for the whole package, meaning every class in that package, not only the current file. To get the matter worst, you can't annotate a package more than once. The jls is clear about that: http://docs.oracle.com/javase/specs/jls/se5.0/html/packages.html#26626 or in java 8 http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4 So in any case, whatever how I get it, from whatever class I get it, I should always get exactly the same packages definitions. Do you understand the same thing as me?
        Hide
        Robert Scholte added a comment -

        Keep in mind that the main task of QDox is to parse code by syntax. If developers produce invalid constructions like these, it shouldn't block QDox from parsing. I don't know what the different compilers do when multiple {{package}}s are annotated. For me the most important thing is that every Annotation can be reached. I will leave this issue open until I know all the details.

        Show
        Robert Scholte added a comment - Keep in mind that the main task of QDox is to parse code by syntax. If developers produce invalid constructions like these, it shouldn't block QDox from parsing. I don't know what the different compilers do when multiple {{package}}s are annotated. For me the most important thing is that every Annotation can be reached. I will leave this issue open until I know all the details.

          People

          • Assignee:
            Unassigned
            Reporter:
            Jonatan Cloutier
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: