Skip to content

Commit 8cd830c

Browse files
authored
Ignore java.util.Iterator in NoGuavaSetsNewHashSet recipe (#921)
Before this commit, `java.lang.Iterable` was ignored to avoid generating broken code. This commit adds `java.util.Iterator` to the ignore list for the same reason. A corresponding test has also been added.
1 parent 46d115e commit 8cd830c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
7575
.build()
7676
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
7777
}
78-
// Skip Iterable-only cases to avoid generating broken code
79-
if (TypeUtils.isAssignableTo("java.lang.Iterable", method.getArguments().get(0).getType())) {
78+
// Skip Iterable-only and Iterator-only cases to avoid generating broken code
79+
if (TypeUtils.isAssignableTo("java.lang.Iterable", method.getArguments().get(0).getType()) || TypeUtils.isAssignableTo("java.util.Iterator", method.getArguments().get(0).getType())) {
8080
return method;
8181
}
8282
}

src/test/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSetTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ void test() {
143143
);
144144
}
145145

146+
@Test
147+
void setsNewHashSetWithIteratorsFilter() {
148+
//language=java
149+
rewriteRun(
150+
java(
151+
"""
152+
import java.util.Collection;
153+
import java.util.Iterator;
154+
155+
import com.google.common.collect.Iterators;
156+
import com.google.common.collect.Sets;
157+
158+
class Test {
159+
public Collection<String> collectExistingRepresentations(Iterator<Object> iterator) {
160+
return Sets.newHashSet(Iterators.filter(iterator, String.class));
161+
}
162+
}
163+
"""
164+
)
165+
);
166+
}
167+
146168
@Test
147169
void setsNewHashSetWithCustomIterable() {
148170
//language=java

0 commit comments

Comments
 (0)