Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.MaybeUsesImport;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Markers;

import java.time.Duration;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;

public class LombokValToFinalVar extends Recipe {
Expand Down Expand Up @@ -79,27 +81,32 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
(varDecls.getTypeExpression() instanceof J.Identifier && "val".equals(((J.Identifier) varDecls.getTypeExpression()).getSimpleName()))) {
maybeRemoveImport(LOMBOK_VAL);

J.VariableDeclarations.NamedVariable nv = mv.getVariables().get(0);
// Manually transform to `var`, rather than using a JavaTemplate, so that the initializer
// is carried over untouched; reprinting it can lose type information, such as the type
// variable in a cast like `(T) o`.
J.VariableDeclarations.NamedVariable nv = varDecls.getVariables().get(0);
TypeTree typeExpression = varDecls.getTypeExpression();
J.Identifier varType = new J.Identifier(Tree.randomId(),
typeExpression.getPrefix(),
typeExpression.getMarkers(),
service(AnnotationService.class).getAllAnnotations(getCursor()),
"var",
nv.getType(),
null);
if (nv.getInitializer() == null) {
// manually transform to var, as val in this case has no sufficient type information
// and the java template parsing would fail, see https://github.com/openrewrite/rewrite/pull/5637
TypeTree typeExpression = varDecls.getTypeExpression();
J.Identifier varType = new J.Identifier(Tree.randomId(),
typeExpression.getPrefix(),
typeExpression.getMarkers(),
service(AnnotationService.class).getAllAnnotations(getCursor()),
"var",
nv.getType(),
null);
// `val` in this case has no sufficient type information, so it becomes a plain `var`,
// see https://github.com/openrewrite/rewrite/pull/5637
return varDecls.withTypeExpression(varType);
}

varDecls = JavaTemplate.builder("final var #{} = #{any()};")
.contextSensitive()
.build()
.apply(updateCursor(varDecls), varDecls.getCoordinates().replace(), nv.getSimpleName(), nv.getInitializer());
varDecls = varDecls.withVariables(ListUtils.map(varDecls.getVariables(), namedVar -> namedVar
.withInitializer(namedVar.getInitializer().withPrefix(nv.getInitializer().getPrefix()))));
if (varDecls.getModifiers().stream().noneMatch(m -> m.getType() == J.Modifier.Type.Final)) {
varDecls = varDecls.withModifiers(ListUtils.concat(
new J.Modifier(Tree.randomId(), typeExpression.getPrefix(), Markers.EMPTY,
null, J.Modifier.Type.Final, emptyList()),
varDecls.getModifiers()));
varType = varType.withPrefix(Space.SINGLE_SPACE);
}
varDecls = varDecls.withTypeExpression(varType);
}
return varDecls;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void upgradesMavenPluginsForJava25() {
spec -> spec.after(actual ->
assertThat(actual)
.contains("<maven.compiler.release>25</maven.compiler.release>")
.containsPattern("maven-compiler-plugin</artifactId>\\s*<version>3\\.15\\.")
.containsPattern("maven-compiler-plugin</artifactId>\\s*<version>3\\.(1[5-9]|[2-9]\\d)\\.")
.containsPattern("maven-surefire-plugin</artifactId>\\s*<version>3\\.5\\.")
.containsPattern("maven-failsafe-plugin</artifactId>\\s*<version>3\\.5\\.")
.contains("<argLine>-javaagent:${org.mockito:mockito-core:jar}</argLine>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,58 @@ void bar() {
)
);
}

@Test
void preserveCast() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;
class CastDemo {
void bar() {
val foo = (String) "foo";
}
}
""",
"""
class CastDemo {
void bar() {
final var foo = (String) "foo";
}
}
"""
),
17
)
);
}

@Test
void preserveGenericCast() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;
class GenericCastDemo<T> {
void bar(Object o) {
val foo = (T) o;
}
}
""",
"""
class GenericCastDemo<T> {
void bar(Object o) {
final var foo = (T) o;
}
}
"""
),
17
)
);
}
}
Loading