Skip to content

Commit 37fb56a

Browse files
committed
Update TemporaryFolderToTempDir to use TypeUtils for checking method type vs comparing fully qualified name strings. (Issue #242)
1 parent 72a0d17 commit 37fb56a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collections;
2929
import java.util.List;
30+
import java.util.Objects;
3031
import java.util.function.Supplier;
3132
import java.util.stream.Collectors;
3233
import java.util.stream.Stream;
@@ -35,8 +36,8 @@ public class TemporaryFolderToTempDir extends Recipe {
3536
private static final AnnotationMatcher CLASS_RULE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.ClassRule");
3637
private static final AnnotationMatcher RULE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.Rule");
3738

38-
private static final JavaType.Class FILE_TYPE = JavaType.Class.build("java.io.File");
39-
private static final JavaType.Class STRING_TYPE = JavaType.Class.build("java.lang.String");
39+
private static final JavaType.Class FILE_TYPE = JavaType.ShallowClass.build("java.io.File");
40+
private static final JavaType.Class STRING_TYPE = JavaType.ShallowClass.build("java.lang.String");
4041
private static final Supplier<JavaParser> TEMPDIR_PARSER = () ->
4142
JavaParser.fromJavaVersion().dependsOn(Collections.singletonList(
4243
Parser.Input.fromString("" +
@@ -109,8 +110,8 @@ private boolean isRuleAnnotatedTemporaryFolder(J.VariableDeclarations vd) {
109110
@Override
110111
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
111112
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);
112-
String declaringType = mi.getMethodType() != null ? mi.getMethodType().getDeclaringType().getFullyQualifiedName() : null;
113-
if ("org.junit.rules.TemporaryFolder".equals(declaringType) && mi.getSelect() != null) {
113+
if (mi.getSelect() != null && mi.getMethodType() != null
114+
&& TypeUtils.isOfClassType(mi.getMethodType().getDeclaringType(), "org.junit.rules.TemporaryFolder")) {
114115
switch (mi.getSimpleName()) {
115116
case "newFile":
116117
return convertToNewFile(mi);
@@ -167,7 +168,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
167168
&& params.size() == 2
168169
&& params.get(0).hasClassType(FILE_TYPE)
169170
&& params.get(1).hasClassType(STRING_TYPE);
170-
}).map(J.MethodDeclaration::getMethodType).findAny().orElse(null);
171+
}).map(J.MethodDeclaration::getMethodType).filter(Objects::nonNull).findAny().orElse(null);
171172

172173
if (newFolderMethodDeclaration == null) {
173174
cd = cd.withTemplate(JavaTemplate.builder(this::getCursor,

0 commit comments

Comments
 (0)