Skip to content

Commit

Permalink
Extract a reusable FieldAnnotator class
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 15, 2024
1 parent 22810ec commit a1288ef
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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 lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import static java.util.Comparator.comparing;
import static lombok.AccessLevel.PUBLIC;

@Value
@EqualsAndHashCode(callSuper = false)
class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> {

Class<?> annotation;
JavaType field;
AccessLevel accessLevel;

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) {
if (variable.getName().getFieldType() == field) {
maybeAddImport(annotation.getName());
maybeAddImport("lombok.AccessLevel");
String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name());
return JavaTemplate.builder("@" + annotation.getSimpleName() + suffix)
.imports(annotation.getName(), "lombok.AccessLevel")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build().apply(getCursor(), multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
}
}
return multiVariable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@
*/
package org.openrewrite.java.migrate.lombok;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import static java.util.Comparator.comparing;
import static lombok.AccessLevel.PUBLIC;

@Value
@EqualsAndHashCode(callSuper = false)
Expand Down Expand Up @@ -66,12 +61,14 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
if (returnExpression instanceof J.Identifier &&
((J.Identifier) returnExpression).getFieldType() != null) {
doAfterVisit(new FieldAnnotator(
Getter.class,
((J.Identifier) returnExpression).getFieldType(),
LombokUtils.getAccessLevel(method)));
return null;
} else if (returnExpression instanceof J.FieldAccess &&
((J.FieldAccess) returnExpression).getName().getFieldType() != null) {
doAfterVisit(new FieldAnnotator(
Getter.class,
((J.FieldAccess) returnExpression).getName().getFieldType(),
LombokUtils.getAccessLevel(method)));
return null;
Expand All @@ -81,29 +78,4 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}
};
}


@Value
@EqualsAndHashCode(callSuper = false)
static class FieldAnnotator extends JavaIsoVisitor<ExecutionContext> {

JavaType field;
AccessLevel accessLevel;

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
for (J.VariableDeclarations.NamedVariable variable : multiVariable.getVariables()) {
if (variable.getName().getFieldType() == field) {
maybeAddImport("lombok.Getter");
maybeAddImport("lombok.AccessLevel");
String suffix = accessLevel == PUBLIC ? "" : String.format("(AccessLevel.%s)", accessLevel.name());
return JavaTemplate.builder("@Getter" + suffix)
.imports("lombok.Getter", "lombok.AccessLevel")
.javaParser(JavaParser.fromJavaVersion().classpath("lombok"))
.build().apply(getCursor(), multiVariable.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName)));
}
}
return multiVariable;
}
}
}

0 comments on commit a1288ef

Please sign in to comment.