QDox
  1. QDox
  2. QDOX-247

Annotations are skipped in JavaSource toString method

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Java API
    • Labels:
      None
    • Number of attachments :
      0

      Description

      I'm having an issue with QDox that I'm parsing a Java source file to a JavaClass object and write it back to file again by using the JavaSource class. Maybe I'm doing this in a wrong way but I cant find any code that gives me the feeling that I am.

      In short, the case is:

      JavaDocBuilder builder = new JavaDocBuilder();
      builder.addSource(new File("MyClass.java"));
      JavaClass clazz = builder.getClassByName("MyClass");
      String javaSource = clazz.getSource().toString();
      

      My feeling is that this should be processed in the writeBody methods of all AbstractJavaEntity classes.

      I'm not sure it this is the way to report an issue/feature. So my apologies in case it's not.

        Activity

        Hide
        Rob Sonke added a comment - - edited

        I solved this for myself by some minor changes in AbstractJavaEntity:

        protected void write(IndentBuffer result) {
            commentHeader(result);
            // added custom method for printing annotations
            writeAnnotations(result);
            writeBody(result);
        }
          
        protected void writeAnnotations(IndentBuffer result)
        {
            Annotation[] annotations = this.getAnnotations();
            	
            for (int i = 0; i < annotations.length; i++) {
                result.write(annotations[i].toString());
                result.newline();
            }
        }
        

        This works fine so far. It's not working for method parameter annotations as that's handled inside the JavaMethod class but adding support for that should be easy.

        Show
        Rob Sonke added a comment - - edited I solved this for myself by some minor changes in AbstractJavaEntity: protected void write(IndentBuffer result) { commentHeader(result); // added custom method for printing annotations writeAnnotations(result); writeBody(result); } protected void writeAnnotations(IndentBuffer result) { Annotation[] annotations = this .getAnnotations(); for ( int i = 0; i < annotations.length; i++) { result.write(annotations[i].toString()); result.newline(); } } This works fine so far. It's not working for method parameter annotations as that's handled inside the JavaMethod class but adding support for that should be easy.
        Hide
        Robert Scholte added a comment -

        With QDox2 this part has been rewritten. There's a ModelWriter with 1 default implementation (which should already fix this issue) and it is very easy to write your own version.

        Show
        Robert Scholte added a comment - With QDox2 this part has been rewritten. There's a ModelWriter with 1 default implementation (which should already fix this issue) and it is very easy to write your own version.
        Hide
        Rob Sonke added a comment -

        Thanks for your fast reply. I'll give qdox2 a try, is it backwards compatible?

        Some more testing showed some issues with my above sample code too but I dont think it matters anymore as you're targetting qdox2 only probably.

        Show
        Rob Sonke added a comment - Thanks for your fast reply. I'll give qdox2 a try, is it backwards compatible? Some more testing showed some issues with my above sample code too but I dont think it matters anymore as you're targetting qdox2 only probably.
        Hide
        Robert Scholte added a comment -

        No, it is not 100% backwards compatible: most exposed methods were for internal usage and shouldn't be used in the first place. These methods are now hidden. You should start with the JavaProjectBuilder now.
        I've created an alpha-version in Nexus repository of Codehaus, which will not make it to Maven Central. Once I'm happy with the new API, I'll start with beta-releases. These still contain deprecated methods, which will be removed with the first 2.0 version.
        There's no roadmap, so I can't give you any release-dates.

        Show
        Robert Scholte added a comment - No, it is not 100% backwards compatible: most exposed methods were for internal usage and shouldn't be used in the first place. These methods are now hidden. You should start with the JavaProjectBuilder now. I've created an alpha-version in Nexus repository of Codehaus, which will not make it to Maven Central. Once I'm happy with the new API, I'll start with beta-releases. These still contain deprecated methods, which will be removed with the first 2.0 version. There's no roadmap, so I can't give you any release-dates.
        Hide
        Rob Sonke added a comment -

        Oke clear. I'll check this code and give it a try. Feel free to wontfix this issue.

        Show
        Rob Sonke added a comment - Oke clear. I'll check this code and give it a try. Feel free to wontfix this issue.
        Hide
        Rob Sonke added a comment -

        It works like a charm with the qdox2 version. I implemented my own ModelWriter and it works great.

        A few things that might be interesting for you which I came across using this:

        • The ModelWriter should have a getBuffer method or something.
        • or the DefaultModelWriter should make the buffer field protected

        Because I now have to write my own complete ModelWriter were I only want to override one method in the DefaultModelWriter.

        And a small issue in DefaultModelWriter:

        • the new line after each annotation is missing in the DefaultModelWriter which now ends up in code like: @Transientpublic class ...
        Show
        Rob Sonke added a comment - It works like a charm with the qdox2 version. I implemented my own ModelWriter and it works great. A few things that might be interesting for you which I came across using this: The ModelWriter should have a getBuffer method or something. or the DefaultModelWriter should make the buffer field protected Because I now have to write my own complete ModelWriter were I only want to override one method in the DefaultModelWriter. And a small issue in DefaultModelWriter: the new line after each annotation is missing in the DefaultModelWriter which now ends up in code like: @Transientpublic class ...
        Hide
        Robert Scholte added a comment -

        Both issues have been fixed on the trunk.
        Any suggestion is really appreciated, so if you have more thoughts, either create a new issue or send them to the mailinglist. This is the time we can still change the API.

        Thanks!

        (closing since the original issue can't be reproduced with the trunk)

        Show
        Robert Scholte added a comment - Both issues have been fixed on the trunk. Any suggestion is really appreciated, so if you have more thoughts, either create a new issue or send them to the mailinglist. This is the time we can still change the API. Thanks! (closing since the original issue can't be reproduced with the trunk)
        Robert Scholte made changes -
        Field Original Value New Value
        Status Open [ 1 ] Closed [ 6 ]
        Assignee Robert Scholte [ rfscholte ]
        Resolution Cannot Reproduce [ 5 ]

          People

          • Assignee:
            Robert Scholte
            Reporter:
            Rob Sonke
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: