Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies {
runtimeOnly("tech.picnic.error-prone-support:error-prone-contrib:latest.release:recipes")
testImplementation("org.junit-pioneer:junit-pioneer:2.0.0")

testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.openrewrite:rewrite-test")
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.ArrayList");
return JavaTemplate.builder("new ArrayList<>()")
.contextSensitive()
.imports("java.util.ArrayList")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -67,7 +66,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.ArrayList");
return JavaTemplate.builder("new ArrayList<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.ArrayList")
.build()
.apply(getCursor(), method.getCoordinates().replace(),
Expand All @@ -77,7 +75,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.ArrayList");
return JavaTemplate.builder("new ArrayList<>(#{any(int)})")
.contextSensitive()
.imports("java.util.ArrayList")
.build()
.apply(getCursor(), method.getCoordinates().replace(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.concurrent.CopyOnWriteArrayList");
return JavaTemplate.builder("new CopyOnWriteArrayList<>()")
.contextSensitive()
.imports("java.util.concurrent.CopyOnWriteArrayList")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -65,7 +64,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.concurrent.CopyOnWriteArrayList");
return JavaTemplate.builder("new CopyOnWriteArrayList<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.concurrent.CopyOnWriteArrayList")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.LinkedList");
return JavaTemplate.builder("new LinkedList<>()")
.contextSensitive()
.imports("java.util.LinkedList")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -65,7 +64,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Lists");
maybeAddImport("java.util.LinkedList");
return JavaTemplate.builder("new LinkedList<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.LinkedList")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.HashMap");
return JavaTemplate.builder("new HashMap<>()")
.contextSensitive()
.imports("java.util.HashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -63,7 +62,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.HashMap");
return JavaTemplate.builder("new HashMap<>(#{any(java.util.Map)})")
.contextSensitive()
.imports("java.util.HashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.LinkedHashMap");
return JavaTemplate.builder("new LinkedHashMap<>()")
.contextSensitive()
.imports("java.util.LinkedHashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -63,7 +62,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.LinkedHashMap");
return JavaTemplate.builder("new LinkedHashMap<>(#{any(java.util.Map)})")
.contextSensitive()
.imports("java.util.LinkedHashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.TreeMap");
return JavaTemplate.builder("new TreeMap<>()")
.contextSensitive()
.imports("java.util.TreeMap")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -65,7 +64,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.TreeMap");
return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Comparator)})")
.contextSensitive()
.imports("java.util.TreeMap")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand All @@ -74,7 +72,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.TreeMap");
return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Map)})")
.contextSensitive()
.imports("java.util.TreeMap")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeAddImport("java.util.Collections");
maybeAddImport("java.util.concurrent.ConcurrentHashMap");
return JavaTemplate.builder("Collections.newSetFromMap(new ConcurrentHashMap<>())")
.contextSensitive()
.imports("java.util.Collections")
.imports("java.util.concurrent.ConcurrentHashMap")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.HashSet");
return JavaTemplate.builder("new HashSet<>()")
.contextSensitive()
.imports("java.util.HashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -65,7 +64,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.HashSet");
return JavaTemplate.builder("new HashSet<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.HashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand All @@ -79,7 +77,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeAddImport("java.util.HashSet");
maybeAddImport("java.util.Arrays");
JavaTemplate newHashSetVarargs = JavaTemplate.builder("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(joining(",")) + "))")
.contextSensitive()
.imports("java.util.Arrays")
.imports("java.util.HashSet")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.LinkedHashSet");
return JavaTemplate.builder("new LinkedHashSet<>()")
.contextSensitive()
.imports("java.util.LinkedHashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace());
Expand All @@ -68,7 +67,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.LinkedHashSet");
return JavaTemplate.builder("new LinkedHashSet<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.LinkedHashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand All @@ -77,7 +75,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.LinkedHashSet");
return JavaTemplate.builder("new LinkedHashSet<>(#{any(int)})")
.contextSensitive()
.imports("java.util.LinkedHashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.java.Assertions.java;


Expand All @@ -31,7 +33,8 @@ class NoGuavaMapsNewHashMapTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec
.recipe(new NoGuavaMapsNewHashMap())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"));
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava"))
.parser(GroovyParser.builder().classpathFromResource(new InMemoryExecutionContext(), "guava"));
}

@DocumentExample
Expand Down Expand Up @@ -88,4 +91,58 @@ class Test {
)
);
}

@Test
void replaceWithNewHashMapWithMapInGroovy() {
//language=groovy
rewriteRun(
groovy(
"""
import com.google.common.collect.Lists
import com.google.common.collect.Maps

class Test extends Specification {
def 'test get_privilege_set'() {
def registry = Mock(Registry)

when:
def result = ms.get_privilege_set(null, null, null)

then:
result == new PrincipalPrivilegeSet(null
, null
, Maps.newHashMap(Map.of("users",
Lists.newArrayList(new PrivilegeGrantInfo("ALL", 0, "hadoop", PrincipalType.ROLE, true)))))
registry.timer(_) >> timer
timer.record(_, _) >> {}
registry.createId(_ as String) >> id
}
}
""",
"""
import com.google.common.collect.Lists

import java.util.HashMap

class Test extends Specification {
def 'test get_privilege_set'() {
def registry = Mock(Registry)

when:
def result = ms.get_privilege_set(null, null, null)

then:
result == new PrincipalPrivilegeSet(null
, null
, new HashMap<>(Map.of("users",
Lists.newArrayList(new PrivilegeGrantInfo("ALL", 0, "hadoop", PrincipalType.ROLE, true)))))
registry.timer(_) >> timer
timer.record(_, _) >> {}
registry.createId(_ as String) >> id
}
}
"""
)
);
}
}
Loading