Skip to content

Commit

Permalink
Use reflection to invoke getCommentTree to support Java 21 & 23
Browse files Browse the repository at this point in the history
Fixes #4540
  • Loading branch information
timtebeek committed Oct 6, 2024
1 parent 094e714 commit 2042135
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -1664,7 +1665,10 @@ 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, (JCTree) t);
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, commentTree));
return j;
} catch (Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
Expand Down

0 comments on commit 2042135

Please sign in to comment.