Skip to content

Commit 6d7ba9f

Browse files
authored
Use recipes moved to openrewrite/rewrite (#662)
* Use recipes moved to openrewrite/rewrite * Update references to moved recipes * Also move `UpdateMavenProjectPropertyJavaVersion` * Clean up imports
1 parent e0a9a98 commit 6d7ba9f

10 files changed

+96
-299
lines changed

src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java

+9-68
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20-
import org.openrewrite.ExecutionContext;
2120
import org.openrewrite.Option;
2221
import org.openrewrite.Recipe;
23-
import org.openrewrite.TreeVisitor;
24-
import org.openrewrite.internal.ListUtils;
25-
import org.openrewrite.java.JavaIsoVisitor;
26-
import org.openrewrite.java.MethodMatcher;
27-
import org.openrewrite.java.tree.J;
28-
import org.openrewrite.java.tree.JavaType;
29-
import org.openrewrite.java.tree.TypeUtils;
30-
import org.openrewrite.marker.Markers;
3122

32-
import static java.util.Collections.emptyList;
23+
import java.util.List;
3324

25+
import static java.util.Collections.singletonList;
26+
27+
/**
28+
* @deprecated in favor of {@link org.openrewrite.java.ChangeMethodInvocationReturnType}.
29+
*/
3430
@Value
3531
@EqualsAndHashCode(callSuper = false)
32+
@Deprecated
3633
public class ChangeMethodInvocationReturnType extends Recipe {
3734

3835
@Option(displayName = "Method pattern",
@@ -56,63 +53,7 @@ public String getDescription() {
5653
}
5754

5855
@Override
59-
public TreeVisitor<?, ExecutionContext> getVisitor() {
60-
return new JavaIsoVisitor<ExecutionContext>() {
61-
private final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, false);
62-
63-
private boolean methodUpdated;
64-
65-
@Override
66-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
67-
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
68-
JavaType.Method type = m.getMethodType();
69-
if (methodMatcher.matches(method) && type != null && !newReturnType.equals(type.getReturnType().toString())) {
70-
type = type.withReturnType(JavaType.buildType(newReturnType));
71-
m = m.withMethodType(type);
72-
if (m.getName().getType() != null) {
73-
m = m.withName(m.getName().withType(type));
74-
}
75-
methodUpdated = true;
76-
}
77-
return m;
78-
}
79-
80-
@Override
81-
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
82-
methodUpdated = false;
83-
JavaType.FullyQualified originalType = multiVariable.getTypeAsFullyQualified();
84-
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);
85-
86-
if (methodUpdated) {
87-
JavaType newType = JavaType.buildType(newReturnType);
88-
JavaType.FullyQualified newFieldType = TypeUtils.asFullyQualified(newType);
89-
90-
maybeAddImport(newFieldType);
91-
maybeRemoveImport(originalType);
92-
93-
mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
94-
null :
95-
new J.Identifier(mv.getTypeExpression().getId(),
96-
mv.getTypeExpression().getPrefix(),
97-
Markers.EMPTY,
98-
emptyList(),
99-
newReturnType.substring(newReturnType.lastIndexOf('.') + 1),
100-
newType,
101-
null
102-
)
103-
);
104-
105-
mv = mv.withVariables(ListUtils.map(mv.getVariables(), var -> {
106-
JavaType.FullyQualified varType = TypeUtils.asFullyQualified(var.getType());
107-
if (varType != null && !varType.equals(newType)) {
108-
return var.withType(newType).withName(var.getName().withType(newType));
109-
}
110-
return var;
111-
}));
112-
}
113-
114-
return mv;
115-
}
116-
};
56+
public List<Recipe> getRecipeList() {
57+
return singletonList(new org.openrewrite.java.ChangeMethodInvocationReturnType(methodPattern, newReturnType));
11758
}
11859
}

src/main/java/org/openrewrite/java/migrate/ReplaceStringLiteralValue.java

+10-19
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020
import lombok.EqualsAndHashCode;
2121
import lombok.Value;
2222
import org.jspecify.annotations.NonNull;
23-
import org.openrewrite.ExecutionContext;
2423
import org.openrewrite.Option;
2524
import org.openrewrite.Recipe;
26-
import org.openrewrite.TreeVisitor;
27-
import org.openrewrite.java.JavaIsoVisitor;
28-
import org.openrewrite.java.tree.J;
29-
import org.openrewrite.java.tree.JavaType;
3025

