Skip to content

Commit 01384fb

Browse files
MBoegerstimtebeek
andauthored
Fix LombokValToFinalVar in loops (#768)
* add test cases based on OpenSource findings * manually migrate from `for(val s : strings)` to `for(var s : strings)` to avoid java template * fix type and imports * Apply suggestions from code review * Apply suggestions from code review * Add early return for new case * Minimize test text blocks --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 4aa3f71 commit 01384fb

File tree

2 files changed

+118
-47
lines changed

2 files changed

+118
-47
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/LombokValToFinalVar.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
package org.openrewrite.java.migrate.lombok;
1717

18-
import org.openrewrite.ExecutionContext;
19-
import org.openrewrite.Preconditions;
20-
import org.openrewrite.Recipe;
21-
import org.openrewrite.TreeVisitor;
18+
import org.openrewrite.*;
2219
import org.openrewrite.internal.ListUtils;
2320
import org.openrewrite.java.JavaIsoVisitor;
2421
import org.openrewrite.java.JavaTemplate;
2522
import org.openrewrite.java.search.MaybeUsesImport;
2623
import org.openrewrite.java.search.UsesType;
24+
import org.openrewrite.java.service.AnnotationService;
2725
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.TypeTree;
2827
import org.openrewrite.java.tree.TypeUtils;
2928

3029
import java.time.Duration;
@@ -75,29 +74,32 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit,
7574
@Override
7675
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations mv, ExecutionContext ctx) {
7776
J.VariableDeclarations varDecls = super.visitVariableDeclarations(mv, ctx);
77+
7878
if (TypeUtils.isOfClassType(varDecls.getType(), LOMBOK_VAL) ||
79-
(varDecls.getTypeExpression() instanceof J.Identifier && ((J.Identifier) varDecls.getTypeExpression()).getSimpleName().equals("val"))) {
79+
(varDecls.getTypeExpression() instanceof J.Identifier && "val".equals(((J.Identifier) varDecls.getTypeExpression()).getSimpleName()))) {
8080
maybeRemoveImport(LOMBOK_VAL);
8181

8282
J.VariableDeclarations.NamedVariable nv = mv.getVariables().get(0);
83-
String finalVarVariableTemplateString;
84-
Object[] args;
8583
if (nv.getInitializer() == null) {
86-
finalVarVariableTemplateString = "final var #{}";
87-
args = new Object[]{nv.getSimpleName()};
88-
} else {
89-
finalVarVariableTemplateString = "final var #{} = #{any()};";
90-
args = new Object[]{nv.getSimpleName(), nv.getInitializer()};
84+
// manually transform to var, as val in this case has no sufficient type information
85+
// and the java template parsing would fail, see https://github.com/openrewrite/rewrite/pull/5637
86+
TypeTree typeExpression = varDecls.getTypeExpression();
87+
J.Identifier varType = new J.Identifier(Tree.randomId(),
88+
typeExpression.getPrefix(),
89+
typeExpression.getMarkers(),
90+
service(AnnotationService.class).getAllAnnotations(getCursor()),
91+
"var",
92+
nv.getType(),
93+
null);
94+
return varDecls.withTypeExpression(varType);
9195
}
92-
varDecls = JavaTemplate.builder(finalVarVariableTemplateString)
96+
97+
varDecls = JavaTemplate.builder("final var #{} = #{any()};")
9398
.contextSensitive()
9499
.build()
95-
.apply(updateCursor(varDecls), varDecls.getCoordinates().replace(), args);
96-
97-
if (nv.getInitializer() != null) {
98-
varDecls = varDecls.withVariables(ListUtils.map(varDecls.getVariables(), namedVar -> namedVar
99-
.withInitializer(namedVar.getInitializer().withPrefix(nv.getInitializer().getPrefix()))));
100-
}
100+
.apply(updateCursor(varDecls), varDecls.getCoordinates().replace(), nv.getSimpleName(), nv.getInitializer());
101+
varDecls = varDecls.withVariables(ListUtils.map(varDecls.getVariables(), namedVar -> namedVar
102+
.withInitializer(namedVar.getInitializer().withPrefix(nv.getInitializer().getPrefix()))));
101103
}
102104
return varDecls;
103105
}

src/test/java/org/openrewrite/java/migrate/lombok/LombokValToFinalVarTest.java

Lines changed: 97 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.migrate.lombok;
1717

18+
import org.junit.jupiter.api.Nested;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.java.JavaParser;
@@ -112,40 +113,108 @@ void bar() {
112113
);
113114
}
114115

116+
@Nested
115117
@SuppressWarnings({"StatementWithEmptyBody", "RedundantOperationOnEmptyContainer"})
116-
@Test
117-
void valInForEachStatement() {
118-
//language=java
119-
rewriteRun(
120-
version(
121-
java(
122-
"""
123-
import lombok.val;
124-
import java.util.List;
125-
import java.util.ArrayList;
118+
class ValInLoop {
119+
@Test
120+
void list() {
121+
//language=java
122+
rewriteRun(
123+
version(
124+
java(
125+
"""
126+
import lombok.val;
126127
127-
class A {
128-
void bar() {
129-
List<String> lst = new ArrayList<>();
130-
for (val s : lst) {}
128+
import java.util.List;
129+
130+
class A {
131+
void bar(List<String> lst) {
132+
for (val s : lst) {}
133+
}
131134
}
132-
}
133-
""",
134-
"""
135-
import java.util.List;
136-
import java.util.ArrayList;
135+
""",
136+
"""
137+
import java.util.List;
137138
138-
class A {
139-
void bar() {
140-
List<String> lst = new ArrayList<>();
141-
for (final var s : lst) {}
139+
class A {
140+
void bar(List<String> lst) {
141+
for (var s : lst) {}
142+
}
142143
}
143-
}
144+
"""
145+
),
146+
17
147+
)
148+
);
149+
}
150+
151+
@Test
152+
void array() {
153+
//language=java
154+
rewriteRun(
155+
version(
156+
java(
157+
"""
158+
import lombok.val;
159+
import java.util.List;
160+
import java.util.ArrayList;
161+
162+
class A {
163+
void bar(String[] lst) {
164+
for (val s : lst) {}
165+
}
166+
}
167+
""",
168+
"""
169+
import java.util.List;
170+
import java.util.ArrayList;
171+
172+
class A {
173+
void bar(String[] lst) {
174+
for (var s : lst) {}
175+
}
176+
}
177+
"""
178+
),
179+
17
180+
)
181+
);
182+
}
183+
184+
@Test
185+
void valuesFromMethod() {
186+
//language=java
187+
rewriteRun(
188+
java(
144189
"""
145-
),
146-
17
147-
)
148-
);
190+
interface Mapper {
191+
String[] getNamesList();
192+
}
193+
"""
194+
),
195+
version(
196+
java(
197+
"""
198+
import lombok.val;
199+
200+
class A {
201+
void bar(Mapper mapper) {
202+
for (val s : mapper.getNamesList()) {}
203+
}
204+
}
205+
""",
206+
"""
207+
class A {
208+
void bar(Mapper mapper) {
209+
for (var s : mapper.getNamesList()) {}
210+
}
211+
}
212+
"""
213+
),
214+
17
215+
)
216+
);
217+
}
149218
}
150219

151220
@Test

0 commit comments

Comments
 (0)