-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e795b0
commit bb67416
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
|
||
}; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...gBoot_2_1/java/org/openrewrite/java/spring/RequiredFieldIntoConstructorParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |