Skip to content

Commit

Permalink
Put the getCommentTree reflection code back in (#5089)
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen authored Feb 22, 2025
1 parent 135fa89 commit 8e85f01
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -1744,9 +1746,15 @@ public J visitWildcard(WildcardTree node, Space fmt) {
// The spacing of initialized enums such as `ONE (1)` is handled in the `visitNewClass` method, so set it explicitly to “” here.
String prefix = isEnum(t) ? "" : source.substring(cursor, indexOfNextNonWhitespace(cursor, source));
cursor += prefix.length();
Space p = formatWithCommentTree(prefix, (JCTree) t, docCommentTable.getCommentTree((JCTree) t));
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, p);
// Java 21 and 23 have a different return type from getCommentTree; with reflection we can support both
// Though this is the Java 17 parser, we still need to support it if people use this parser with newer Java versions (e.g. https://github.com/PicnicSupermarket/error-prone-support/pull/1557)
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) {
reportJavaParsingException(ex);
throw ex;
Expand Down

0 comments on commit 8e85f01

Please sign in to comment.