Skip to content

Commit 50df561

Browse files
author
Mushtaq Ahmed
committed
Convert into recipe instead of Visitor
1 parent 722e646 commit 50df561

File tree

2 files changed

+229
-171
lines changed

2 files changed

+229
-171
lines changed

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/ChangeMavenCompilerPluginConfiguration.java

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
import java.util.Optional;
44

5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
import org.jetbrains.annotations.NotNull;
58
import org.openrewrite.ExecutionContext;
6-
import org.openrewrite.maven.AddProperty;
9+
import org.openrewrite.Recipe;
10+
import org.openrewrite.TreeVisitor;
711
import org.openrewrite.maven.ChangePropertyValue;
812
import org.openrewrite.maven.MavenVisitor;
913
import org.openrewrite.maven.RemoveProperty;
1014
import org.openrewrite.xml.AddToTagVisitor;
1115
import org.openrewrite.xml.ChangeTagValueVisitor;
1216
import org.openrewrite.xml.XPathMatcher;
13-
import org.openrewrite.xml.format.AutoFormat;
1417
import org.openrewrite.xml.tree.Xml;
1518

1619
import static org.openrewrite.xml.AddToTagVisitor.addToTag;
1720

18-
public class ChangeMavenCompilerPluginConfiguration extends MavenVisitor<ExecutionContext> {
21+
@Data
22+
@EqualsAndHashCode(callSuper = true)
23+
public class ChangeMavenCompilerPluginConfiguration extends Recipe {
1924

2025
private static final String GROUP_ID = "org.apache.maven.plugins";
2126

2227
private static final String ARTIFACT_ID = "maven-compiler-plugin";
2328

24-
private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher("/project/build/plugins");
29+
private static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("/project/build/plugins");
2530

2631
private static final String MAVEN_COMPILER_SOURCE = "${maven.compiler.source}";
2732

@@ -30,69 +35,84 @@ public class ChangeMavenCompilerPluginConfiguration extends MavenVisitor<Executi
3035
private static final String JAVA_VERSION = "java.version";
3136

3237
@Override
33-
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
34-
Xml.Tag plugins = (Xml.Tag) super.visitTag(tag, ctx);
35-
if (!PLUGINS_MATCHER.matches(getCursor())) {
36-
return plugins;
37-
}
38-
Optional<Xml.Tag> maybePlugin = plugins.getChildren().stream()
39-
.filter(plugin -> "plugin".equals(plugin.getName())
40-
&& GROUP_ID.equals(plugin.getChildValue("groupId").orElse(null))
41-
&& ARTIFACT_ID.equals(plugin.getChildValue("artifactId").orElse(null)))
42-
.findAny();
43-
if (maybePlugin.isEmpty()) {
44-
return plugins;
45-
}
46-
47-
Xml.Tag plugin = maybePlugin.get();
48-
Optional<Xml.Tag> maybeConfiguration = maybePlugin.get().getChild("configuration");
49-
if (maybeConfiguration.isPresent()) {
50-
Xml.Tag configuration = maybeConfiguration.get();
51-
52-
String sourceValue = configuration.getChildValue("source").orElse(null);
53-
String targetValue = configuration.getChildValue("target").orElse(null);
38+
public @NotNull String getDisplayName() {
39+
return "null";
40+
}
5441

55-
if (sourceValue != null && targetValue != null) {
56-
String sourceLookupValue = sourceValue.startsWith("${")
57-
? super.getResolutionResult().getPom().getValue(sourceValue.trim()) : sourceValue;
42+
@Override
43+
protected @NotNull TreeVisitor<?, ExecutionContext> getVisitor() {
44+
return new ChangeMavenPluginConfigurationVisitor();
45+
}
5846

59-
String targetLookupValue = targetValue.startsWith("${")
60-
? super.getResolutionResult().getPom().getValue(targetValue.trim()) : targetValue;
47+
private static class ChangeMavenPluginConfigurationVisitor extends MavenVisitor<ExecutionContext> {
6148

62-
// If source and target version are same
63-
if (sourceLookupValue != null && sourceLookupValue.equals(targetLookupValue)){
64-
doAfterVisit(new ChangePropertyValue(JAVA_VERSION, sourceLookupValue, true));
65-
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("source").get(), MAVEN_COMPILER_SOURCE));
66-
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("target").get(), MAVEN_COMPILER_TARGET));
49+
@Override
50+
public @NotNull Xml visitTag(Xml.@NotNull Tag tag, @NotNull ExecutionContext ctx) {
51+
Xml.Tag plugins = (Xml.Tag) super.visitTag(tag, ctx);
52+
if (!PLUGIN_MATCHER.matches(getCursor())) {
53+
return plugins;
54+
}
55+
Optional<Xml.Tag> maybePlugin = plugins.getChildren().stream()
56+
.filter(plugin -> "plugin".equals(plugin.getName())
57+
&& GROUP_ID.equals(plugin.getChildValue("groupId").orElse(null))
58+
&& ARTIFACT_ID.equals(plugin.getChildValue("artifactId").orElse(null)))
59+
.findAny();
60+
if (maybePlugin.isEmpty()) {
61+
return plugins;
62+
}
6763

68-
if (sourceValue.startsWith("${")) {
69-
doAfterVisit(new RemoveProperty(sourceValue.substring(2, sourceValue.length() - 1)));
70-
}
71-
if (targetValue.startsWith("${")){
72-
doAfterVisit(new RemoveProperty(targetValue.substring(2, targetValue.length() - 1)));
64+
Xml.Tag plugin = maybePlugin.get();
65+
Optional<Xml.Tag> maybeConfiguration = maybePlugin.get().getChild("configuration");
66+
if (maybeConfiguration.isPresent()) {
67+
Xml.Tag configuration = maybeConfiguration.get();
68+
69+
String sourceValue = configuration.getChildValue("source").orElse(null);
70+
String targetValue = configuration.getChildValue("target").orElse(null);
71+
72+
if (sourceValue != null && targetValue != null) {
73+
String sourceLookupValue = sourceValue.startsWith("${")
74+
? super.getResolutionResult().getPom().getValue(sourceValue.trim()) : sourceValue;
75+
76+
String targetLookupValue = targetValue.startsWith("${")
77+
? super.getResolutionResult().getPom().getValue(targetValue.trim()) : targetValue;
78+
79+
// If source and target version are same
80+
if (sourceLookupValue != null && sourceLookupValue.equals(targetLookupValue)) {
81+
doAfterVisit(new ChangePropertyValue(JAVA_VERSION, sourceLookupValue, true));
82+
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("source").get(),
83+
MAVEN_COMPILER_SOURCE));
84+
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("target").get(),
85+
MAVEN_COMPILER_TARGET));
86+
87+
if (sourceValue.startsWith("${")) {
88+
doAfterVisit(new RemoveProperty(sourceValue.substring(2, sourceValue.length() - 1)));
89+
}
90+
if (targetValue.startsWith("${")) {
91+
doAfterVisit(new RemoveProperty(targetValue.substring(2, targetValue.length() - 1)));
92+
}
7393
}
7494
}
75-
}
7695

