Skip to content

Commit

Permalink
Issue sdaschner#182/sdaschner#191: NPE on enum methods with javadoc c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
mwasson committed May 26, 2021
1 parent 4ac6294 commit d4d7028
Showing 1 changed file with 29 additions and 16 deletions.
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);
}

}

0 comments on commit d4d7028

Please sign in to comment.