From baa552f3b15820bc4d05c9f12477ec73bf818331 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:06:25 +0100 Subject: [PATCH 01/10] migrate recipes as-is --- .../java/migrate/lombok/NormalizeSetter.java | 180 +++++ .../migrate/lombok/NormalizeSetterTest.java | 671 ++++++++++++++++++ 2 files changed, 851 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java create mode 100644 src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java new file mode 100644 index 000000000..b80e58f3d --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java @@ -0,0 +1,180 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 { + + @Override + public String getDisplayName() { + //language=markdown + return "Rename setter methods to fit lombok"; + } + + @Override + public String getDescription() { + //language=markdown + return new StringJoiner("\n") + .add("Rename methods that are effectively setter to the name lombok would give them.") + .add("") + .add("Limitations:") + .add("") + .add(" - 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.") + .add(" - If the correct name for a method is already taken by another method then the name will not be corrected.") + .add(" - Method name swaps or circular renaming within a class cannot be performed because the names block each other. ") + .add("E.g. `int getFoo() { return ba; } int getBa() { return foo; }` stays as it is.") + .toString(); + } + + public static class MethodAcc { + List renameRecords = new ArrayList<>(); + } + + @Override + public MethodAcc getInitialValue(ExecutionContext ctx) { + return new MethodAcc(); + } + + @Override + public TreeVisitor 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 { + + private final static String METHOD_BLACKLIST = "METHOD_BLACKLIST"; + + private final MethodAcc acc; + + @Override + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { + + List 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 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 getVisitor(MethodAcc acc) { + + return new TreeVisitor() { + + @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; + } + }; + } +} diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java new file mode 100644 index 000000000..edc61f761 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java @@ -0,0 +1,671 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.Disabled; +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.ChangeMethodName; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class NormalizeSetterTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new NormalizeSetter()) + .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); + } + + @DocumentExample + + @Test + void applyDirectly() {//TODO remove again + rewriteRun( + spec -> spec + .recipe(new ChangeMethodName("com.yourorg.whatever.A giveFoo()", "getFoo", null, null)) + .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)), + // language=java + java( + """ + package com.yourorg.whatever; + class A { + int foo = 9; + int giveFoo() { return foo; } + } + """, + """ + package com.yourorg.whatever; + class A { + int foo = 9; + int getFoo() { return foo; } + } + """ + ) + ); + } + + @Test + void applyDirectlyOnSetter() {//TODO remove again + rewriteRun( + spec -> spec + .recipe(new ChangeMethodName("com.yourorg.whatever.A storeFoo(int)", "setFoo", null, null)) + .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)), + // language=java + java( + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void storeFoo(int foo) { + this.foo = foo; + } + } + """, + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void setFoo(int foo) { + this.foo = foo; + } + } + """ + ) + ); + } + + + + @Test + void renameInSingleClass() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void storeFoo(int foo) { + this.foo = foo; + } + } + """, + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void setFoo(int foo) { + this.foo = foo; + } + } + """ + ) + ); + } + + @Test + void renameInSingleClassWhitespace() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void storeFoo( int foo ) { + this .foo = foo; + } + } + """, + """ + package com.yourorg.whatever; + class A { + int foo = 9; + public void setFoo( int foo ) { + this .foo = foo; + } + } + """ + ) + ); + } + + @Test + void renamePrimitiveBooleanInSingleClass() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + boolean foo; + void storeFoo(boolean foo) { this.foo = foo; } + } + """, + """ + package com.yourorg.whatever; + class A { + boolean foo; + void setFoo(boolean foo) { this.foo = foo; } + } + """ + ) + ); + } + + @Test + void renameClassBooleanInSingleClass() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + Boolean foo; + void storeFoo(Boolean foo) { this.foo = foo; } + } + """, + """ + package com.yourorg.whatever; + class A { + Boolean foo; + void setFoo(Boolean foo) { this.foo = foo; } + } + """ + ) + ); + } + + @Test + void noBoxing1() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + Boolean Foo; + void storeFoo(boolean foo) { this.foo = foo; } + } + """ + ) + ); + } + + @Test + void noBoxing2() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + boolean Foo; + void storeFoo(Boolean foo) { this.foo = foo; } + } + """ + ) + ); + } + + @Test + void renameAcrossClasses() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + class A { + int foo = 9; + void storeFoo(int foo) { this.foo = foo; } + } + """, + """ + package com.yourorg.whatever; + class A { + int foo = 9; + void setFoo(int foo) { this.foo = foo; } + } + """ + ),// language=java + java( + """ + package com.yourorg.whatever; + class B { + void useIt() { + var a = new A(); + a.storeFoo(4); + } + } + """, + """ + package com.yourorg.whatever; + class B { + void useIt() { + var a = new A(); + a.setFoo(4); + } + } + """ + ) + ); + } + + @Test + void shouldNotChangeOverridesOfExternalMethods() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + import java.util.Date; + + class A extends Date { + + private long foo; + + @Override + public long setTime(long time) { + this.foo = time; + } + } + """ + ) + ); + } + + @Test + void withoutPackage() { + rewriteRun(// language=java + java( + """ + class A { + + private long foo; + + public void setTime(long foo) { + this.foo = foo; + } + } + """, + """ + class A { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + } + """ + ) + ); + } + + @Test + void shouldChangeOverridesOfInternalMethods() { + rewriteRun(// language=java + java( + """ + class A { + + private long foo; + + public void setTime(long foo) { + this.foo = foo; + } + } + """, + """ + class A { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + } + """ + ),// language=java + java( + """ + class B extends A { + + @Override + public void setTime(long foo) { + } + } + """, + """ + class B extends A { + + @Override + public void setFoo(long foo) { + } + } + """ + ) + ); + } + + @Test + void shouldNotRenameToExistingMethods() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void setTime(long foo) { + this.foo = foo; + } + + public void setFoo(long foo) { + } + } + """ + ) + ); + } + + /** + * If two methods are effectively the same setter then only one can be renamed. + * Renaming both would result in a duplicate method definition, so we cannot do this. + * Ideally the other effective setter would have their usages renamed but be themselves deleted... + * TODO: create a second cleanup recipe that identifies redundant Setters (isEffectiveSetter + field already has the setter annotation) + * and redirects their usage (ChangeMethodName with both flags true) and then deletes them. + */ + @Test + void shouldNotRenameTwoToTheSame() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void firstToBeRenamed(long foo) { + this.foo = foo; + } + + public void secondToBeRenamed(long foo) { + this.foo = foo; + } + } + """, + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + + public void secondToBeRenamed(long foo) { + this.foo = foo; + } + } + """ + ) + ); + } + + /** + * Methods in inner classes should be renamed as well. + */ + @Test + void shouldWorkOnInnerClasses() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + class B { + + private long foo; + + public void storeFoo(long foo) { + this.foo = foo; + } + } + } + """, + """ + package com.yourorg.whatever; + + class A { + + class B { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + } + } + """ + ) + ); + } + + @Test + void shouldWorkOnInnerClasses2() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + class B { + + class C { + + private long foo; + + public void giveFoo(long foo) { + this.foo = foo; + } + }} + } + """, + """ + package com.yourorg.whatever; + + class A { + + class B { + + class C { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + }} + } + """ + ) + ); + } + + /** + * Methods on top level should be renamed just as well when there is an inner class. + */ + @Test + void shouldWorkDespiteInnerClassesSameNameMethods() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void storeFoo(long foo) { + this.foo = foo; + } + + class B { + + private long foo; + + public void storeFoo(long foo) { + this.foo = foo; + } + } + } + """, + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + + class B { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + } + } + """ + ) + ); + } + + /** + * Methods on top level should be renamed just as well when there is an inner class. + */ + @Test + void shouldWorkDespiteInnerClassesDifferentNameMethods() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void storeFoo(long foo) { + this.foo = foo; + } + + class B { + + private long ba; + + public void storeBa(long ba) { + this.ba = ba; + } + } + } + """, + """ + package com.yourorg.whatever; + + class A { + + private long foo; + + public void setFoo(long foo) { + this.foo = foo; + } + + class B { + + private long ba; + + public void setBa(long ba) { + this.ba = ba; + } + } + } + """ + ) + ); + } + + /** + * If existing method names need to be rotated in a loop the recipe should still work. + * For now this is not planned. + */ + @Disabled("Not planned to fix but listed here for completeness") + @Test + void shouldWorkOnCircleCases() { + rewriteRun(// language=java + java( + """ + package com.yourorg.whatever; + + class A { + + int foo; + + int bar; + + public void setBar(long bar) { + this.foo = bar; + } + + public void getFoo(long foo) { + this.bar = foo; + } + + } + """, + """ + package com.yourorg.whatever; + + class A { + + int foo; + + int bar; + + public void getFoo(long foo) { + this.foo = foo; + } + + public void setBar(long bar) { + this.bar = bar; + } + + } + """ + ) + ); + } + +} From 2077e75a97ef7d9ef676a866e412b762c9e80154 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:11:20 +0100 Subject: [PATCH 02/10] fix year in license --- .../org/openrewrite/java/migrate/lombok/NormalizeSetter.java | 2 +- .../openrewrite/java/migrate/lombok/NormalizeSetterTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java index b80e58f3d..4f8de64d3 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java index edc61f761..24bb1b6d7 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From a752e8b4d1f4654694de4b41b5339eb6ca504926 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:11:51 +0100 Subject: [PATCH 03/10] bring back original helper methods --- .../java/migrate/lombok/LombokUtils.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java index a397bf904..454147b9a 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -21,6 +21,10 @@ import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.Statement; + +import java.util.List; +import java.util.stream.Collectors; import static lombok.AccessLevel.*; import static org.openrewrite.java.tree.J.Modifier.Type.*; @@ -131,6 +135,43 @@ private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, S return method.getSimpleName().equals("set" + StringUtils.capitalize(simpleName)); } + public static boolean isEffectivelySetter(J.MethodDeclaration method) { + boolean isVoid = "void".equals(method.getType().toString()); + List actualParameters = method.getParameters().stream() + .filter(s -> !(s instanceof J.Empty)) + .collect(Collectors.toList()); + boolean oneParam = actualParameters.size() == 1; + if (!isVoid || !oneParam) + return false; + + J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) actualParameters.get(0); + J.VariableDeclarations.NamedVariable param = variableDeclarations.getVariables().get(0); + String paramName = param.getName().toString(); + + boolean singularStatement = method.getBody() != null //abstract methods can be null + && method.getBody().getStatements().size() == 1 + && method.getBody().getStatements().get(0) instanceof J.Assignment; + + if (!singularStatement) { + return false; + } + J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0); + + J.FieldAccess fieldAccess = (J.FieldAccess) assignment.getVariable(); + + return + // assigned value is exactly the parameter + assignment.getAssignment().toString().equals(paramName) + + // type of parameter and field have to match + && param.getType().equals(fieldAccess.getType()); + + } + + public static String deriveSetterMethodName(JavaType.Variable fieldType) { + return "set" + StringUtils.capitalize(fieldType.getName()); + } + static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) { if (methodDeclaration.hasModifier(Public)) { return PUBLIC; From aec0da63bbc770a8bc099cf20aa3c8bedd64712c Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:14:52 +0100 Subject: [PATCH 04/10] minor polish --- .../java/migrate/lombok/NormalizeSetter.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java index 4f8de64d3..eef5176d2 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/NormalizeSetter.java @@ -39,23 +39,18 @@ public class NormalizeSetter extends ScanningRecipe { @Override public String getDisplayName() { - //language=markdown return "Rename setter methods to fit lombok"; } @Override public String getDescription() { //language=markdown - return new StringJoiner("\n") - .add("Rename methods that are effectively setter to the name lombok would give them.") - .add("") - .add("Limitations:") - .add("") - .add(" - 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.") - .add(" - If the correct name for a method is already taken by another method then the name will not be corrected.") - .add(" - Method name swaps or circular renaming within a class cannot be performed because the names block each other. ") - .add("E.g. `int getFoo() { return ba; } int getBa() { return foo; }` stays as it is.") - .toString(); + 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 { From 2749ebbc56afbea040de4fa5f1e6620c57be8e1e Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:21:48 +0100 Subject: [PATCH 05/10] remove line in recipe spec --- .../migrate/lombok/NormalizeSetterTest.java | 62 +------------------ 1 file changed, 1 insertion(+), 61 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java index 24bb1b6d7..55111dccb 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java @@ -29,70 +29,10 @@ class NormalizeSetterTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new NormalizeSetter()) - .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)); + spec.recipe(new NormalizeSetter()); } @DocumentExample - - @Test - void applyDirectly() {//TODO remove again - rewriteRun( - spec -> spec - .recipe(new ChangeMethodName("com.yourorg.whatever.A giveFoo()", "getFoo", null, null)) - .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)), - // language=java - java( - """ - package com.yourorg.whatever; - class A { - int foo = 9; - int giveFoo() { return foo; } - } - """, - """ - package com.yourorg.whatever; - class A { - int foo = 9; - int getFoo() { return foo; } - } - """ - ) - ); - } - - @Test - void applyDirectlyOnSetter() {//TODO remove again - rewriteRun( - spec -> spec - .recipe(new ChangeMethodName("com.yourorg.whatever.A storeFoo(int)", "setFoo", null, null)) - .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true)), - // language=java - java( - """ - package com.yourorg.whatever; - class A { - int foo = 9; - public void storeFoo(int foo) { - this.foo = foo; - } - } - """, - """ - package com.yourorg.whatever; - class A { - int foo = 9; - public void setFoo(int foo) { - this.foo = foo; - } - } - """ - ) - ); - } - - - @Test void renameInSingleClass() { rewriteRun(// language=java From 2716b85f74629c6735868e94c80d989c3a98b118 Mon Sep 17 00:00:00 2001 From: timo-a <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:33:37 +0100 Subject: [PATCH 06/10] Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/org/openrewrite/java/migrate/lombok/LombokUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java index 454147b9a..731fcfb01 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -149,8 +149,8 @@ public static boolean isEffectivelySetter(J.MethodDeclaration method) { String paramName = param.getName().toString(); boolean singularStatement = method.getBody() != null //abstract methods can be null - && method.getBody().getStatements().size() == 1 - && method.getBody().getStatements().get(0) instanceof J.Assignment; + && method.getBody().getStatements().size() == 1 && + method.getBody().getStatements().get(0) instanceof J.Assignment; if (!singularStatement) { return false; From 2e04ef6d51c90e9fff9ba10aeef55b58a200296e Mon Sep 17 00:00:00 2001 From: timo-a <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:34:00 +0100 Subject: [PATCH 07/10] Update src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../org/openrewrite/java/migrate/lombok/LombokUtils.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java index 731fcfb01..74e967228 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -161,9 +161,9 @@ public static boolean isEffectivelySetter(J.MethodDeclaration method) { return // assigned value is exactly the parameter - assignment.getAssignment().toString().equals(paramName) - - // type of parameter and field have to match + assignment.getAssignment().toString().equals(paramName) // type of parameter and field have to match + && + param.getType().equals(fieldAccess.getType()); && param.getType().equals(fieldAccess.getType()); } From 44b7aa69da88a5dbb945b8e7367f44e1992bfe49 Mon Sep 17 00:00:00 2001 From: timo-a <1398557+timo-a@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:34:16 +0100 Subject: [PATCH 08/10] Update src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../openrewrite/java/migrate/lombok/NormalizeSetterTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java index 55111dccb..1414b79a5 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java @@ -18,9 +18,6 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; -import org.openrewrite.java.ChangeMethodName; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; From 73e78a7a8315d3cb0328372f77fc6c08f5f69ef8 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sat, 21 Dec 2024 23:08:55 +0100 Subject: [PATCH 09/10] fix build --- .../org/openrewrite/java/migrate/lombok/LombokUtils.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java index 74e967228..38db56719 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -161,10 +161,9 @@ public static boolean isEffectivelySetter(J.MethodDeclaration method) { return // assigned value is exactly the parameter - assignment.getAssignment().toString().equals(paramName) // type of parameter and field have to match - && - param.getType().equals(fieldAccess.getType()); - && param.getType().equals(fieldAccess.getType()); + assignment.getAssignment().toString().equals(paramName) && + param.getType().equals(fieldAccess.getType()) // type of parameter and field have to match + ; } From 1024a1ea3d063c013a7da0a531f50228a5ee3a50 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Sun, 22 Dec 2024 00:58:35 +0100 Subject: [PATCH 10/10] fix build --- .../org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java index 1414b79a5..12ed973d1 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/NormalizeSetterTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java;