Skip to content

Commit

Permalink
Stop over eager exclude of UseStaticImport (#3734)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 24, 2023
1 parent 155d533 commit 335dddb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ private int emptyList(String canHaveDifferentArguments) {
);
}

@Test
void doReplaceWhenWildcard() {
rewriteRun(
spec -> spec.recipe(new UseStaticImport("java.util.Collections *()")),
java(
"""
import java.util.Collections;
import java.util.List;
class SameMethodNameLocally {
void avoidCollision() {
List<Object> list = Collections.emptyList();
}
}
""",
"""
import java.util.List;
import static java.util.Collections.emptyList;
class SameMethodNameLocally {
void avoidCollision() {
List<Object> list = emptyList();
}
}
"""
)
);
}

@Test
void methodInvocationsHavingNullSelect() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
int indexSpace = methodPattern.indexOf(' ');
int indexBrace = methodPattern.indexOf('(', indexSpace);
String identicalMethodName = "* " + methodPattern.substring(indexSpace, indexBrace) + "(..)";
return Preconditions.check(Preconditions.and(new UsesMethod<>(methodPattern), Preconditions.not(new DeclaresMethod<>(identicalMethodName))), new UseStaticImportVisitor());
TreeVisitor<?, ExecutionContext> preconditions = new UsesMethod<>(methodPattern);
if (!methodPattern.contains(" *(")) {
int indexSpace = methodPattern.indexOf(' ');
int indexBrace = methodPattern.indexOf('(', indexSpace);
String methodNameMatcher = methodPattern.substring(indexSpace, indexBrace);
preconditions = Preconditions.and(preconditions,
Preconditions.not(new DeclaresMethod<>("* " + methodNameMatcher + "(..)")));
}
return Preconditions.check(preconditions, new UseStaticImportVisitor());
}

private class UseStaticImportVisitor extends JavaIsoVisitor<ExecutionContext> {
Expand Down

0 comments on commit 335dddb

Please sign in to comment.