Skip to content

Commit 1a47ffa

Browse files
authored
Fix NoSuchMethodError for DeferredDiagnosticHandler.getDiagnostics() on JDK 25 EA (#1367)
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()' ```
1 parent 7dfb900 commit 1a47ffa

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type: fix
2+
fix:
3+
description: |-
4+
Fix the following error when running under JDK 25 EA:
5+
6+
```
7+
'java.util.Queue com.sun.tools.javac.util.Log$DeferredDiagnosticHandler.getDiagnostics()'
8+
java.lang.NoSuchMethodError: 'java.util.Queue com.sun.tools.javac.util.Log$DeferredDiagnosticHandler.getDiagnostics()'
9+
```
10+
links:
11+
- https://github.com/palantir/palantir-java-format/pull/1367

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
import com.sun.tools.javac.parser.Tokens.TokenKind;
4040
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
4141
import com.sun.tools.javac.util.Context;
42+
import com.sun.tools.javac.util.JCDiagnostic;
4243
import com.sun.tools.javac.util.Log;
4344
import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
4445
import com.sun.tools.javac.util.Options;
4546
import java.io.IOException;
47+
import java.lang.reflect.Method;
4648
import java.net.URI;
4749
import java.util.ArrayList;
4850
import java.util.Collection;
@@ -368,7 +370,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
368370
});
369371
DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log);
370372
ImmutableList<RawTok> rawToks = JavacTokens.getTokens(text, context, stopTokens);
371-
if (diagnostics.getDiagnostics().stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) {
373+
if (getDiagnostics(diagnostics).stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) {
372374
return ImmutableList.of(new Tok(0, "", "", 0, 0, true, null)); // EOF
373375
}
374376
int kN = 0;
@@ -465,6 +467,33 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
465467
return ImmutableList.copyOf(toks);
466468
}
467469

470+
/**
471+
* Gets diagnostics from a DeferredDiagnosticHandler using reflection.
472+
* This method handles the API change in JDK 25 where getDiagnostics()
473+
* changed from returning Queue to List.
474+
*
475+
* @param handler the diagnostic handler
476+
* @return a collection of diagnostics
477+
*/
478+
@SuppressWarnings("unchecked")
479+
private static Collection<JCDiagnostic> getDiagnostics(DeferredDiagnosticHandler handler) {
480+
try {
481+
return (Collection<JCDiagnostic>) GET_DIAGNOSTICS.invoke(handler);
482+
} catch (ReflectiveOperationException e) {
483+
throw new LinkageError(e.getMessage(), e);
484+
}
485+
}
486+
487+
private static final Method GET_DIAGNOSTICS;
488+
489+
static {
490+
try {
491+
GET_DIAGNOSTICS = DeferredDiagnosticHandler.class.getMethod("getDiagnostics");
492+
} catch (NoSuchMethodException e) {
493+
throw new LinkageError(e.getMessage(), e);
494+
}
495+
}
496+
468497
@SuppressWarnings("for-rollout:NullAway")
469498
private static int updateColumn(int columnI, String originalTokText) {
470499
Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));

0 commit comments

Comments
 (0)