Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog/@unreleased/pr-1367.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,7 +370,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
});
DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log);
ImmutableList<RawTok> 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;
Expand Down Expand Up @@ -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<JCDiagnostic> getDiagnostics(DeferredDiagnosticHandler handler) {
try {
return (Collection<JCDiagnostic>) 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));
Expand Down