diff --git a/changelog/@unreleased/pr-1367.v2.yml b/changelog/@unreleased/pr-1367.v2.yml new file mode 100644 index 000000000..13a22d751 --- /dev/null +++ b/changelog/@unreleased/pr-1367.v2.yml @@ -0,0 +1,11 @@ +type: fix +fix: + description: |- + Fix the following error when running under JDK 25 EA: + + ``` + 'java.util.Queue com.sun.tools.javac.util.Log$DeferredDiagnosticHandler.getDiagnostics()' + java.lang.NoSuchMethodError: 'java.util.Queue com.sun.tools.javac.util.Log$DeferredDiagnosticHandler.getDiagnostics()' + ``` + links: + - https://github.com/palantir/palantir-java-format/pull/1367 diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java index 3ec68a2a3..e3745dd5e 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java @@ -39,10 +39,12 @@ import com.sun.tools.javac.parser.Tokens.TokenKind; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler; import com.sun.tools.javac.util.Options; import java.io.IOException; +import java.lang.reflect.Method; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -368,7 +370,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept }); DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log); ImmutableList rawToks = JavacTokens.getTokens(text, context, stopTokens); - if (diagnostics.getDiagnostics().stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) { + if (getDiagnostics(diagnostics).stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) { return ImmutableList.of(new Tok(0, "", "", 0, 0, true, null)); // EOF } int kN = 0; @@ -465,6 +467,33 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept return ImmutableList.copyOf(toks); } + /** + * Gets diagnostics from a DeferredDiagnosticHandler using reflection. + * This method handles the API change in JDK 25 where getDiagnostics() + * changed from returning Queue to List. + * + * @param handler the diagnostic handler + * @return a collection of diagnostics + */ + @SuppressWarnings("unchecked") + private static Collection getDiagnostics(DeferredDiagnosticHandler handler) { + try { + return (Collection) GET_DIAGNOSTICS.invoke(handler); + } catch (ReflectiveOperationException e) { + throw new LinkageError(e.getMessage(), e); + } + } + + private static final Method GET_DIAGNOSTICS; + + static { + try { + GET_DIAGNOSTICS = DeferredDiagnosticHandler.class.getMethod("getDiagnostics"); + } catch (NoSuchMethodException e) { + throw new LinkageError(e.getMessage(), e); + } + } + @SuppressWarnings("for-rollout:NullAway") private static int updateColumn(int columnI, String originalTokText) { Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));