Skip to content

Fix LombokValToFinalVar in loops #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -15,16 +15,15 @@
*/
package org.openrewrite.java.migrate.lombok;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
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.TypeTree;
import org.openrewrite.java.tree.TypeUtils;

import java.time.Duration;
Expand Down Expand Up @@ -75,26 +74,28 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit,
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations mv, ExecutionContext ctx) {
J.VariableDeclarations varDecls = super.visitVariableDeclarations(mv, ctx);

if (TypeUtils.isOfClassType(varDecls.getType(), LOMBOK_VAL) ||
(varDecls.getTypeExpression() instanceof J.Identifier && ((J.Identifier) varDecls.getTypeExpression()).getSimpleName().equals("val"))) {
(varDecls.getTypeExpression() instanceof J.Identifier && ((J.Identifier) varDecls.getTypeExpression()).getSimpleName().equals("val"))) {
maybeRemoveImport(LOMBOK_VAL);

J.VariableDeclarations.NamedVariable nv = mv.getVariables().get(0);
String finalVarVariableTemplateString;
Object[] args;
if (nv.getInitializer() == null) {
finalVarVariableTemplateString = "final var #{}";
args = new Object[]{nv.getSimpleName()};
// 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);
varDecls = varDecls.withTypeExpression(varType);
} else {
finalVarVariableTemplateString = "final var #{} = #{any()};";
args = new Object[]{nv.getSimpleName(), nv.getInitializer()};
}
varDecls = JavaTemplate.builder(finalVarVariableTemplateString)
.contextSensitive()
.build()
.apply(updateCursor(varDecls), varDecls.getCoordinates().replace(), args);

if (nv.getInitializer() != null) {
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()))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.openrewrite.java.migrate.lombok;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -112,40 +114,152 @@ void bar() {
);
}

@Nested
@SuppressWarnings({"StatementWithEmptyBody", "RedundantOperationOnEmptyContainer"})
@Test
void valInForEachStatement() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;
import java.util.List;
import java.util.ArrayList;
class ValInLoop {
@Test
void list() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;

class A {
void bar() {
List<String> lst = new ArrayList<>();
for (val s : lst) {}
import java.util.ArrayList;
import java.util.List;

class A {
void bar() {
List<String> lst = new ArrayList<>();
for (val s : lst) {}
}
}
}
""",
"""
import java.util.List;
import java.util.ArrayList;
""",
"""
import java.util.ArrayList;
import java.util.List;

class A {
void bar() {
List<String> lst = new ArrayList<>();
for (final var s : lst) {}
class A {
void bar() {
List<String> lst = new ArrayList<>();
for (var s : lst) {}
}
}
}
"""
),
17
)
);
}

@Test
void array() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;
import java.util.List;
import java.util.ArrayList;

class A {
void bar() {
String[] lst = new String[]{"ABC", "DEF", "DOESN'T", "MATTER"};
for (val s : lst) {}
}
}
""",
"""
import java.util.List;
import java.util.ArrayList;

class A {
void bar() {
String[] lst = new String[]{"ABC", "DEF", "DOESN'T", "MATTER"};
for (var s : lst) {}
}
}
"""
),
17
)
);
}

@Test
void parameter() {
//language=java
rewriteRun(
version(
java(
"""
import lombok.val;
import java.nio.file.Path;
import java.util.List;
import java.util.ArrayList;

class A {
static void bar(final List<Path> lst) {
for (val s : lst) {}
}
}
""",
"""
import java.nio.file.Path;
import java.util.List;
import java.util.ArrayList;

class A {
static void bar(final List<Path> lst) {
for (var s : lst) {}
}
}
"""
),
17
)
);
}


@Test
void valuesFromMethod() {
//language=java
rewriteRun(
java(
"""
),
17
)
);
interface Mapper {
String[] getNamesList();
}
"""
),
version(
java(
"""
import lombok.val;

class A {
Mapper mapper;
void bar() {
for (val s : mapper.getNamesList()) {}
}
}
""",
"""
class A {
Mapper mapper;
void bar() {
for (var s : mapper.getNamesList()) {}
}
}
"""
),
17
)
);
}
}

@Test
Expand Down