-
Notifications
You must be signed in to change notification settings - Fork 96
feat: convert any equals and hashcode to the lombok annotation, ignore implementation #647
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
Draft
timo-a
wants to merge
8
commits into
openrewrite:main
Choose a base branch
from
timo-a:lombok/convert-equals-negligently
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d258000
migrate recipe as-is
timo-a 4db9a72
add test for empty class
timo-a b2206b6
Apply suggestions from code review
timtebeek 51ffbc2
fix suggestions from reviewbot
timo-a fc54dce
Merge branch 'main' into lombok/convert-equals-negligently
timtebeek 6eee968
Merge branch 'main' into lombok/convert-equals-negligently
timtebeek 7f46b72
Merge branch 'main' into lombok/convert-equals-negligently
timtebeek 4cd463d
Merge branch 'main' into lombok/convert-equals-negligently
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
101 changes: 101 additions & 0 deletions
101
src/main/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEquals.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,101 @@ | ||
/* | ||
* 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.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.MethodMatcher; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import static java.util.Comparator.comparing; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class NegligentlyConvertEquals extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
//language=markdown | ||
return "Replace any custom `equals` or `hashCode` methods with the `EqualsAndHashCode` annotation"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
//language=markdown | ||
return "This recipe substitutes a class level `@EqualsAndHashCode` annotation for a custom `equals` or `hashCode` methods. " + | ||
"If both are defined, then both will be replaced. If only one is defined then it will be replaced. " + | ||
"This recipe does not check if the custom `equals` or `hashCode` methods behave like ones generated by the lombok annotation. " + | ||
"Doing so is considered infeasible at this time. " + | ||
"As a compromise this recipe finds and replaces the custom methods and relies on the user to review the changes closely. " + | ||
"As a consequence this recipe is VERY DANGEROUS to include into a composite recipe! " + | ||
"Users are advised to run it only in isolation."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Converter(); | ||
} | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
private static class Converter extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
MethodMatcher equalsMatcher = new MethodMatcher("* equals(Object)"); | ||
MethodMatcher hashCodeMatcher = new MethodMatcher("* hashCode()"); | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
|
||
J.ClassDeclaration classDeclAfterVisit = super.visitClassDeclaration(classDecl, ctx); | ||
|
||
//only thing that can have changed is removal of either equals or hash code | ||
//and something needs to have changed before we add an annotation at class level | ||
if (classDeclAfterVisit != classDecl) { | ||
maybeAddImport("lombok.EqualsAndHashCode"); | ||
|
||
//Add annotation | ||
JavaTemplate template = JavaTemplate.builder("@EqualsAndHashCode\n") | ||
.imports("lombok.EqualsAndHashCode") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("lombok")) | ||
.build(); | ||
|
||
return template.apply( | ||
updateCursor(classDeclAfterVisit), | ||
classDeclAfterVisit.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); | ||
} | ||
return classDecl; | ||
} | ||
|
||
@Override | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
J.ClassDeclaration classDecl = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); | ||
|
||
// The enclosing class of a J.MethodDeclaration must be known for a MethodMatcher to match it | ||
if (equalsMatcher.matches(method, classDecl) || hashCodeMatcher.matches(method, classDecl)) { | ||
return null; | ||
} else { | ||
return method; | ||
} | ||
} | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.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,181 @@ | ||
/* | ||
* 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; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class NegligentlyConvertEqualsTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new NegligentlyConvertEquals()) | ||
.parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true).classpath("lombok")); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void replaceEquals() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
class A { | ||
|
||
int foo; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return false; | ||
} | ||
} | ||
""", | ||
""" | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode | ||
class A { | ||
|
||
int foo; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noCostomMethodsNoAnnotation() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
class A { | ||
|
||
int foo; | ||
|
||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceEqualsInPackage() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
package com.example; | ||
|
||
class A { | ||
|
||
int foo; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return false; | ||
} | ||
} | ||
""", | ||
""" | ||
package com.example; | ||
|
||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode | ||
class A { | ||
|
||
int foo; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceHashCode() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
package com.example; | ||
|
||
class A { | ||
|
||
int foo; | ||
|
||
@Override | ||
public int hashCode() { | ||
return 6; | ||
} | ||
} | ||
""", | ||
""" | ||
package com.example; | ||
|
||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode | ||
class A { | ||
|
||
int foo; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void replaceEqualsAndHashCode() { | ||
rewriteRun(// language=java | ||
java( | ||
""" | ||
package com.example; | ||
|
||
class A { | ||
|
||
int foo; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 6; | ||
} | ||
|
||
} | ||
""", | ||
""" | ||
package com.example; | ||
|
||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode | ||
class A { | ||
|
||
int foo; | ||
|
||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
} |
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.