Skip to content

Commit 6f46e8a

Browse files
java-team-github-botgoogle-java-format Team
authored and
google-java-format Team
committed
Work around a JDK-head failure.
The return type here changed from `Queue` to `List`. PiperOrigin-RevId: 747457032
1 parent 8cf65c7 commit 6f46e8a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
import com.sun.tools.javac.parser.Tokens.TokenKind;
3838
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
3939
import com.sun.tools.javac.util.Context;
40+
import com.sun.tools.javac.util.JCDiagnostic;
4041
import com.sun.tools.javac.util.Log;
4142
import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
4243
import com.sun.tools.javac.util.Options;
4344
import java.io.IOException;
45+
import java.lang.reflect.Method;
4446
import java.net.URI;
4547
import java.util.ArrayList;
4648
import java.util.Collection;
@@ -53,6 +55,7 @@
5355
import javax.tools.JavaFileObject;
5456
import javax.tools.JavaFileObject.Kind;
5557
import javax.tools.SimpleJavaFileObject;
58+
import org.jspecify.annotations.Nullable;
5659

5760
/** {@code JavaInput} extends {@link Input} to represent a Java input document. */
5861
public final class JavaInput extends Input {
@@ -364,7 +367,15 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
364367
});
365368
DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log);
366369
ImmutableList<RawTok> rawToks = JavacTokens.getTokens(text, context, stopTokens);
367-
if (diagnostics.getDiagnostics().stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) {
370+
Collection<JCDiagnostic> ds;
371+
try {
372+
@SuppressWarnings("unchecked")
373+
var extraLocalForSuppression = (Collection<JCDiagnostic>) GET_DIAGNOSTICS.invoke(diagnostics);
374+
ds = extraLocalForSuppression;
375+
} catch (ReflectiveOperationException e) {
376+
throw new LinkageError(e.getMessage(), e);
377+
}
378+
if (ds.stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) {
368379
return ImmutableList.of(new Tok(0, "", "", 0, 0, true, null)); // EOF
369380
}
370381
int kN = 0;
@@ -471,6 +482,16 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
471482
return ImmutableList.copyOf(toks);
472483
}
473484

485+
private static final Method GET_DIAGNOSTICS = getGetDiagnostics();
486+
487+
private static @Nullable Method getGetDiagnostics() {
488+
try {
489+
return DeferredDiagnosticHandler.class.getMethod("getDiagnostics");
490+
} catch (NoSuchMethodException e) {
491+
throw new LinkageError(e.getMessage(), e);
492+
}
493+
}
494+
474495
private static int updateColumn(int columnI, String originalTokText) {
475496
Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
476497
if (last > 0) {

0 commit comments

Comments
 (0)