From 2a1ac825b30472de91245c79cd0f3df7095a6b30 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 21 May 2025 18:00:45 +0200 Subject: [PATCH 1/3] fix: support JDK 25 API change --- .../palantir/javaformat/java/JavaInput.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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 fea551af8..cca2dbc48 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,13 +39,16 @@ 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; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Supplier; @@ -365,7 +368,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; @@ -462,6 +465,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 { + // Try the JDK 25+ method first (List) + Method listMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics"); + return (Collection) listMethod.invoke(handler); + } catch (Exception e1) { + try { + // Fall back to pre-JDK 25 method (Queue) + Method queueMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics"); + return (Collection) queueMethod.invoke(handler); + } catch (Exception e2) { + // If all else fails, return empty collection + System.err.println("Unable to access diagnostics: " + e2.getMessage()); + return Collections.emptyList(); + } + } + } + private static int updateColumn(int columnI, String originalTokText) { Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText)); if (last > 0) { From 955d289b2d79ed8e127c7eba2bae561aaf8977de Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 2 Jul 2025 11:59:47 +0200 Subject: [PATCH 2/3] Cache the method handle in a constant, no need for two fallbacks --- .../palantir/javaformat/java/JavaInput.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) 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 a79d86dd7..d78b4c4d1 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 @@ -48,7 +48,6 @@ import java.net.URI; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Supplier; @@ -477,19 +476,19 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept @SuppressWarnings("unchecked") private static Collection getDiagnostics(DeferredDiagnosticHandler handler) { try { - // Try the JDK 25+ method first (List) - Method listMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics"); - return (Collection) listMethod.invoke(handler); - } catch (Exception e1) { - try { - // Fall back to pre-JDK 25 method (Queue) - Method queueMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics"); - return (Collection) queueMethod.invoke(handler); - } catch (Exception e2) { - // If all else fails, return empty collection - System.err.println("Unable to access diagnostics: " + e2.getMessage()); - return Collections.emptyList(); - } + 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); } } From 268eea7dd5ebd68609b7a52fd35a709a408b4af2 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Wed, 16 Jul 2025 10:11:16 +0000 Subject: [PATCH 3/3] Add generated changelog entries --- changelog/@unreleased/pr-1367.v2.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 changelog/@unreleased/pr-1367.v2.yml 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