
org.openrewrite.java.migrate.lombok.LombokValToFinalVar Maven / Gradle / Ivy
Show all versions of rewrite-migrate-java Show documentation
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.RemoveImport;
import org.openrewrite.java.search.MaybeUsesImport;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;
import java.time.Duration;
import java.util.Collections;
import java.util.Set;
public class LombokValToFinalVar extends Recipe {
private static final String LOMBOK_VAL = "lombok.val";
private static final String LOMBOK_VAR = "lombok.var";
@Override
public String getDisplayName() {
return "Prefer `final var` over `lombok.val`";
}
@Override
public String getDescription() {
return "Prefer the Java standard library's `final var` and `var` over third-party usage of Lombok's `lombok.val` and `lombok.var` in Java 10 or higher.";
}
@Override
public Set getTags() {
return Collections.singleton("lombok");
}
@Override
public Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(1);
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
TreeVisitor, ExecutionContext> check = Preconditions.or(
new UsesType<>(LOMBOK_VAL, false),
// The parser does not recognize `var` as coming from Lombok, which happens to align with what we want
new MaybeUsesImport<>(LOMBOK_VAR));
return Preconditions.check(check, new LombokValToFinalVarVisitor());
}
private static class LombokValToFinalVarVisitor extends JavaIsoVisitor {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit, ExecutionContext ctx) {
// Force remove `lombok.var` import; as the parser does not recognize `var` as coming from Lombok
RemoveImport op = new RemoveImport<>(LOMBOK_VAR, true);
if (!getAfterVisit().contains(op)) {
doAfterVisit(op);
}
return super.visitCompilationUnit(compilationUnit, ctx);
}
@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"))) {
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()};
} 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 = varDecls.withVariables(ListUtils.map(varDecls.getVariables(), namedVar -> namedVar
.withInitializer(namedVar.getInitializer().withPrefix(nv.getInitializer().getPrefix()))));
}
}
return varDecls;
}
}
}