77-
if (sourceValue == null ){
78-
addToTag(configuration,
79-
Xml.Tag.build("<source>" + MAVEN_COMPILER_SOURCE + "</source>\n")
80-
.withPrefix("\n"),
81-
getCursor());
82-
}
96+
if (sourceValue == null) {
97+
addToTag(configuration,
98+
Xml.Tag.build("<source>" + MAVEN_COMPILER_SOURCE + "</source>\n").withPrefix("\n"),
99+
getCursor());
100+
}
83101

84-
if (targetValue == null){
85-
addToTag(configuration,
86-
Xml.Tag.build("<target>" + MAVEN_COMPILER_TARGET + "</target>\n")
87-
.withPrefix("\n"),
88-
getCursor());
102+
if (targetValue == null) {
103+
addToTag(configuration,
104+
Xml.Tag.build("<target>" + MAVEN_COMPILER_TARGET + "</target>\n").withPrefix("\n"),
105+
getCursor());
106+
}
89107
}
108+
else {
109+
Xml.Tag configurations = Xml.Tag.build("<configuration>\n<source>" + MAVEN_COMPILER_SOURCE
110+
+ "</source>\n<target>" + MAVEN_COMPILER_TARGET + "</target>\n</configuration>\n");
111+
doAfterVisit(new AddToTagVisitor<>(plugin, configurations));
112+
}
113+
return plugins;
90114
}
91-
else {
92-
Xml.Tag configurations = Xml.Tag.build("<configuration>\n<source>" + MAVEN_COMPILER_SOURCE
93-
+ "</source>\n<target>" + MAVEN_COMPILER_TARGET + "</target>\n</configuration>\n");
94-
doAfterVisit(new AddToTagVisitor<>(plugin, configurations));
95-
}
96-
return plugins;
115+
97116
}
117+
98118
}

0 commit comments

Comments
 (0)