Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #182/#191: NPE on enum methods with javadoc comments #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Expression;
Expand Down Expand Up @@ -38,7 +40,7 @@
public class JavaDocParserVisitor extends VoidVisitorAdapter<Void> {

private String packageName;
private String className;
private String typeName;
private final Map<MethodIdentifier, MethodComment> methodComments;
private final Map<String, ClassComment> classComments = new HashMap<>();

Expand All @@ -54,14 +56,25 @@ public void visit(PackageDeclaration packageDeclaration, Void arg) {

@Override
public void visit(ClassOrInterfaceDeclaration classOrInterface, Void arg) {
className = calculateClassName(classOrInterface);
recordTypeInfo(classOrInterface);

classOrInterface.getComment()
super.visit(classOrInterface, arg);
}

@Override
public void visit(EnumDeclaration enumDeclaration, Void arg) {
recordTypeInfo(enumDeclaration);

super.visit(enumDeclaration, arg);
}

private void recordTypeInfo(TypeDeclaration typeDeclaration) {
typeName = calculateTypeName(typeDeclaration);

typeDeclaration.getComment()
.filter(Comment::isJavadocComment)
.map(this::toJavaDoc)
.ifPresent(this::recordClassComment);

super.visit(classOrInterface, arg);
.ifPresent(this::recordTypeComment);
}

private Javadoc toJavaDoc(Comment comment) {
Expand All @@ -72,16 +85,16 @@ private boolean isDeprecated(Javadoc javadoc) {
return javadoc.getBlockTags().stream().anyMatch(t -> t.getType() == JavadocBlockTag.Type.DEPRECATED);
}

private String calculateClassName(ClassOrInterfaceDeclaration classOrInterface) {
private String calculateTypeName(TypeDeclaration type) {
if (StringUtils.isBlank(packageName))
return classOrInterface.getNameAsString();
return packageName.replace('.', '/') + "/" + classOrInterface.getNameAsString();
return type.getNameAsString();
return packageName.replace('.', '/') + "/" + type.getNameAsString();
}

private void recordClassComment(Javadoc javadoc) {
private void recordTypeComment(Javadoc javadoc) {
String comment = javadoc.getDescription().toText();
Map<Integer, String> responseComments = createResponseComments(javadoc);
classComments.put(className, new ClassComment(comment, responseComments, isDeprecated(javadoc)));
classComments.put(typeName, new ClassComment(comment, responseComments, isDeprecated(javadoc)));
}

@Override
Expand All @@ -94,10 +107,10 @@ public void visit(FieldDeclaration field, Void arg) {
}

private void createFieldComment(Javadoc javadoc, FieldDeclaration field) {
ClassComment classComment = classComments.get(className);
ClassComment classComment = classComments.get(typeName);
if (classComment == null) {
classComment = new ClassComment();
classComments.put(className, classComment);
classComments.put(typeName, classComment);
}
classComment.getFieldComments().add(createMemberParamTag(javadoc.getDescription(), field.getAnnotations().stream()));
}
Expand All @@ -116,7 +129,7 @@ private void recordMethodComment(Javadoc javadoc, MethodDeclaration method) {
String comment = javadoc.getDescription().toText();
List<MemberParameterTag> tags = createMethodParameterTags(javadoc, method);
Map<Integer, String> responseComments = createResponseComments(javadoc);
methodComments.put(identifier, new MethodComment(comment, tags, responseComments, classComments.get(className), isDeprecated(javadoc)));
methodComments.put(identifier, new MethodComment(comment, tags, responseComments, classComments.get(typeName), isDeprecated(javadoc)));
}

private List<MemberParameterTag> createMethodParameterTags(Javadoc javadoc, MethodDeclaration method) {
Expand Down Expand Up @@ -176,9 +189,9 @@ private MethodIdentifier calculateMethodIdentifier(MethodDeclaration method) {
String returnType = method.getType().asString().replace('.', '/');

if (method.isStatic()) {
return ofStatic(className, method.getNameAsString(), returnType, parameters);
return ofStatic(typeName, method.getNameAsString(), returnType, parameters);
}
return ofNonStatic(className, method.getNameAsString(), returnType, parameters);
return ofNonStatic(typeName, method.getNameAsString(), returnType, parameters);
}

}