26+
import java.util.List;
27+
28+
import static java.util.Collections.singletonList;
29+
30+
/**
31+
* @deprecated in favor of {@link org.openrewrite.java.ReplaceStringLiteralValue}.
32+
*/
3133
@Value
3234
@EqualsAndHashCode(callSuper = false)
35+
@Deprecated
3336
public class ReplaceStringLiteralValue extends Recipe {
3437

3538
@Option(displayName = "Old literal `String` value",
@@ -61,19 +64,7 @@ public String getDescription() {
6164
}
6265

6366
@Override
64-
public TreeVisitor<?, ExecutionContext> getVisitor() {
65-
return new JavaIsoVisitor<ExecutionContext>() {
66-
@Override
67-
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
68-
J.Literal l = super.visitLiteral(literal, ctx);
69-
if (l.getType() != JavaType.Primitive.String || !oldLiteralValue.equals(literal.getValue())) {
70-
return l;
71-
}
72-
return literal
73-
.withValue(newLiteralValue)
74-
.withValueSource('"' + newLiteralValue + '"');
75-
}
76-
};
67+
public List<Recipe> getRecipeList() {
68+
return singletonList(new org.openrewrite.java.ReplaceStringLiteralValue(oldLiteralValue, newLiteralValue));
7769
}
78-
7970
}

src/main/java/org/openrewrite/java/migrate/UpgradeJavaVersion.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.openrewrite.gradle.UpdateJavaCompatibility;
2525
import org.openrewrite.java.JavaIsoVisitor;
2626
import org.openrewrite.java.marker.JavaVersion;
27-
import org.openrewrite.java.migrate.maven.UpdateMavenProjectPropertyJavaVersion;
28-
import org.openrewrite.java.migrate.maven.UseMavenCompilerPluginReleaseConfiguration;
2927
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.maven.UpdateMavenProjectPropertyJavaVersion;
29+
import org.openrewrite.maven.UseMavenCompilerPluginReleaseConfiguration;
3030

3131
import java.time.Duration;
3232
import java.util.*;
@@ -48,9 +48,9 @@ public String getDisplayName() {
4848
@Override
4949
public String getDescription() {
5050
return "Upgrade build plugin configuration to use the specified Java version. " +
51-
"This recipe changes `java.toolchain.languageVersion` in `build.gradle(.kts)` of gradle projects, " +
52-
"or maven-compiler-plugin target version and related settings. " +
53-
"Will not downgrade if the version is newer than the specified version.";
51+
"This recipe changes `java.toolchain.languageVersion` in `build.gradle(.kts)` of gradle projects, " +
52+
"or maven-compiler-plugin target version and related settings. " +
53+
"Will not downgrade if the version is newer than the specified version.";
5454
}
5555

5656
@Override

src/main/java/org/openrewrite/java/migrate/maven/UpdateMavenProjectPropertyJavaVersion.java

+17-97
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,21 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20-
import org.openrewrite.ExecutionContext;
2120
import org.openrewrite.Option;
2221
import org.openrewrite.Recipe;
23-
import org.openrewrite.TreeVisitor;
24-
import org.openrewrite.maven.AddProperty;
25-
import org.openrewrite.maven.MavenIsoVisitor;
26-
import org.openrewrite.xml.XPathMatcher;
27-
import org.openrewrite.xml.tree.Xml;
2822

29-
import java.util.Arrays;
3023
import java.util.List;
31-
import java.util.Map;
32-
import java.util.stream.Collectors;
3324

25+
import static java.util.Collections.singletonList;
26+
27+
/**
28+
* @deprecated in favor of {@link org.openrewrite.maven.UpdateMavenProjectPropertyJavaVersion}
29+
*/
3430
@Value
3531
@EqualsAndHashCode(callSuper = false)
32+
@Deprecated
3633
public class UpdateMavenProjectPropertyJavaVersion extends Recipe {
3734

38-
private static final List<String> JAVA_VERSION_PROPERTIES = Arrays.asList(
39-
"java.version",
40-
"jdk.version",
41-
"javaVersion",
42-
"jdkVersion",
43-
"maven.compiler.source",
44-
"maven.compiler.target",
45-
"maven.compiler.release",
46-
"release.version");
47-
48-
private static final List<XPathMatcher> JAVA_VERSION_XPATH_MATCHERS =
49-
JAVA_VERSION_PROPERTIES.stream()
50-
.map(property -> "/project/properties/" + property)
51-
.map(XPathMatcher::new).collect(Collectors.toList());
52-
53-
private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher("/project/build//plugins");
54-
5535
@Option(displayName = "Java version",
5636
description = "The Java version to upgrade to.",
5737
example = "11")
@@ -66,79 +46,19 @@ public String getDisplayName() {
6646
public String getDescription() {
6747
//language=markdown
6848
return "The Java version is determined by several project properties, including:\n\n" +
69-
" * `java.version`\n" +
70-
" * `jdk.version`\n" +
71-
" * `javaVersion`\n" +
72-
" * `jdkVersion`\n" +
73-
" * `maven.compiler.source`\n" +
74-
" * `maven.compiler.target`\n" +
75-
" * `maven.compiler.release`\n" +
76-
" * `release.version`\n\n" +
77-
"If none of these properties are in use and the maven compiler plugin is not otherwise configured, adds the `maven.compiler.release` property.";
49+
" * `java.version`\n" +
50+
" * `jdk.version`\n" +
51+
" * `javaVersion`\n" +
52+
" * `jdkVersion`\n" +
53+
" * `maven.compiler.source`\n" +
54+
" * `maven.compiler.target`\n" +
55+
" * `maven.compiler.release`\n" +
56+
" * `release.version`\n\n" +
57+
"If none of these properties are in use and the maven compiler plugin is not otherwise configured, adds the `maven.compiler.release` property.";
7858
}
7959

8060
@Override
81-
public TreeVisitor<?, ExecutionContext> getVisitor() {
82-
return new MavenIsoVisitor<ExecutionContext>() {
83-
boolean compilerPluginConfiguredExplicitly;
84-
85-
@Override
86-
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
87-
// Update properties already defined in the current pom
88-
Xml.Document d = super.visitDocument(document, ctx);
89-
90-
// Return early if the parent appears to be within the current repository, as properties defined there will be updated
91-
if (getResolutionResult().parentPomIsProjectPom()) {
92-
return d;
93-
}
94-
95-
// Otherwise override remote parent's properties locally
96-
Map<String, String> currentProperties = getResolutionResult().getPom().getProperties();
97-
boolean foundProperty = false;
98-
for (String property : JAVA_VERSION_PROPERTIES) {
99-
String propertyValue = currentProperties.get(property);
100-
if (propertyValue != null) {
101-
foundProperty = true;
102-
try {
103-
if (Float.parseFloat(propertyValue) < version) {
104-
d = (Xml.Document) new AddProperty(property, String.valueOf(version), null, false)
105-
.getVisitor()
106-
.visitNonNull(d, ctx);
107-
maybeUpdateModel();
108-
}
109-
} catch (NumberFormatException ex) {
110-
// either an expression or something else, don't touch
111-
}
112-
}
113-
}
114-
115-
// When none of the relevant properties are explicitly configured Maven defaults to Java 8
116-
// The release option was added in 9
117-
// If no properties have yet been updated then set release explicitly
118-
if (!foundProperty && version >= 9 && !compilerPluginConfiguredExplicitly) {
119-
d = (Xml.Document) new AddProperty("maven.compiler.release", String.valueOf(version), null, false)
120-
.getVisitor()
121-
.visitNonNull(d, ctx);
122-
maybeUpdateModel();
123-
}
124-
125-
return d;
126-
}
127-
128-
@Override
129-
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
130-
Xml.Tag t = super.visitTag(tag, ctx);
131-
if (isPluginTag("org.apache.maven.plugins", "maven-compiler-plugin")) {
132-
t.getChild("configuration").ifPresent(compilerPluginConfig -> {
133-
if (compilerPluginConfig.getChildValue("source").isPresent() ||
134-
compilerPluginConfig.getChildValue("target").isPresent() ||
135-
compilerPluginConfig.getChildValue("release").isPresent()) {
136-
compilerPluginConfiguredExplicitly = true;
137-
}
138-
});
139-
}
140-
return t;
141-
}
142-
};
61+
public List<Recipe> getRecipeList() {
62+
return singletonList(new org.openrewrite.maven.UpdateMavenProjectPropertyJavaVersion(version));
14363
}
14464
}

0 commit comments

Comments
 (0)