From 868328ba7813668436c23a71fdf56c7654e9d0d6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 15 Dec 2024 18:23:35 +0100 Subject: [PATCH] Add one more style we ought to cover --- .../java/migrate/lombok/LombokUtils.java | 28 ++++++---- .../migrate/lombok/UseLombokSetterTest.java | 54 +++++++++++++++++++ 2 files changed, 71 insertions(+), 11 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 267cf778a..4343ed851 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -99,26 +99,32 @@ static boolean isSetter(J.MethodDeclaration method) { return false; } J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0); - J.FieldAccess fieldAccess = (J.FieldAccess) assignment.getVariable(); - if (!method.getSimpleName().equals(deriveSetterMethodName(fieldAccess))) { + J.FieldAccess assignedField = (J.FieldAccess) assignment.getVariable(); + if (!method.getSimpleName().equals(deriveSetterMethodName(assignedField))) { return false; } // Check argument is assigned to field J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) method.getParameters().get(0); J.VariableDeclarations.NamedVariable param = variableDeclarations.getVariables().get(0); - JavaType paramType = param.getType(); - String paramName = param.getName().toString(); + // type of parameter and field have to match + if (!param.getType().equals(assignedField.getType())) { + return false; + } - return - // assigned value is exactly the parameter - assignment.getAssignment().toString().equals(paramName) // type of parameter and field have to match - && - - // type of parameter and field have to match - param.getType().equals(fieldAccess.getType()); + // Check field is declared on method type + JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType(); + if (assignedField.getTarget() instanceof J.Identifier) { + J.Identifier target = (J.Identifier) assignedField.getTarget(); + return target.getFieldType() != null && declaringType == target.getFieldType().getOwner(); + } else if (assignedField.getTarget() instanceof J.FieldAccess) { + Expression target = ((J.FieldAccess) assignedField.getTarget()).getTarget(); + return target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null && + declaringType == ((J.Identifier) target).getFieldType().getOwner(); + } + return false; } private static String deriveSetterMethodName(J.FieldAccess fieldAccess) { diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/UseLombokSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/UseLombokSetterTest.java index bab9ddf25..5116b15e0 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/UseLombokSetterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/UseLombokSetterTest.java @@ -66,6 +66,60 @@ class A { ); } + @Test + void replaceSetterWhenArgNameWithUnderscore() { + rewriteRun(// language=java + java( + """ + class A { + + int foo = 9; + + public void setFoo(int foo_) { + this.foo = foo_; + } + } + """, + """ + import lombok.Setter; + + class A { + + @Setter + int foo = 9; + } + """ + ) + ); + } + + @Test + void replaceSetterWhenArgNameWithUnderscoreAndUnqualifiedFieldAccess() { + rewriteRun(// language=java + java( + """ + class A { + + int foo = 9; + + public void setFoo(int foo_) { + foo = foo_; + } + } + """, + """ + import lombok.Setter; + + class A { + + @Setter + int foo = 9; + } + """ + ) + ); + } + @Test void tolerantToNonstandardParameterNames() { rewriteRun(// language=java