Skip to content

Commit

Permalink
Fix test0008 and others: empty block comment misunderstood by javac a…
Browse files Browse the repository at this point in the history
…s block comment

Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Dec 16, 2024
1 parent b643067 commit 45d829a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAnyPattern;
Expand Down Expand Up @@ -125,6 +124,7 @@
import com.sun.tools.javac.tree.JCTree.JCWildcard;
import com.sun.tools.javac.tree.JCTree.JCYield;
import com.sun.tools.javac.tree.JCTree.Tag;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.Log;
Expand Down Expand Up @@ -1231,7 +1231,11 @@ private FieldDeclaration convertFieldDeclaration(JCVariableDecl javac, ASTNode p
private void setJavadocForNode(JCTree javac, ASTNode node) {
Comment c = this.javacCompilationUnit.docComments.getComment(javac);
if(c != null && (c.getStyle() == Comment.CommentStyle.JAVADOC_BLOCK || c.getStyle() == CommentStyle.JAVADOC_LINE)) {
Javadoc javadoc = (Javadoc)convert(c, javac);
org.eclipse.jdt.core.dom.Comment comment = convert(c, javac);
if( !(comment instanceof Javadoc)) {
return;
}
Javadoc javadoc = (Javadoc)comment;
if (node instanceof BodyDeclaration bodyDeclaration) {
bodyDeclaration.setJavadoc(javadoc);
bodyDeclaration.setSourceRange(javadoc.getStartPosition(), bodyDeclaration.getStartPosition() + bodyDeclaration.getLength() - javadoc.getStartPosition());
Expand Down Expand Up @@ -3401,27 +3405,35 @@ private Name convertName(com.sun.tools.javac.util.Name javac) {


public org.eclipse.jdt.core.dom.Comment convert(Comment javac, JCTree context) {
if ((javac.getStyle() == CommentStyle.JAVADOC_BLOCK || javac.getStyle() == CommentStyle.JAVADOC_LINE) && context != null) {
CommentStyle style = javac.getStyle();
if ((style == CommentStyle.JAVADOC_BLOCK || style == CommentStyle.JAVADOC_LINE) && context != null) {
var docCommentTree = this.javacCompilationUnit.docComments.getCommentTree(context);
if (docCommentTree instanceof DCDocComment dcDocComment) {
JavadocConverter javadocConverter = new JavadocConverter(this, dcDocComment, TreePath.getPath(this.javacCompilationUnit, context), this.buildJavadoc);
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
if (this.ast.apiLevel() >= AST.JLS23) {
javadoc.setMarkdown(javac.getStyle() == CommentStyle.JAVADOC_LINE);
String raw = javadocConverter.getRawContent();
if( !"/**/".equals(raw)) {
this.javadocConverters.add(javadocConverter);
Javadoc javadoc = javadocConverter.convertJavadoc();
if (this.ast.apiLevel() >= AST.JLS23) {
javadoc.setMarkdown(javac.getStyle() == CommentStyle.JAVADOC_LINE);
}
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
} else {
style = CommentStyle.BLOCK;
}
this.javadocDiagnostics.addAll(javadocConverter.getDiagnostics());
return javadoc;
}
}
org.eclipse.jdt.core.dom.Comment jdt = switch (javac.getStyle()) {
org.eclipse.jdt.core.dom.Comment jdt = switch (style) {
case LINE -> this.ast.newLineComment();
case BLOCK -> this.ast.newBlockComment();
case JAVADOC_BLOCK -> this.ast.newJavadoc();
case JAVADOC_LINE -> this.ast.newJavadoc();
};
javac.isDeprecated(); javac.getText(); // initialize docComment
jdt.setSourceRange(javac.getSourcePos(0), javac.getText().length());
int startPos = javac.getPos().getStartPosition();
int endPos = javac.getPos().getEndPosition(this.javacCompilationUnit.endPositions);
jdt.setSourceRange(startPos, endPos-startPos);
return jdt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.sun.source.util.DocTreePath;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.DCTree;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.tree.DCTree.DCAuthor;
import com.sun.tools.javac.tree.DCTree.DCBlockTag;
import com.sun.tools.javac.tree.DCTree.DCComment;
Expand Down Expand Up @@ -57,7 +55,9 @@
import com.sun.tools.javac.tree.DCTree.DCUses;
import com.sun.tools.javac.tree.DCTree.DCValue;
import com.sun.tools.javac.tree.DCTree.DCVersion;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.Convert;
import com.sun.tools.javac.util.JCDiagnostic;

Expand All @@ -79,6 +79,7 @@ class JavadocConverter {
private final int endOffset;
private boolean buildJavadoc;
private final TreePath contextTreePath;
private String rawContent;

public final Map<ASTNode, DocTreePath> converted = new HashMap<>();

Expand Down Expand Up @@ -120,13 +121,19 @@ private void commonSettings(ASTNode res, DCTree javac) {
}
}
}

public String getRawContent() {
if( this.rawContent == null )
this.rawContent = this.javacConverter.rawText.substring(this.initialOffset, this.endOffset);
return rawContent;
}

Javadoc convertJavadoc() {
Javadoc res = this.ast.newJavadoc();
res.setSourceRange(this.initialOffset, this.endOffset - this.initialOffset);
try {
if( this.javacConverter.ast.apiLevel == AST.JLS2_INTERNAL) {
String rawContent = this.javacConverter.rawText.substring(this.initialOffset, this.endOffset);
String rawContent = getRawContent();
try {
res.setComment(rawContent);
} catch( IllegalArgumentException iae) {
Expand Down

0 comments on commit 45d829a

Please sign in to comment.