Accessing Javadoc with Javaparser returns null

2019-08-31 09:37发布

问题:

I'm parsing Java source files with Javaparser (javaparser-core 2.0.0) to get Javadoc comments from methods.

However, I always get null When I call MethodDeclaration.getJavaDoc(). Looking at the source of MethodDeclaration, I can see why:

public JavadocComment getJavaDoc() {
    return null;
}

(It appears Javadoc is available from Node.getComment() instead)

My question is: Why is method Javadoc not available from MethodDeclaration.getJavaDoc()?

回答1:

The answer is: because we added the interface DocumentableNode but later we did not implemented correctly. Currently all nodes can have comments attached, however we wanted to add an interface to differentiate the nodes that could have a proper JavaDoc comment (technically you can add a Javadoc comment to a local variable but you shouldn't). Note that a ticket is already open for this issue https://github.com/javaparser/javaparser/issues/141

Disclaimer: I am a JavaParser contributor