Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace @Required on setters with @Autowired #612

Merged
merged 12 commits into from
Oct 29, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openrewrite.java.spring;
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved

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<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.openrewrite.java.spring;
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved

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
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
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() {
nielsdebruin marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
"""
)
);
}
}
Loading