diff --git a/src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java b/src/main/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowired.java similarity index 73% rename from src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java rename to src/main/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowired.java index c2d26000..2fa3259f 100644 --- a/src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java +++ b/src/main/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowired.java @@ -22,18 +22,16 @@ import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.search.UsesType; -public class RequiredFieldIntoConstructorParameter extends Recipe { +public class ReplaceRequiredAnnotationOnSetterWithAutowired extends Recipe { @Override public String getDisplayName() { - return "Required field into constructor parameter"; + return "Replace `@Required` annotation on setter with `@Autowired`"; } @Override public String getDescription() { - return "The RequiredFieldIntoConstructorParameter recipe moves fields which have setters marked with the " + - "@Required annotation to constructor parameters. It does not make changes when an existing constructor " + - "exists, for inherited fields, or for classes annotated with Lombok constructors."; + return "Replace setter methods annotated with `@Required` with `@Autowired`."; } @Override diff --git a/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowiredTest.java b/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowiredTest.java new file mode 100644 index 00000000..01c7eb36 --- /dev/null +++ b/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/ReplaceRequiredAnnotationOnSetterWithAutowiredTest.java @@ -0,0 +1,172 @@ +/* + * 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. + * 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.spring; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class ReplaceRequiredAnnotationOnSetterWithAutowiredTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new ReplaceRequiredAnnotationOnSetterWithAutowired()) + .parser(JavaParser.fromJavaVersion().classpath("spring-beans")); + } + + @DocumentExample + @Test + void replaceRequiredWithAutowired() { + //language=java + rewriteRun( + java( + """ + import org.springframework.beans.factory.annotation.Required; + + class Foo { + private String a; + + @Required + void setA(String a) { + this.a = a; + } + } + """, + """ + import org.springframework.beans.factory.annotation.Autowired; + + class Foo { + private String a; + + @Autowired + void setA(String a) { + this.a = a; + } + } + """ + ) + ); + } + + @Test + void replaceRequiredWithAutowiredMultipleSetters() { + rewriteRun( + //language=java + java( + """ + import org.springframework.beans.factory.annotation.Required; + + class Foo { + private String a; + private String b; + + @Required + void setA(String a) { + this.a = a; + } + + @Required + void setB(String b) { + this.b = b; + } + } + """, + """ + import org.springframework.beans.factory.annotation.Autowired; + + class Foo { + private String a; + private String b; + + @Autowired + void setA(String a) { + this.a = a; + } + + @Autowired + void setB(String b) { + this.b = b; + } + } + """ + ) + ); + } + + @Test + void replaceRequiredWithAutowiredMultipleSettersNotAllRequired() { + rewriteRun( + //language=java + java( + """ + import org.springframework.beans.factory.annotation.Required; + + class Foo { + private String a; + private String b; + + void setA(String a) { + this.a = a; + } + + @Required + void setB(String b) { + this.b = b; + } + } + """, + """ + import org.springframework.beans.factory.annotation.Autowired; + + class Foo { + private String a; + private String b; + + void setA(String a) { + this.a = a; + } + + @Autowired + void setB(String b) { + this.b = b; + } + } + """ + ) + ); + } + + @Test + void setterWithoutAnnotationsShouldNotCauseChanges() { + rewriteRun( + //language=java + java( + """ + class Foo { + private String a; + + void setA(String a) { + this.a = a; + } + } + """ + ) + ); + } +} diff --git a/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameterTest.java b/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameterTest.java deleted file mode 100644 index aad95ba7..00000000 --- a/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameterTest.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * 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. - * 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.spring; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.openrewrite.DocumentExample; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.java.Assertions.java; - -class RequiredFieldIntoConstructorParameterTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { - spec.recipe(new RequiredFieldIntoConstructorParameter()) - .parser(JavaParser.fromJavaVersion().classpath("spring-beans", "lombok")); - } - - @DocumentExample - @Test - void fieldIntoNewConstructor() { - //language=java - rewriteRun( - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String a; - - @Required - void setA(String a) { - this.a = a; - } - } - """, - """ - import org.springframework.beans.factory.annotation.Autowired; - - class Foo { - private String a; - - Foo() {} - - @Autowired - Foo(String a) { - this.a = a; - } - - void setA(String a) { - this.a = a; - } - } - """ - ) - ); - } - - @Test - void existingConstructorsShouldNotBeChanged() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String first; - private String a; - - Foo(String first) { - this.first = first; - } - - @Required - void setA(String a) { - this.a = a; - } - } - """ - ) - ); - } - - @Test - void inheritedFieldsAreNotConsidered() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Bar { - String a; - } - - class Foo extends Bar { - @Required - void setA(String a) { - this.a = a; - } - } - """ - ) - ); - } - - @Test - void multipleRequiredFieldIntoConstructorParameter() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String a; - private String b; - - @Required - void setA(String a) { - this.a = a; - } - - @Required - void setB(String b) { - this.b = b; - } - } - """, - """ - class Foo { - private String a; - private String b; - - Foo() {} - - @Autowired - Foo(String a, String b) { - this.a = a; - this.b = b; - } - - void setA(String a) { - this.a = a; - } - - void setB(String b) { - this.b = b; - } - } - """ - ) - ); - } - - @Test - void explicitEmptyConstructorShouldNotBeModified() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String a; - - Foo() { - } - - @Required - void setA(String a) { - this.a = a; - } - } - """, - """ - import org.springframework.beans.factory.annotation.Autowired; - - class Foo { - private String a; - - Foo() { - } - - @Autowired - Foo(String a) { - this.a = a; - } - - void setA(String a) { - this.a = a; - } - } - """ - ) - ); - } - - @Test - void onlyRemoveAnnotationsForSingleSetter() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String a; - private String b; - - Foo(String a, String b) { - this.a = a; - this.b = b; - } - - @Required - void setA(String a) { - this.a = a; - } - - void setB(String b) { - this.b = b; - } - } - """, - """ - import org.springframework.beans.factory.annotation.Autowired; - - class Foo { - private String a; - private String b; - - Foo(String a, String b) { - this.a = a; - this.b = b; - } - - void setA(String a) { - this.a = a; - } - - void setB(String b) { - this.b = b; - } - } - """ - ) - ); - } - - @Test - void onlyRemoveAnnotationsMultipleSetters() { - rewriteRun( - //language=java - java( - """ - import org.springframework.beans.factory.annotation.Required; - - class Foo { - private String a; - private String b; - - @Required - void setA(String a) { - this.a = a; - } - - @Required - void setB(String b) { - this.b = b; - } - } - """, - """ - import org.springframework.beans.factory.annotation.Autowired; - - class Foo { - private String a; - private String b; - - Foo() { - } - - @Autowired - Foo(String a, String b) { - this.a = a; - this.b = b; - } - - void setA(String a) { - this.a = a; - } - - void setB(String b) { - this.b = b; - } - } - """ - ) - ); - } - - @ParameterizedTest - @ValueSource(strings = { - "AllArgsConstructor", - "RequiredArgsConstructor", - "NoArgsConstructor", - "Data", - "Value" - }) - void ignoreLombok(String annotation) { - rewriteRun( - //language=java - java( - """ - import lombok.%1$s; - import org.springframework.beans.factory.annotation.Required; - - @%1$s - class Foo { - private String a; - private String b; - - @Required - void setA(String a) { - this.a = a; - } - - @Required - void setB(String b) { - this.b = b; - } - } - """.formatted(annotation) - ) - ); - } - -}