From a94279cf624b035060fddbe4c2a5bf3e2d9de616 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 24 Jul 2024 13:51:15 -0700 Subject: [PATCH] Improve fix to prepare for a change to the return type of `JCCompilationUnit#getImports` in an upcoming JDK version Follow-up to https://github.com/google/error-prone/commit/9e0fbf705dc98faf2a8ac88cbdb1facc0ba18696 This allows the code to work if it's compiled and executed against different javac versions. PiperOrigin-RevId: 655688755 --- .../com/google/errorprone/refaster/ImportPolicy.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/refaster/ImportPolicy.java b/core/src/main/java/com/google/errorprone/refaster/ImportPolicy.java index c351d02288a..1576928a56d 100644 --- a/core/src/main/java/com/google/errorprone/refaster/ImportPolicy.java +++ b/core/src/main/java/com/google/errorprone/refaster/ImportPolicy.java @@ -266,7 +266,7 @@ private static ImmutableSet getAllImports(Inliner inliner, WhichImports whichImports.getExistingImports(inliner), Optional.ofNullable(inliner.getContext()) .map(c -> c.get(JCCompilationUnit.class)) - .map(JCCompilationUnit::getImports) + .map(ImportPolicy::getImports) .map(Collection::stream) .orElse(Stream.of()) .filter(JCImport.class::isInstance) @@ -276,6 +276,15 @@ private static ImmutableSet getAllImports(Inliner inliner, WhichImports .collect(toImmutableSet()); } + @SuppressWarnings("unchecked") + private static Collection getImports(JCCompilationUnit unit) { + try { + return (Collection) JCCompilationUnit.class.getMethod("getImports").invoke(unit); + } catch (ReflectiveOperationException e) { + throw new LinkageError(e.getMessage(), e); + } + } + private static JCTree getQualifiedIdentifier(JCImport i) { try { return (JCTree) JCImport.class.getMethod("getQualifiedIdentifier").invoke(i);