Skip to content

Commit

Permalink
Improve fix to prepare for a change to the return type of `JCCompilat…
Browse files Browse the repository at this point in the history
…ionUnit#getImports` in an upcoming JDK version

Follow-up to 9e0fbf7

This allows the code to work if it's compiled and executed against different javac versions.

PiperOrigin-RevId: 655688755
  • Loading branch information
cushon authored and Error Prone Team committed Jul 24, 2024
1 parent fc339e0 commit a94279c
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static ImmutableSet<String> 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)
Expand All @@ -276,6 +276,15 @@ private static ImmutableSet<String> getAllImports(Inliner inliner, WhichImports
.collect(toImmutableSet());
}

@SuppressWarnings("unchecked")
private static Collection<JCTree> getImports(JCCompilationUnit unit) {
try {
return (Collection<JCTree>) 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);
Expand Down

0 comments on commit a94279c

Please sign in to comment.