|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.lombok; |
| 17 | + |
| 18 | +import lombok.AccessLevel; |
| 19 | +import lombok.EqualsAndHashCode; |
| 20 | +import lombok.Value; |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | +import org.openrewrite.ExecutionContext; |
| 23 | +import org.openrewrite.Recipe; |
| 24 | +import org.openrewrite.TreeVisitor; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.JavaParser; |
| 27 | +import org.openrewrite.java.JavaTemplate; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.java.tree.TypeUtils; |
| 30 | + |
| 31 | +import static java.util.Comparator.comparing; |
| 32 | + |
| 33 | +@Value |
| 34 | +@EqualsAndHashCode(callSuper = false) |
| 35 | +public class UseNoArgsConstructor extends Recipe { |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDisplayName() { |
| 39 | + //language=markdown |
| 40 | + return "Use `@NoArgsConstructor` where applicable"; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getDescription() { |
| 45 | + //language=markdown |
| 46 | + return "Prefer the Lombok `@NoArgsConstructor` annotation over explicitly written out constructors."; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 51 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 52 | + @Override |
| 53 | + public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 54 | + if (method.isConstructor() && |
| 55 | + method.getParameters().get(0) instanceof J.Empty && |
| 56 | + method.getBody() != null && method.getBody().getStatements().isEmpty()) { |
| 57 | + J.ClassDeclaration enclosing = getCursor().firstEnclosing(J.ClassDeclaration.class); |
| 58 | + AccessLevel accessLevel = LombokUtils.getAccessLevel(method); |
| 59 | + doAfterVisit(new JavaIsoVisitor<ExecutionContext>() { |
| 60 | + @Override |
| 61 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 62 | + if (TypeUtils.isOfType(classDecl.getType(), enclosing.getType())) { |
| 63 | + String template = "@NoArgsConstructor" + (accessLevel == AccessLevel.PUBLIC ? |
| 64 | + "" : "(access = AccessLevel." + accessLevel.name() + ")"); |
| 65 | + maybeAddImport("lombok.AccessLevel"); |
| 66 | + maybeAddImport("lombok.NoArgsConstructor"); |
| 67 | + return JavaTemplate.builder(template) |
| 68 | + .imports("lombok.*") |
| 69 | + .javaParser(JavaParser.fromJavaVersion().classpath("lombok")) |
| 70 | + .build() |
| 71 | + .apply(getCursor(), classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 72 | + } |
| 73 | + return super.visitClassDeclaration(classDecl, ctx); |
| 74 | + } |
| 75 | + }); |
| 76 | + return null; |
| 77 | + } |
| 78 | + return super.visitMethodDeclaration(method, ctx); |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
0 commit comments