|
26 | 26 | import org.openrewrite.java.tree.J; |
27 | 27 | import org.openrewrite.marker.SearchResult; |
28 | 28 | import org.openrewrite.maven.MavenVisitor; |
| 29 | +import org.openrewrite.xml.XPathMatcher; |
| 30 | +import org.openrewrite.xml.search.FindTags; |
29 | 31 | import org.openrewrite.xml.tree.Xml; |
30 | 32 |
|
| 33 | +import java.util.Arrays; |
31 | 34 | import java.util.Collections; |
32 | 35 | import java.util.List; |
33 | 36 | import java.util.Optional; |
| 37 | +import java.util.stream.Collectors; |
34 | 38 |
|
35 | 39 |
|
36 | 40 | @Value |
@@ -117,22 +121,49 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, |
117 | 121 | } |
118 | 122 | } |
119 | 123 |
|
| 124 | + private static final List<String> JAVA_VERSION_XPATHS = Arrays.asList( |
| 125 | + "/project/properties/java.version", |
| 126 | + "/project/properties/jdk.version", |
| 127 | + "/project/properties/javaVersion", |
| 128 | + "/project/properties/jdkVersion", |
| 129 | + "/project/properties/maven.compiler.source", |
| 130 | + "/project/properties/maven.compiler.target", |
| 131 | + "/project/properties/maven.compiler.release", |
| 132 | + "/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/source", |
| 133 | + "/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/target", |
| 134 | + "/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/release"); |
| 135 | + |
| 136 | + private static final List<XPathMatcher> JAVA_VERSION_XPATH_MATCHERS = |
| 137 | + JAVA_VERSION_XPATHS.stream().map(XPathMatcher::new).collect(Collectors.toList()); |
| 138 | + |
| 139 | + |
120 | 140 | private class MavenUpdateJavaVersionVisitor extends MavenVisitor<ExecutionContext> { |
121 | 141 | @Override |
122 | 142 | public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { |
123 | | - Xml.Tag t = (Xml.Tag) super.visitTag(tag, ctx); |
124 | | - if (!isPropertyTag()) { |
125 | | - return t; |
126 | | - } |
127 | | - if (!"java.version".equals(t.getName()) && !"maven.compiler.source".equals(t.getName()) && !"maven.compiler.target".equals(t.getName()) || |
128 | | - (tag.getValue().isPresent() && tag.getValue().get().startsWith("${"))) { |
129 | | - return t; |
130 | | - } |
131 | | - float value = tag.getValue().map(Float::parseFloat).orElse(0f); |
132 | | - if (value >= version) { |
133 | | - return t; |
| 143 | + tag = (Xml.Tag) super.visitTag(tag, ctx); |
| 144 | + |
| 145 | + if (JAVA_VERSION_XPATH_MATCHERS.stream().anyMatch(matcher -> matcher.matches(getCursor()))) { |
| 146 | + Optional<Float> maybeVersion = tag.getValue().flatMap( |
| 147 | + value -> { |
| 148 | + try { |
| 149 | + return Optional.of(Float.parseFloat(value)); |
| 150 | + } catch (NumberFormatException e) { |
| 151 | + return Optional.empty(); |
| 152 | + } |
| 153 | + } |
| 154 | + ); |
| 155 | + |
| 156 | + if (!maybeVersion.isPresent()) { |
| 157 | + return tag; |
| 158 | + } |
| 159 | + float currentVersion = maybeVersion.get(); |
| 160 | + if (currentVersion >= version) { |
| 161 | + return tag; |
| 162 | + } |
| 163 | + return tag.withValue(String.valueOf(version)); |
134 | 164 | } |
135 | | - return t.withValue(String.valueOf(version)); |
| 165 | + |
| 166 | + return tag; |
136 | 167 | } |
137 | 168 | } |
138 | 169 | } |
0 commit comments