|
39 | 39 | import com.sun.tools.javac.parser.Tokens.TokenKind; |
40 | 40 | import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
41 | 41 | import com.sun.tools.javac.util.Context; |
| 42 | +import com.sun.tools.javac.util.JCDiagnostic; |
42 | 43 | import com.sun.tools.javac.util.Log; |
43 | 44 | import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler; |
44 | 45 | import com.sun.tools.javac.util.Options; |
45 | 46 | import java.io.IOException; |
| 47 | +import java.lang.reflect.Method; |
46 | 48 | import java.net.URI; |
47 | 49 | import java.util.ArrayList; |
48 | 50 | import java.util.Collection; |
@@ -368,7 +370,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept |
368 | 370 | }); |
369 | 371 | DeferredDiagnosticHandler diagnostics = new DeferredDiagnosticHandler(log); |
370 | 372 | 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)) { |
372 | 374 | return ImmutableList.of(new Tok(0, "", "", 0, 0, true, null)); // EOF |
373 | 375 | } |
374 | 376 | int kN = 0; |
@@ -465,6 +467,33 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept |
465 | 467 | return ImmutableList.copyOf(toks); |
466 | 468 | } |
467 | 469 |
|
| 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 | + |
468 | 497 | @SuppressWarnings("for-rollout:NullAway") |
469 | 498 | private static int updateColumn(int columnI, String originalTokText) { |
470 | 499 | Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText)); |
|
0 commit comments