From f0a9748d045d54f3ebb78511bafc358cea6feb31 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 4 Feb 2026 01:02:41 -0800 Subject: [PATCH] Update workaround for JDK-8372948 The upstream change is removing the overload of `newParser`. PiperOrigin-RevId: 865273867 --- .../google/googlejavaformat/java/Trees.java | 59 ++++++------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/Trees.java b/core/src/main/java/com/google/googlejavaformat/java/Trees.java index faca6ff86..ad8201bae 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Trees.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Trees.java @@ -179,13 +179,12 @@ public String getCharContent(boolean ignoreEncodingErrors) { JavacParser parser; try { parser = - (JavacParser) - NEW_PARSER_HANDLE.invokeExact( - parserFactory, - (CharSequence) javaInput, - /* keepDocComments */ true, - /* keepEndPos */ true, - /* keepLineMap */ true); + newParser( + parserFactory, + javaInput, + /* keepDocComments= */ true, + /* keepEndPos= */ true, + /* keepLineMap= */ true); } catch (Throwable e) { Throwables.throwIfUnchecked(e); throw new AssertionError(e); @@ -195,6 +194,19 @@ public String getCharContent(boolean ignoreEncodingErrors) { return unit; } + private static JavacParser newParser( + ParserFactory parserFactory, + CharSequence source, + boolean keepDocComments, + boolean keepEndPos, + boolean keepLineMap) { + if (END_POS_TABLE_CLASS != null) { + return parserFactory.newParser(source, keepDocComments, keepEndPos, keepLineMap); + } + return parserFactory.newParser( + source, keepDocComments, keepLineMap, /* parseModuleInfo */ false); + } + private static boolean errorDiagnostic(Diagnostic input) { if (input.getKind() != Diagnostic.Kind.ERROR) { return false; @@ -215,39 +227,6 @@ private static boolean errorDiagnostic(Diagnostic input) { } } - private static final MethodHandle NEW_PARSER_HANDLE = getNewParserHandle(); - - private static MethodHandle getNewParserHandle() { - MethodHandles.Lookup lookup = MethodHandles.lookup(); - if (END_POS_TABLE_CLASS == null) { - try { - // (parserFactory, input, keepDocComments, keepEndPos, keepLineMap) -> - // parserFactory.newParser(input, keepDocComments, keepLineMap) - return MethodHandles.dropArguments( - lookup.findVirtual( - ParserFactory.class, - "newParser", - MethodType.methodType( - JavacParser.class, CharSequence.class, boolean.class, boolean.class)), - 3, - boolean.class); - } catch (ReflectiveOperationException e) { - throw new LinkageError(e.getMessage(), e); - } - } - try { - // (parserFactory, input, keepDocComments, keepEndPos, keepLineMap) -> - // parserFactory.newParser(input, keepDocComments, keepEndPos, keepLineMap) - return lookup.findVirtual( - ParserFactory.class, - "newParser", - MethodType.methodType( - JavacParser.class, CharSequence.class, boolean.class, boolean.class, boolean.class)); - } catch (ReflectiveOperationException e) { - throw new LinkageError(e.getMessage(), e); - } - } - private static final MethodHandle GET_END_POS_HANDLE = getEndPosMethodHandle(); private static MethodHandle getEndPosMethodHandle() {