Skip to content

Commit 4e7cfb2

Browse files
committed
Fix ReplaceDeprecatedEnvironmentTestUtils coallescing fluent calls when the target is a constructor
1 parent 26f5dbd commit 4e7cfb2

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/main/java/org/openrewrite/java/spring/boot2/ReplaceDeprecatedEnvironmentTestUtils.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
import java.util.*;
3030

31-
import static org.openrewrite.Tree.randomId;
32-
3331
public class ReplaceDeprecatedEnvironmentTestUtils extends Recipe {
3432

3533
private static final MethodMatcher APP_CONTEXT = new MethodMatcher("org.springframework.boot.test.util.EnvironmentTestUtils addEnvironment(org.springframework.context.ConfigurableApplicationContext, String...)");
@@ -66,21 +64,18 @@ private static final class ReplaceEnvironmentUtilsMarker implements Marker {
6664
private ReplaceEnvironmentUtilsMarker(String templateString, List<Expression> parameters, UUID id) {
6765
this.templateString = templateString;
6866
this.parameters = parameters;
69-
this.id = UUID.randomUUID();
70-
}
71-
72-
public String getDescription() {
73-
return templateString;
67+
this.id = id;
7468
}
7569

7670
@Override
7771
public UUID getId() {
7872
return id;
7973
}
8074

75+
@SuppressWarnings("unchecked")
8176
@Override
82-
public <T extends Tree> T withId(UUID id) {
83-
return (T) new ReplaceEnvironmentUtilsMarker(templateString, parameters, id);
77+
public ReplaceEnvironmentUtilsMarker withId(UUID id) {
78+
return new ReplaceEnvironmentUtilsMarker(templateString, parameters, id);
8479
}
8580
}
8681

@@ -150,19 +145,21 @@ private boolean isCollectedContextOrEnvironment(List<J.MethodInvocation> collect
150145
Expression environmentNameToCheck = getEnvironmentNameArgument(methodInvocation);
151146
Expression collectedEnvironmentName = getEnvironmentNameArgument(collectedMethod);
152147

153-
return SemanticallyEqual.areEqual(contextOrEnvironmentToCheck, collectedContextOrEnvironment)
148+
return !(contextOrEnvironmentToCheck instanceof J.NewClass) &&
149+
SemanticallyEqual.areEqual(contextOrEnvironmentToCheck, collectedContextOrEnvironment)
154150
&& (environmentNameToCheck == null && collectedEnvironmentName == null)
155151
|| (environmentNameToCheck != null && collectedEnvironmentName != null
156152
&& SemanticallyEqual.areEqual(environmentNameToCheck, collectedEnvironmentName));
157153
}
158154

155+
@Nullable
159156
private Expression getEnvironmentNameArgument(J.MethodInvocation methodInvocation) {
160157
if (methodInvocation.getArguments().size() < MINIMUM_ARGUMENT_COUNT_WITH_NAME) {
161158
return null;
162159
}
163160
Expression firstArgument = methodInvocation.getArguments().get(0);
164161

165-
if (firstArgument.getType().equals(JavaType.Primitive.String)) {
162+
if (firstArgument.getType() != null && firstArgument.getType().equals(JavaType.Primitive.String)) {
166163
return firstArgument;
167164
}
168165
return null;

0 commit comments

Comments
 (0)