Skip to content

Commit

Permalink
Prepare for a change to the return type of `JCCompilationUnit#getImpo…
Browse files Browse the repository at this point in the history
…rts` in an upcoming JDK version

PiperOrigin-RevId: 636582473
  • Loading branch information
cushon authored and Error Prone Team committed May 23, 2024
1 parent 13be411 commit 9e0fbf7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
package com.google.errorprone.apply;

import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCImport;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static ImportStatements create(

ImportStatements(
JCExpression packageTree,
List<JCImport> importTrees,
List<? extends JCTree> importTrees,
EndPosTable endPositions,
ImportOrganizer importOrganizer) {

Expand All @@ -80,7 +79,7 @@ public static ImportStatements create(
} else {
// process list of imports and find start/end positions
hasExistingImports = true;
for (JCImport importTree : importTrees) {
for (JCTree importTree : importTrees) {
int currStartPos = importTree.getStartPosition();
int currEndPos = importTree.getEndPosition(endPositions);

Expand All @@ -99,14 +98,9 @@ public static ImportStatements create(
importStrings.addAll(
Lists.transform(
importTrees,
new Function<JCImport, String>() {
@Override
public String apply(JCImport input) {
String importExpr = input.toString();
return CharMatcher.whitespace()
.or(CharMatcher.is(';'))
.trimTrailingFrom(importExpr);
}
input -> {
String importExpr = input.toString();
return CharMatcher.whitespace().or(CharMatcher.is(';')).trimTrailingFrom(importExpr);
}));

originalImports = ImmutableSet.copyOf(importStrings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ private static ImmutableSet<String> getAllImports(Inliner inliner, WhichImports
.map(JCCompilationUnit::getImports)
.map(Collection::stream)
.orElse(Stream.of())
.filter(JCImport.class::isInstance)
.map(JCImport.class::cast)
.filter(whichImports::existingImportMatches)
.map(imp -> getQualifiedIdentifier(imp).toString()))
.collect(toImmutableSet());
Expand Down

0 comments on commit 9e0fbf7

Please sign in to comment.