0

Disclaimer: I am only a lowly trainee, so please forgive me if I made elementary mistakes :(

I am writing an automatic API generator, and the classes need JavaDoc as well as comments because some of the values the API contains shouldn't be written down in the JavaDoc (exampleResponse for example).

However, it seems that the comments above the individual Methods replace the Javadoc, so when I want to get the description from the JavaDoc (which i want to do so I don't have to write it again in the comments), I have a problem.

Using getJavadoc() always returns null. I also attempted to use getOrphanComments(), but it returned null. Did I misunderstand the documentation? I assumed if I wrote two comments above a method, the top one would move to orphanComments for that method.

Is there any way to work around this?

Alex Eggers
  • 328
  • 3
  • 16
  • Could you give a simple example of the generated code? – Samuel Åslund Jan 14 '16 at 12:19
  • @SamuelÅslund My method is something like this: /** \* Javadoc \*/ /* \* APIAnnotations etc \*/ public Generic getBootstrap() { methodstuff.do(); } The output of getComment().toString() is: /* \* APIAnnotations etc \*/ Holy crap pressing enter saves the response :O I hope i didnt flood your inbox just now :S – Alex Eggers Jan 14 '16 at 13:00
  • I can't find the documentation now but when I read it i'm pretty sure it said that the comment _directly_ above the item documented should have the /** to become a javadoc comment. you have a standard comment in between thus your javadoc comment does not qualify. I would expect the way for you to include developer instructions in the generated code is to go the same way as Eclipse and put it below the documented item. – Samuel Åslund Jan 14 '16 at 23:26
  • @SamuelÅslund could you expand on "below the documented item"? – Alex Eggers Jan 15 '16 at 15:46

2 Answers2

0

Let the MethodDeclaration object is method than you can get java doc using

if( method.hasComment() && method.getComment() instanceof JavadocComment ){
    JavadocComment javaDoc = (JavadocComment)method.getComment();
    // now you can get the content using 
    String content = javaDoc.getContent();
}
Waqas Ahmed
  • 1,321
  • 1
  • 14
  • 23
0

For the following types:

public final String             name;
public final String             signature;
public final String             returnType;      
public final Type               returnFullType; // com.github.javaparser.ast.type.Type
public final String             body;
public final String[]           modifiers;
public final String[]           parameterNames;
public final String[]           parameterTypes;
public final Type[]             parameterFullTypes; // com.github.javaparser.ast.type.Type
public final String[]           exceptions;
public final String             jdComment;
public final MethodDeclaration  nativeJP_API_reference; // com.github.javaparser.ast.body.MethodDeclaration

This is a constructor for a class Method of my own concoction:

Method (MethodDeclaration md)
{
    NodeList<Modifier>      ml  = md.getModifiers();
    NodeList<Parameter>     pl  = md.getParameters();
    NodeList<ReferenceType> te  = md.getThrownExceptions();

    this.nativeJP_API_reference = md;
    this.name                   = md.getNameAsString();
    this.signature              = md.getDeclarationAsString();
    this.returnType             = md.getType().toString();
    this.returnFullType         = md.getType();
    this.body                   = md.getBody().isPresent() ? md.getBody().get().toString() : null;  // In ConstructorDeclaration, this is an Optional<BlockStmt>, not here!
    this.modifiers              = new String[ml.size()];
    this.parameterNames         = new String[pl.size()];
    this.parameterTypes         = new String[pl.size()];
    this.parameterFullTypes     = new com.github.javaparser.ast.type.Type[pl.size()];
    this.exceptions             = new String[te.size()];
    this.jdComment              = md.hasJavaDocComment() ? md.getJavadocComment().get().toString() : null;

    int i = 0;
    for (Parameter p : pl)
    {
        parameterNames[i]       = p.getName().toString();
        parameterTypes[i]       = p.getType().toString();
        parameterFullTypes[i]   = p.getType();
        i++;
    }
    i = 0;
    for (Modifier m : ml)       modifiers[i++]  = m.toString();
    i = 0;
    for (ReferenceType r : te)  exceptions[i++] = r.toString();;
}