Skip to content

Commit c69bd6f

Browse files
authored
Support Gradle 8.x on Java 23+ (#6725)
1 parent 802bc72 commit c69bd6f

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import javax.lang.model.element.Modifier;
4949
import javax.lang.model.element.Name;
5050
import java.lang.reflect.Field;
51+
import java.lang.reflect.InvocationTargetException;
52+
import java.lang.reflect.Method;
5153
import java.nio.charset.Charset;
5254
import java.nio.file.Path;
5355
import java.util.*;
@@ -1850,8 +1852,14 @@ public J visitWildcard(WildcardTree node, Space fmt) {
18501852
// The spacing of initialized enums such as `ONE (1)` is handled in the `visitNewClass` method, so set it explicitly to “” here.
18511853
String prefix = isEnum(t) ? "" : source.substring(cursor, indexOfNextNonWhitespace(cursor, source));
18521854
cursor += prefix.length();
1853-
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, docCommentTable.getCommentTree((JCTree) t)));
1855+
// Java 21 and 23 have a different return type from getCommentTree; with reflection we can support both
1856+
Method getCommentTreeMethod = DocCommentTable.class.getMethod("getCommentTree", JCTree.class);
1857+
DocCommentTree commentTree = (DocCommentTree) getCommentTreeMethod.invoke(docCommentTable, t);
1858+
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, commentTree));
18541859
return j;
1860+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
1861+
reportJavaParsingException(ex);
1862+
throw new IllegalStateException("Failed to invoke getCommentTree method", ex);
18551863
} catch (Throwable ex) {
18561864
reportJavaParsingException(ex);
18571865
throw ex;

0 commit comments

Comments
 (0)