-
Notifications
You must be signed in to change notification settings - Fork 80
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]: feat: normalize effective setter methods #632
Draft
timo-a
wants to merge
12
commits into
openrewrite:main
Choose a base branch
from
timo-a:lombok/normalize-setter
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.
+824
−0
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
baa552f
migrate recipes as-is
timo-a 2077e75
fix year in license
timo-a a752e8b
bring back original helper methods
timo-a aec0da6
minor polish
timo-a 2749ebb
remove line in recipe spec
timo-a 2716b85
Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.…
timo-a 2e04ef6
Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.…
timo-a 44b7aa6
Update src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSet…
timo-a 73e78a7
fix build
timo-a 1024a1e
fix build
timo-a 6789157
Merge branch 'main' into lombok/normalize-setter
timtebeek 52dd329
Merge branch 'main' into lombok/normalize-setter
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
This file contains 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
175 changes: 175 additions & 0 deletions
175
src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java
This file contains 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,175 @@ | ||
/* | ||
* 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.RequiredArgsConstructor; | ||
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.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.StringJoiner; | ||
import java.util.stream.Collectors; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class NormalizeSetter extends ScanningRecipe<NormalizeSetter.MethodAcc> { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Rename setter methods to fit lombok"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
//language=markdown | ||
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." + | ||
" - If the correct name for a method is already taken by another method then the name will not be corrected." + | ||
" - Method name swaps or circular renaming within a class cannot be performed because the names block each other. " + | ||
"E.g. `int getFoo() { return ba; } int getBa() { return foo; }` stays as it is."; | ||
} | ||
|
||
public static class MethodAcc { | ||
List<RenameRecord> renameRecords = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public MethodAcc getInitialValue(ExecutionContext ctx) { | ||
return new MethodAcc(); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getScanner(MethodAcc acc) { | ||
return new MethodRecorder(acc); | ||
} | ||
|
||
@Value | ||
private static class RenameRecord { | ||
String pathToClass_; | ||
String methodName_; | ||
String parameterType_; | ||
String newMethodName_; | ||
} | ||
|
||
|
||
@RequiredArgsConstructor | ||
private static class MethodRecorder extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
private final static String METHOD_BLACKLIST = "METHOD_BLACKLIST"; | ||
|
||
private final MethodAcc acc; | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
|
||
List<String> blackList = classDecl.getBody().getStatements().stream() | ||
.filter(s -> s instanceof J.MethodDeclaration) | ||
.map(s -> (J.MethodDeclaration) s) | ||
.map(J.MethodDeclaration::getSimpleName) | ||
.collect(Collectors.toList()); | ||
|
||
getCursor().putMessage(METHOD_BLACKLIST, blackList); | ||
|
||
super.visitClassDeclaration(classDecl, ctx); | ||
|
||
return classDecl; | ||
} | ||
|
||
@Override | ||
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
assert method.getMethodType() != null; | ||
|
||
if (!LombokUtils.isEffectivelySetter(method)) { | ||
return method; | ||
} | ||
|
||
//return early if the method overrides another | ||
//if the project defined both the original and the overridden method, | ||
// then the renaming of the "original" in the base class will cover the override | ||
if (method.getLeadingAnnotations().stream().anyMatch(a -> "Override".equals(a.getSimpleName()))) { | ||
return method; | ||
} | ||
|
||
J.Assignment assignment_ = (J.Assignment) method.getBody().getStatements().get(0); | ||
J.FieldAccess fieldAccess = (J.FieldAccess) assignment_.getVariable(); | ||
JavaType.Variable fieldType = fieldAccess.getName().getFieldType(); | ||
|
||
String expectedMethodName = LombokUtils.deriveSetterMethodName(fieldType); | ||
String parameterType = fieldType.getType().toString(); | ||
String actualMethodName = method.getSimpleName(); | ||
|
||
//if method already has the name it should have, then nothing to be done | ||
if (expectedMethodName.equals(actualMethodName)) { | ||
return method; | ||
} | ||
|
||
//If the desired method name is already taken by an existing method, the current method cannot be renamed | ||
List<String> blackList = getCursor().getNearestMessage(METHOD_BLACKLIST); | ||
assert blackList != null; | ||
if (blackList.contains(expectedMethodName)) { | ||
return method; | ||
} | ||
//WON'T DO: there is a rare edge case, that is not addressed yet. | ||
// If `getFoo()` returns `ba` and `getBa()` returns `foo` then neither will be renamed. | ||
// This could be fixed by compiling a list of planned changes and doing a soundness check (and not renaming sequentially, or rather introducing temporary method names) | ||
// At this point I don't think it's worth the effort. | ||
|
||
|
||
String pathToClass = method.getMethodType().getDeclaringType().getFullyQualifiedName().replace('$', '.'); | ||
//todo write separate recipe for merging effective setters | ||
acc.renameRecords.add( | ||
new RenameRecord( | ||
pathToClass, | ||
actualMethodName, | ||
parameterType, | ||
expectedMethodName | ||
) | ||
); | ||
blackList.remove(actualMethodName);//actual method name becomes available again | ||
blackList.add(expectedMethodName);//expected method name now blocked | ||
return method; | ||
} | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor(MethodAcc acc) { | ||
|
||
return new TreeVisitor<Tree, ExecutionContext>() { | ||
|
||
@Override | ||
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
|
||
for (RenameRecord rr : acc.renameRecords) { | ||
String methodPattern = String.format("%s %s(%s)", rr.pathToClass_, rr.methodName_, rr.parameterType_); | ||
tree = new ChangeMethodName(methodPattern, rr.newMethodName_, true, null) | ||
.getVisitor().visit(tree, ctx); | ||
} | ||
return tree; | ||
} | ||
}; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.