-
Notifications
You must be signed in to change notification settings - Fork 109
New recipe to adopt Lombok setter method names #632
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 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
cbd4deb
migrate recipes as-is
timo-a eb2ccca
fix year in license
timo-a 8d8ad1d
bring back original helper methods
timo-a 316657f
minor polish
timo-a 28b76ff
remove line in recipe spec
timo-a 04a78bc
Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.…
timo-a ccf978d
Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.…
timo-a daecba7
Update src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSet…
timo-a 45be826
fix build
timo-a 783df6d
fix build
timo-a 28946ff
markdown and comments
timo-a a1fab56
support no field access
timo-a 772ccba
Inline MethodRecorder & rename list of methods not renamed
timo-a dfbcb60
Remove unnecessary packages
timo-a 23a98d3
Capture current behavior with a running test
timo-a 29cf350
fix test
timo-a 73f2732
Apply suggestions from code review (of getter pr)
timo-a ca9f973
Group the tests that expect no change
timo-a 31659ef
Use early returns in `isEffectivelySetter`
timo-a 37be475
Adopt `TypeUtils.isOverride`
timo-a e99aa5f
Use MethodMatcher.methodPattern(method)
timo-a 080aa67
Use`getTypesInUse().getDeclaredMethods()`
timo-a ee573ca
Expect to fail for a case not covered yet
timo-a cc6e71f
Inline MethodAcc
timo-a 54502e8
refactor: rename to sth. less ambiguous
timo-a afa84e7
Merge branch 'main' into lombok/normalize-setter
timtebeek 00c3727
Update src/main/java/org/openrewrite/java/migrate/lombok/AdoptLombokS…
timtebeek 4fc323a
Minimize changes with Getter equivalent
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
144 changes: 144 additions & 0 deletions
144
src/main/java/org/openrewrite/java/migrate/lombok/AdoptLombokSetterMethodNames.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,144 @@ | ||
| /* | ||
| * 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; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.ScanningRecipe; | ||
| import org.openrewrite.Tree; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.ChangeMethodName; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class AdoptLombokSetterMethodNames extends ScanningRecipe<List<AdoptLombokSetterMethodNames.RenameRecord>> { | ||
|
|
||
| private final static String DO_NOT_RENAME = "DO_NOT_RENAME"; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Rename setter methods to fit Lombok"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Rename methods that are effectively setter to the name Lombok would give them.\n" + | ||
| "Limitations:\n" + | ||
| " - If two methods in a class are effectively the same setter then one's name will be corrected and the others name will be left as it is.\n" + | ||
| " - If the correct name for a method is already taken by another method then the name will not be corrected.\n" + | ||
| " - Method name swaps or circular renaming within a class cannot be performed because the names block each other.\n " + | ||
| "E.g. `int getFoo() { return ba; } int getBa() { return foo; }` stays as it is."; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Value | ||
| public static class RenameRecord { | ||
| String methodPattern; | ||
| String newMethodName; | ||
| } | ||
|
|
||
| @Override | ||
| public List<RenameRecord> getInitialValue(ExecutionContext ctx) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getScanner(List<RenameRecord> renameRecords) { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { | ||
| // Cheaply collect all declared methods; this also means we do not support clashing nested class methods | ||
| Set<JavaType.Method> declaredMethods = cu.getTypesInUse().getDeclaredMethods(); | ||
| List<String> existingMethodNames = new ArrayList<>(); | ||
| for (JavaType.Method method : declaredMethods) { | ||
| existingMethodNames.add(method.getName()); | ||
| } | ||
| getCursor().putMessage(DO_NOT_RENAME, existingMethodNames); | ||
| return super.visitCompilationUnit(cu, ctx); | ||
| } | ||
|
|
||
| @Override | ||
| public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
| if (method.getMethodType() == null || method.getBody() == null || | ||
| !LombokUtils.isEffectivelySetter(method) || | ||
| TypeUtils.isOverride(method.getMethodType())) { | ||
| return method; | ||
| } | ||
|
|
||
| JavaType.Variable fieldType = extractVariable(method); | ||
|
|
||
| String expectedMethodName = LombokUtils.deriveSetterMethodName(fieldType); | ||
| // If method already has the name it should have, then nothing to be done | ||
| if (expectedMethodName.equals(method.getSimpleName())) { | ||
| return method; | ||
| } | ||
|
|
||
| // If the desired method name is already taken by an existing method, the current method cannot be renamed | ||
| List<String> doNotRename = getCursor().getNearestMessage(DO_NOT_RENAME); | ||
| assert doNotRename != null; | ||
| if (doNotRename.contains(expectedMethodName)) { | ||
| return method; | ||
| } | ||
|
|
||
| renameRecords.add(new RenameRecord(MethodMatcher.methodPattern(method), expectedMethodName)); | ||
| doNotRename.remove(method.getSimpleName()); //actual method name becomes available again | ||
| doNotRename.add(expectedMethodName); //expected method name now blocked | ||
| return method; | ||
| } | ||
|
|
||
| private JavaType.@Nullable Variable extractVariable(J.MethodDeclaration method) { | ||
| J.Assignment assignment_ = (J.Assignment) method.getBody().getStatements().get(0); | ||
|
|
||
| JavaType.Variable fieldType; | ||
| if (assignment_.getVariable() instanceof J.FieldAccess) { | ||
| J.FieldAccess fieldAccess = (J.FieldAccess) assignment_.getVariable(); | ||
| fieldType = fieldAccess.getName().getFieldType(); | ||
| } else if (assignment_.getVariable() instanceof J.Identifier) { | ||
| J.Identifier fieldAccess = (J.Identifier) assignment_.getVariable(); | ||
| fieldType = fieldAccess.getFieldType(); | ||
| } else { | ||
| //only those types above are possible, see LombokUtils::isEffectivelySetter | ||
| throw new IllegalStateException("Unexpected type for returned variable"); | ||
| } | ||
| return fieldType; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor(List<RenameRecord> renameRecords) { | ||
| return new TreeVisitor<Tree, ExecutionContext>() { | ||
| @Override | ||
| public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
| for (RenameRecord rr : renameRecords) { | ||
| tree = new ChangeMethodName(rr.methodPattern, rr.newMethodName, true, null) | ||
| .getVisitor().visit(tree, ctx); | ||
| } | ||
| return tree; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.