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
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,33 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (NEW_HASH_SET.matches(method)) {
maybeRemoveImport("com.google.common.collect.Sets");
maybeAddImport("java.util.HashSet");
if (method.getArguments().isEmpty() || method.getArguments().get(0) instanceof J.Empty) {
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());
}
if (method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
return JavaTemplate.builder("new HashSet<>(#{any(java.util.Collection)})")
.contextSensitive()
.imports("java.util.HashSet")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
if (method.getArguments().size() == 1) {
// Only handle if it's a Collection (not just any Iterable)
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
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));
}
// Skip Iterable-only cases to avoid generating broken code
if (TypeUtils.isAssignableTo("java.lang.Iterable", method.getArguments().get(0).getType())) {
return method;
}
}
maybeRemoveImport("com.google.common.collect.Sets");
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,78 @@ class Test {
)
);
}

@Test
void setsNewHashSetWithIterablesFilter() {
//language=java
rewriteRun(
java(
"""
import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

class Test {
void test() {
final List<ClassCastException> result = new ArrayList<ClassCastException>();
List<Exception> myExceptions = new ArrayList<Exception>();
result.addAll(Sets.newHashSet(Iterables.filter(myExceptions, ClassCastException.class)));
}
}
"""
)
);
}

@Test
void setsNewHashSetWithCustomIterable() {
//language=java
rewriteRun(
java(
"""
import com.google.common.collect.Sets;

class Test {
void test(Iterable<String> myIterable) {
var result = Sets.newHashSet(myIterable);
}
}
"""
)
);
}

@Test
void setsNewHashSetWithCollectionStillWorks() {
//language=java
rewriteRun(
java(
"""
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Set;

class Test {
public static void test(List<String> myList) {
Set<String> result = Sets.newHashSet(myList);
}
}
""",
"""
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Test {
public static void test(List<String> myList) {
Set<String> result = new HashSet<>(myList);
}
}
"""
)
);
}
}