Skip to content

Commit e969a22

Browse files
authored
Added support for generic instanceof checks (#734)
1 parent 1a25e21 commit e969a22

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/main/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOf.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
9595

9696

9797
JavaTemplate template = JavaTemplate
98-
.builder("assertInstanceOf(#{}.class, #{any(java.lang.Object)}" + (reason != null ? ", #{any(java.lang.String)})" : ")"))
98+
.builder("assertInstanceOf(#{any(java.lang.Object)}.class, #{any(java.lang.Object)}" + (reason != null ? ", #{any(java.lang.String)})" : ")"))
9999
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5", "junit-4"))
100100
.staticImports("org.junit.jupiter.api.Assertions.assertInstanceOf")
101101
.imports(String.valueOf(clazz.getType()))
102102
.build();
103103

104+
J rawClazz = clazz instanceof J.ParameterizedType ? ((J.ParameterizedType) clazz).getClazz() : clazz;
104105
J.MethodInvocation methodd = reason != null ?
105-
template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression, reason) :
106-
template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression);
106+
template.apply(getCursor(), mi.getCoordinates().replace(), rawClazz, expression, reason) :
107+
template.apply(getCursor(), mi.getCoordinates().replace(), rawClazz, expression);
107108
maybeAddImport("org.junit.jupiter.api.Assertions", "assertInstanceOf");
108109
return methodd;
109110
}

src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,42 @@ void testJUnit5() {
185185
"""
186186
));
187187
}
188+
189+
@Test
190+
void jUnit4GenericInstanceOf() {
191+
//language=java
192+
rewriteRun(
193+
java(
194+
"""
195+
import org.junit.jupiter.api.Test;
196+
import java.util.ArrayList;
197+
import java.util.List;
198+
199+
import static org.junit.Assert.assertTrue;
200+
201+
class ATest {
202+
@Test
203+
void testJUnit5() {
204+
List<String> list = new ArrayList<>();
205+
assertTrue(list instanceof List<?>);
206+
}
207+
}
208+
""",
209+
"""
210+
import org.junit.jupiter.api.Test;
211+
import java.util.ArrayList;
212+
import java.util.List;
213+
214+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
215+
216+
class ATest {
217+
@Test
218+
void testJUnit5() {
219+
List<String> list = new ArrayList<>();
220+
assertInstanceOf(List.class, list);
221+
}
222+
}
223+
"""
224+
));
225+
}
188226
}

0 commit comments

Comments
 (0)