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

Backport DocCommentTree reflection to ReloadableJava17ParserVisitor #4618

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
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 @@ -43,6 +43,8 @@
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -1666,32 +1668,42 @@ private <J2 extends J> J2 convert(Tree t) {
try {
String prefix = source.substring(cursor, max(((JCTree) t).getStartPosition(), cursor));
cursor += prefix.length();
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, docCommentTable.getCommentTree((JCTree) t)));
// Java 21 and 23 have a different return type from getCommentTree; with reflection we can support both
Method getCommentTreeMethod = DocCommentTable.class.getMethod("getCommentTree", JCTree.class);
DocCommentTree commentTree = (DocCommentTree) getCommentTreeMethod.invoke(docCommentTable, t);
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, commentTree));
return j;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
reportJavaParsingException(ex);
throw new IllegalStateException("Failed to invoke getCommentTree method", ex);
} catch (Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
StringBuilder message = new StringBuilder("Failed to convert for the following cursor stack:");
message.append("--- BEGIN PATH ---\n");

List<Tree> paths = stream(getCurrentPath().spliterator(), false).toList();
for (int i = paths.size(); i-- > 0; ) {
JCTree tree = (JCTree) paths.get(i);
if (tree instanceof JCCompilationUnit) {
message.append("JCCompilationUnit(sourceFile = ").append(((JCCompilationUnit) tree).sourcefile.getName()).append(")\n");
} else if (tree instanceof JCClassDecl) {
message.append("JCClassDecl(name = ").append(((JCClassDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else if (tree instanceof JCVariableDecl) {
message.append("JCVariableDecl(name = ").append(((JCVariableDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else {
message.append(tree.getClass().getSimpleName()).append("(line = ").append(lineNumber(tree)).append(")\n");
}
}
reportJavaParsingException(ex);
throw ex;
}
}

message.append("--- END PATH ---\n");
private void reportJavaParsingException(Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
StringBuilder message = new StringBuilder("Failed to convert for the following cursor stack:");
message.append("--- BEGIN PATH ---\n");

ctx.getOnError().accept(new JavaParsingException(message.toString(), ex));
throw ex;
List<Tree> paths = stream(getCurrentPath().spliterator(), false).toList();
for (int i = paths.size(); i-- > 0; ) {
JCTree tree = (JCTree) paths.get(i);
if (tree instanceof JCCompilationUnit) {
message.append("JCCompilationUnit(sourceFile = ").append(((JCCompilationUnit) tree).sourcefile.getName()).append(")\n");
} else if (tree instanceof JCClassDecl) {
message.append("JCClassDecl(name = ").append(((JCClassDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else if (tree instanceof JCVariableDecl) {
message.append("JCVariableDecl(name = ").append(((JCVariableDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else {
message.append(tree.getClass().getSimpleName()).append("(line = ").append(lineNumber(tree)).append(")\n");
}
}

message.append("--- END PATH ---\n");

ctx.getOnError().accept(new JavaParsingException(message.toString(), ex));
}

private <J2 extends J> JRightPadded<J2> convert(Tree t, Function<Tree, Space> suffix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.DCTree;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
Expand Down Expand Up @@ -1668,7 +1667,7 @@ private <J2 extends J> J2 convert(Tree t) {
cursor += prefix.length();
// Java 21 and 23 have a different return type from getCommentTree; with reflection we can support both
Method getCommentTreeMethod = DocCommentTable.class.getMethod("getCommentTree", JCTree.class);
DocCommentTree commentTree = (DocCommentTree) getCommentTreeMethod.invoke(docCommentTable, (JCTree) t);
DocCommentTree commentTree = (DocCommentTree) getCommentTreeMethod.invoke(docCommentTable, t);
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, commentTree));
return j;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
Expand Down Expand Up @@ -2009,7 +2008,7 @@ private ReloadableJava21ModifierResults sortedModifiersAndAnnotations(ModifiersT
} else {
leadingAnnotations.add(annotation);
}
i = cursor -1;
i = cursor - 1;
lastAnnotationPosition = cursor;
continue;
}
Expand Down