|
28 | 28 |
|
29 | 29 | import java.util.*;
|
30 | 30 |
|
31 |
| -import static org.openrewrite.Tree.randomId; |
32 |
| - |
33 | 31 | public class ReplaceDeprecatedEnvironmentTestUtils extends Recipe {
|
34 | 32 |
|
35 | 33 | 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 {
|
66 | 64 | private ReplaceEnvironmentUtilsMarker(String templateString, List<Expression> parameters, UUID id) {
|
67 | 65 | this.templateString = templateString;
|
68 | 66 | this.parameters = parameters;
|
69 |
| - this.id = UUID.randomUUID(); |
70 |
| - } |
71 |
| - |
72 |
| - public String getDescription() { |
73 |
| - return templateString; |
| 67 | + this.id = id; |
74 | 68 | }
|
75 | 69 |
|
76 | 70 | @Override
|
77 | 71 | public UUID getId() {
|
78 | 72 | return id;
|
79 | 73 | }
|
80 | 74 |
|
| 75 | + @SuppressWarnings("unchecked") |
81 | 76 | @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); |
84 | 79 | }
|
85 | 80 | }
|
86 | 81 |
|
@@ -150,19 +145,21 @@ private boolean isCollectedContextOrEnvironment(List<J.MethodInvocation> collect
|
150 | 145 | Expression environmentNameToCheck = getEnvironmentNameArgument(methodInvocation);
|
151 | 146 | Expression collectedEnvironmentName = getEnvironmentNameArgument(collectedMethod);
|
152 | 147 |
|
153 |
| - return SemanticallyEqual.areEqual(contextOrEnvironmentToCheck, collectedContextOrEnvironment) |
| 148 | + return !(contextOrEnvironmentToCheck instanceof J.NewClass) && |
| 149 | + SemanticallyEqual.areEqual(contextOrEnvironmentToCheck, collectedContextOrEnvironment) |
154 | 150 | && (environmentNameToCheck == null && collectedEnvironmentName == null)
|
155 | 151 | || (environmentNameToCheck != null && collectedEnvironmentName != null
|
156 | 152 | && SemanticallyEqual.areEqual(environmentNameToCheck, collectedEnvironmentName));
|
157 | 153 | }
|
158 | 154 |
|
| 155 | + @Nullable |
159 | 156 | private Expression getEnvironmentNameArgument(J.MethodInvocation methodInvocation) {
|
160 | 157 | if (methodInvocation.getArguments().size() < MINIMUM_ARGUMENT_COUNT_WITH_NAME) {
|
161 | 158 | return null;
|
162 | 159 | }
|
163 | 160 | Expression firstArgument = methodInvocation.getArguments().get(0);
|
164 | 161 |
|
165 |
| - if (firstArgument.getType().equals(JavaType.Primitive.String)) { |
| 162 | + if (firstArgument.getType() != null && firstArgument.getType().equals(JavaType.Primitive.String)) { |
166 | 163 | return firstArgument;
|
167 | 164 | }
|
168 | 165 | return null;
|
|
0 commit comments