|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0 |
| 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.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.JavaIsoVisitor; |
| 25 | +import org.openrewrite.java.JavaParser; |
| 26 | +import org.openrewrite.java.JavaTemplate; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | + |
| 29 | +import static java.util.Comparator.comparing; |
| 30 | + |
| 31 | +@Value |
| 32 | +@EqualsAndHashCode(callSuper = false) |
| 33 | +public class UseNoArgsConstructor extends Recipe { |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getDisplayName() { |
| 37 | + //language=markdown |
| 38 | + return "Use `@NoArgsConstructor` where applicable"; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public String getDescription() { |
| 43 | + //language=markdown |
| 44 | + return "Prefer the lombok annotation `@NoArgsConstructor` over explicitly written out constructors.\n" + |
| 45 | + "This recipe does not create annotations for implicit constructors."; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 50 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 51 | + public static final String FOUND_EMPTY_CONSTRUCTOR = "FOUND_EMPTY_CONSTRUCTOR"; |
| 52 | + |
| 53 | + |
| 54 | + @Override |
| 55 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 56 | + J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, ctx); |
| 57 | + |
| 58 | + J.MethodDeclaration message = getCursor().pollMessage(FOUND_EMPTY_CONSTRUCTOR); |
| 59 | + |
| 60 | + //if no constructor is found return immediately |
| 61 | + if (message == null) { |
| 62 | + return classDecl;//since nothing changed the original can be returned |
| 63 | + } |
| 64 | + |
| 65 | + maybeAddImport("lombok.NoArgsConstructor"); |
| 66 | + |
| 67 | + AccessLevel accessLevel = LombokUtils.getAccessLevel(message); |
| 68 | + |
| 69 | + return getAnnotation(accessLevel).apply( |
| 70 | + updateCursor(classDeclAfterVisit), |
| 71 | + classDeclAfterVisit.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 72 | + } |
| 73 | + |
| 74 | + private JavaTemplate getAnnotation(AccessLevel accessLevel) { |
| 75 | + |
| 76 | + JavaTemplate.Builder builder = AccessLevel.PUBLIC.equals(accessLevel) |
| 77 | + ? JavaTemplate.builder("@NoArgsConstructor()\n") |
| 78 | + : JavaTemplate.builder("@NoArgsConstructor(access = AccessLevel." + accessLevel.name() + ")\n") |
| 79 | + .imports("lombok.AccessLevel"); |
| 80 | + |
| 81 | + return builder |
| 82 | + .imports("lombok.NoArgsConstructor") |
| 83 | + .javaParser(JavaParser.fromJavaVersion() |
| 84 | + .classpath("lombok")) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 90 | + assert method.getMethodType() != null; |
| 91 | + if (method.getMethodType().getName().equals("<constructor>") //it's a constructor |
| 92 | + && method.getParameters().get(0) instanceof J.Empty //no parameters |
| 93 | + && method.getBody().getStatements().isEmpty() //no side effects (=> does nothing) |
| 94 | + ) { |
| 95 | + getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, FOUND_EMPTY_CONSTRUCTOR, method); |
| 96 | + return null; |
| 97 | + } |
| 98 | + return super.visitMethodDeclaration(method, ctx); |
| 99 | + } |
| 100 | + |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments