-
Notifications
You must be signed in to change notification settings - Fork 99
feat: use lombok @NoArgsConstructor annotation #645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
52339a8
migrate recipe as-is
timo-a 3652d06
Apply suggestions from code review
timtebeek fe25d11
Merge branch 'main' into lombok/noargsconstructor
timtebeek 35494ea
Minimize implementation
timtebeek 5bc0325
Add missing AccessLevel imports
timtebeek 50c2375
Remove unused import
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/main/java/org/openrewrite/java/migrate/lombok/UseNoArgsConstructor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <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; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.J; | ||
|
||
import static java.util.Comparator.comparing; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UseNoArgsConstructor extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
//language=markdown | ||
return "Use `@NoArgsConstructor` where applicable"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
//language=markdown | ||
return "Prefer the lombok annotation `@NoArgsConstructor` over explicitly written out constructors.\n" + | ||
"This recipe does not create annotations for implicit constructors."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
public static final String FOUND_EMPTY_CONSTRUCTOR = "FOUND_EMPTY_CONSTRUCTOR"; | ||
|
||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, ctx); | ||
|
||
J.MethodDeclaration message = getCursor().pollMessage(FOUND_EMPTY_CONSTRUCTOR); | ||
|
||
//if no constructor is found return immediately | ||
if (message == null) { | ||
return classDecl;//since nothing changed the original can be returned | ||
} | ||
|
||
maybeAddImport("lombok.NoArgsConstructor"); | ||
|
||
AccessLevel accessLevel = LombokUtils.getAccessLevel(message); | ||
|
||
return getAnnotation(accessLevel).apply( | ||
updateCursor(classDeclAfterVisit), | ||
classDeclAfterVisit.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
} | ||
|
||
private JavaTemplate getAnnotation(AccessLevel accessLevel) { | ||
|
||
JavaTemplate.Builder builder = AccessLevel.PUBLIC.equals(accessLevel) ? | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
JavaTemplate.builder("@NoArgsConstructor()\n") : | ||
JavaTemplate.builder("@NoArgsConstructor(access = AccessLevel." + accessLevel.name() + ")\n") | ||
.imports("lombok.AccessLevel"); | ||
|
||
return builder | ||
.imports("lombok.NoArgsConstructor") | ||
.javaParser(JavaParser.fromJavaVersion() | ||
.classpath("lombok")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
assert method.getMethodType() != null; | ||
if (method.getMethodType().getName().equals("<constructor>") //it's a constructor | ||
&& method.getParameters().get(0) instanceof J.Empty //no parameters | ||
&& method.getBody().getStatements().isEmpty() //no side effects (=> does nothing) | ||
) { | ||
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, FOUND_EMPTY_CONSTRUCTOR, method); | ||
return null; | ||
} | ||
return super.visitMethodDeclaration(method, ctx); | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
src/test/java/org/openrewrite/java/migrate/lombok/UseNoArgsConstructorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <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 org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class UseNoArgsConstructorTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new UseNoArgsConstructor()) | ||
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void replaceEmptyPublicConstructor() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
class A { | ||
public A() {} | ||
} | ||
""", | ||
""" | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor() | ||
class A { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void keepNonEmptyPublicConstructor() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class A { | ||
|
||
int foo; | ||
|
||
public A() { | ||
foo = 7; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceEmptyProtectedConstructor() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class A { | ||
protected A() {} | ||
} | ||
""", | ||
""" | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
class A { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceEmptyPrivateConstructor() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class A { | ||
private A() {} | ||
} | ||
""", | ||
""" | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
class A { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceEmptyPackageConstructor() { | ||
rewriteRun( | ||
java( | ||
""" | ||
class A { | ||
A() {} | ||
} | ||
""", | ||
""" | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PACKAGE) | ||
class A { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.