Skip to content
Closed
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -366,7 +369,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 @@ -463,6 +466,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 {
// Try the JDK 25+ method first (List<JCDiagnostic>)
Method listMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics");
return (Collection<JCDiagnostic>) listMethod.invoke(handler);
} catch (Exception e1) {
try {
// Fall back to pre-JDK 25 method (Queue<JCDiagnostic>)
Method queueMethod = DeferredDiagnosticHandler.class.getMethod("getDiagnostics");
return (Collection<JCDiagnostic>) queueMethod.invoke(handler);
} catch (Exception e2) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this particular change does anything? There is no difference between this and lines 480-482. It makes more sense to do the same/equivalent change as google-java-format: https://github.com/google/google-java-format/pull/1247/files.

Copy link
Contributor Author

@gnodet gnodet Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixdesouza You're right, I removed the second fallback which is completely useless.
I also cached the method handle in a static final field to get closer to google/google-java-format#1247. Just cherry-picking their commit could work too I suppose.

// If all else fails, return empty collection
System.err.println("Unable to access diagnostics: " + e2.getMessage());
return Collections.emptyList();
}
}
}

@SuppressWarnings("for-rollout:NullAway")
private static int updateColumn(int columnI, String originalTokText) {
Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
Expand Down