diff --git a/src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java b/src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java new file mode 100644 index 000000000..93f2c306d --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java @@ -0,0 +1,26 @@ +package org.openrewrite.java.spring; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; + +public class RequiredFieldIntoConstructorParameter extends Recipe { + + @Override + public String getDisplayName() { + return "Required FieldInto Constructor Parameter"; + } + + @Override + public String getDescription() { + return "Moves attributes for setters maker with @Required annotation to constructor parameter."; + } + + @Override + public TreeVisitor getVisitor() { + return new JavaIsoVisitor() { + + }; + } +} 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 new file mode 100644 index 000000000..d1b1c3bec --- /dev/null +++ b/src/testWithSpringBoot_2_1/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameterTest.java @@ -0,0 +1,89 @@ +package org.openrewrite.java.spring; + +import org.junit.jupiter.api.Test; +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")); + } + + @Test + void fieldIntoExistingSingleConstructor() { + //language=java + rewriteRun( + java( + """ + package demo; + import org.springframework.beans.factory.annotation.Required; + + public class Test { + private String a; + + @Required + void setA(String a) { + this.a = a; + } + } + """, + """ + package demo; + + public class Test { + private final String a; + + Test(String a) { + this.a = a; + } + } + """ + ) + ); + } + + @Test + public void requiredFieldIntoConstructorParameter() { + rewriteRun( + //language=java + java( + """ + package demo; + import org.springframework.beans.factory.annotation.Required; + + public class Test { + private String first; + private String a; + + Test(String first, String a) { + this.first = first; + } + + @Required + void setA(String a) { + this.a = a; + } + } + """, + """ + package demo; + + public class Test { + private String first; + private final String a; + + Test(String first, String a) { + this.first = first; + this.a = a; + } + } + """ + ) + ); + } +}