-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize Java upgrade to not overreport time savings
- Loading branch information
1 parent
3b324fa
commit 9c6acfb
Showing
8 changed files
with
138 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/org/openrewrite/java/migrate/maven/UpdateMavenProjectPropertyJavaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.openrewrite.java.migrate.maven; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.maven.MavenVisitor; | ||
import org.openrewrite.xml.XPathMatcher; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UpdateMavenProjectPropertyJavaVersion extends Recipe { | ||
|
||
private static final List<String> JAVA_VERSION_XPATHS = Arrays.asList( | ||
"/project/properties/java.version", | ||
"/project/properties/jdk.version", | ||
"/project/properties/javaVersion", | ||
"/project/properties/jdkVersion", | ||
"/project/properties/maven.compiler.source", | ||
"/project/properties/maven.compiler.target", | ||
"/project/properties/maven.compiler.release", | ||
"/project/properties/release.version"); | ||
|
||
private static final List<XPathMatcher> JAVA_VERSION_XPATH_MATCHERS = | ||
JAVA_VERSION_XPATHS.stream().map(XPathMatcher::new).collect(Collectors.toList()); | ||
|
||
@Option(displayName = "Java version", | ||
description = "The Java version to upgrade to.", | ||
example = "11") | ||
Integer version; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Update Maven Java project properties"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "The Java version is determined by several project properties, including:\n\n" + | ||
" * `java.version`\n" + | ||
" * `jdk.version`\n" + | ||
" * `javaVersion`\n" + | ||
" * `jdkVersion`\n" + | ||
" * `maven.compiler.source`\n" + | ||
" * `maven.compiler.target`\n" + | ||
" * `maven.compiler.release`\n" + | ||
" * `release.version`\n\n" + | ||
" These project properties are not added if they are not currently set, but only updated in place."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new MavenVisitor<ExecutionContext>() { | ||
@Override | ||
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { | ||
tag = (Xml.Tag) super.visitTag(tag, ctx); | ||
|
||
if (JAVA_VERSION_XPATH_MATCHERS.stream().anyMatch(matcher -> matcher.matches(getCursor()))) { | ||
Optional<Float> maybeVersion = tag.getValue().flatMap( | ||
value -> { | ||
try { | ||
return Optional.of(Float.parseFloat(value)); | ||
} catch (NumberFormatException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
); | ||
|
||
if (!maybeVersion.isPresent()) { | ||
return tag; | ||
} | ||
float currentVersion = maybeVersion.get(); | ||
if (currentVersion >= version) { | ||
return tag; | ||
} | ||
return tag.withValue(String.valueOf(version)); | ||
} | ||
|
||
return tag; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters