Skip to content

Commit 080aa67

Browse files
committed
UsegetTypesInUse().getDeclaredMethods()
1 parent e99aa5f commit 080aa67

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import java.util.ArrayList;
3333
import java.util.List;
34-
import java.util.stream.Collectors;
34+
import java.util.Set;
3535

3636
@Value
3737
@EqualsAndHashCode(callSuper = false)
@@ -61,8 +61,7 @@ public static class MethodAcc {
6161
@Value
6262
private static class RenameRecord {
6363
String methodPattern;
64-
String parameterType_;
65-
String newMethodName_;
64+
String newMethodName;
6665
}
6766

6867
@Override
@@ -73,21 +72,16 @@ public MethodAcc getInitialValue(ExecutionContext ctx) {
7372
@Override
7473
public TreeVisitor<?, ExecutionContext> getScanner(MethodAcc acc) {
7574
return new JavaIsoVisitor<ExecutionContext>() {
76-
7775
@Override
78-
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
79-
80-
List<String> doNotRename = classDecl.getBody().getStatements().stream()
81-
.filter(s -> s instanceof J.MethodDeclaration)
82-
.map(s -> (J.MethodDeclaration) s)
83-
.map(J.MethodDeclaration::getSimpleName)
84-
.collect(Collectors.toList());
85-
86-
getCursor().putMessage(DO_NOT_RENAME, doNotRename);
87-
88-
super.visitClassDeclaration(classDecl, ctx);
89-
90-
return classDecl;
76+
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
77+
// Cheaply collect all declared methods; this also means we do not support clashing nested class methods
78+
Set<JavaType.Method> declaredMethods = cu.getTypesInUse().getDeclaredMethods();
79+
List<String> existingMethodNames = new ArrayList<>();
80+
for (JavaType.Method method : declaredMethods) {
81+
existingMethodNames.add(method.getName());
82+
}
83+
getCursor().putMessage(DO_NOT_RENAME, existingMethodNames);
84+
return super.visitCompilationUnit(cu, ctx);
9185
}
9286

9387
@Override
@@ -101,11 +95,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
10195
JavaType.Variable fieldType = extractVariable(method);
10296

10397
String expectedMethodName = LombokUtils.deriveSetterMethodName(fieldType);
104-
String parameterType = fieldType.getType().toString();
105-
String actualMethodName = method.getSimpleName();
106-
10798
// If method already has the name it should have, then nothing to be done
108-
if (expectedMethodName.equals(actualMethodName)) {
99+
if (expectedMethodName.equals(method.getSimpleName())) {
109100
return method;
110101
}
111102

@@ -115,23 +106,10 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
115106
if (doNotRename.contains(expectedMethodName)) {
116107
return method;
117108
}
118-
//WON'T DO: there is a rare edge case, that is not addressed yet.
119-
// If `getFoo()` returns `ba` and `getBa()` returns `foo` then neither will be renamed.
120-
// 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)
121-
// At this point I don't think it's worth the effort.
122-
123-
124-
String pathToClass = method.getMethodType().getDeclaringType().getFullyQualifiedName().replace('$', '.');
125-
//todo write separate recipe for merging effective setters
126-
acc.renameRecords.add(
127-
new RenameRecord(
128-
MethodMatcher.methodPattern(method),
129-
parameterType,
130-
expectedMethodName
131-
)
132-
);
133-
doNotRename.remove(actualMethodName);//actual method name becomes available again
134-
doNotRename.add(expectedMethodName);//expected method name now blocked
109+
110+
acc.renameRecords.add(new RenameRecord(MethodMatcher.methodPattern(method), expectedMethodName));
111+
doNotRename.remove(method.getSimpleName()); //actual method name becomes available again
112+
doNotRename.add(expectedMethodName); //expected method name now blocked
135113
return method;
136114
}
137115

@@ -160,7 +138,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(MethodAcc acc) {
160138
@Override
161139
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
162140
for (RenameRecord rr : acc.renameRecords) {
163-
tree = new ChangeMethodName(rr.methodPattern, rr.newMethodName_, true, null)
141+
tree = new ChangeMethodName(rr.methodPattern, rr.newMethodName, true, null)
164142
.getVisitor().visit(tree, ctx);
165143
}
166144
return tree;

src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Nested;
1919
import org.junit.jupiter.api.Test;
20+
import org.junitpioneer.jupiter.ExpectedToFail;
2021
import org.openrewrite.DocumentExample;
2122
import org.openrewrite.test.RecipeSpec;
2223
import org.openrewrite.test.RewriteTest;
@@ -382,6 +383,7 @@ public void setFoo(long foo) {
382383
/**
383384
* Methods on top level should be renamed just as well when there is an inner class.
384385
*/
386+
@ExpectedToFail("We use `cu.getTypesInUse().getDeclaredMethods()` as a performance optimization to avoid visits")
385387
@Test
386388
void shouldWorkDespiteInnerClassesSameNameMethods() {
387389
rewriteRun(// language=java

0 commit comments

Comments
 (0)