Skip to content

Commit

Permalink
Polish UpdateJavaCompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Mar 15, 2024
1 parent 3f7b739 commit 2977329
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.Tree.randomId;

@Value
Expand All @@ -42,14 +43,14 @@ public class UpdateJavaCompatibility extends Recipe {
example = "11")
Integer version;

@Option(displayName = "Compatibility Type",
@Option(displayName = "Compatibility type",
description = "The compatibility type to change",
valid = {"source", "target"},
required = false)
@Nullable
CompatibilityType compatibilityType;

@Option(displayName = "Declaration Style",
@Option(displayName = "Declaration style",
description = "The desired style to write the new version as when being written to the `sourceCompatibility` " +
"or `targetCompatibility` variables. Default, match current source style. " +
"(ex. Enum: `JavaVersion.VERSION_11`, Number: 11, or String: \"11\")",
Expand All @@ -58,14 +59,14 @@ public class UpdateJavaCompatibility extends Recipe {
@Nullable
DeclarationStyle declarationStyle;

@Option(displayName = "Allow Downgrade",
@Option(displayName = "Allow downgrade",
description = "Allow downgrading the Java version.",
required = false)
@Nullable
Boolean allowDowngrade;

@Option(displayName = "Add Compatibility Type if missing",
description = "Adds the specified Compatibility Type if one is not found.",
@Option(displayName = "Add compatibility type if missing",
description = "Adds the specified compatibility type if one is not found.",
required = false)
@Nullable
Boolean addIfMissing;
Expand All @@ -84,7 +85,7 @@ public String getDescription() {
}

@Override
public Validated validate() {
public Validated<Object> validate() {
return super.validate().and(Validated.test("version", "Version must be > 0.", version, v -> v > 0));
}

Expand Down Expand Up @@ -214,7 +215,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return m;
}

private int getMajorVersion(String version) {
private int getMajorVersion(@Nullable String version) {
if(version == null) {
return -1;
}
try {
return Integer.parseInt(normalize(version));
} catch (NumberFormatException e) {
Expand All @@ -229,9 +233,9 @@ private int getMajorVersion(Expression expression) {
if (type == JavaType.Primitive.String) {
return getMajorVersion((String) argument.getValue());
} else if (type == JavaType.Primitive.Int) {
return (int) argument.getValue();
return (int) requireNonNull(argument.getValue());
} else if (type == JavaType.Primitive.Double) {
return getMajorVersion(argument.getValue().toString());
return getMajorVersion(requireNonNull(argument.getValue()).toString());
}
} else if (expression instanceof J.FieldAccess) {
J.FieldAccess field = (J.FieldAccess) expression;
Expand Down Expand Up @@ -383,7 +387,7 @@ private G.CompilationUnit addCompatibilityTypeToSourceFile(G.CompilationUnit c,
if ((this.compatibilityType == null || compatibilityType.equals(this.compatibilityType.toString())) && Boolean.TRUE.equals(addIfMissing)) {
G.CompilationUnit sourceFile = (G.CompilationUnit) GradleParser.builder().build().parse("\n" + compatibilityType + "Compatibility = " + styleMissingCompatibilityVersion())
.findFirst()
.get();
.orElseThrow(() -> new IllegalStateException("Unable to parse compatibility type as a Gradle file"));
sourceFile.getStatements();
c = c.withStatements(ListUtils.concatAll(c.getStatements(), sourceFile.getStatements()));
}
Expand Down

0 comments on commit 2977329

Please sign in to comment.