Closed jdelobel closed 1 year ago
Hi @jdelobel ! It sounds like what you're looking to do is very similar to what @m-brophy is working on in https://github.com/openrewrite/rewrite/pull/3337 and the replacement likely to come in rewrite-migrate-java, with the minor change that he's adding a different annotation. There was also a bit of back and forth on our Slack with some additional fixes to his tests that could help you see the recommended test pattern. You might want to reach out here or on our Slack to learn from each other too.
Separately there's also a collection of JEE to Spring migration recipes over at Spring Boot Migrator. That could be interesting for you as well, with potentially some work you can lift over. @fabapp2 would be to one to reach out to there.
Does that help get you past the initial hurdle in continuing your recipe development?
Hi @jdelobel ! It sounds like what you're looking to do is very similar to what @m-brophy is working on in #3337 and the replacement likely to come in rewrite-migrate-java, with the minor change that he's adding a different annotation. There was also a bit of back and forth on our Slack with some additional fixes to his tests that could help you see the recommended test pattern. You might want to reach out here or on our Slack to learn from each other too.
Separately there's also a collection of JEE to Spring migration recipes over at Spring Boot Migrator. That could be interesting for you as well, with potentially some work you can lift over. @fabapp2 would be to one to reach out to there.
Does that help get you past the initial hurdle in continuing your recipe development?
Hi @timtebeek ,
-Thanks a lot for all of your shared links!. I will check its works ASAP. @fabapp2 Do you intend to migrate to open-rewrite v8?
Unfortunately I'm on a company and the firewall blocks slack (and it's not very practical to rewrite the code snippets on the phone).
Sure! Since I don't want you missing out on unknown details in Slack, here's a quick copy of what was discussed:
FYI, i did the test:
package com.myorg.spm.annotation.java.component;
import com.myorg.spm.java.annotation.component.AddComponentAnnotationForAllInjected;
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;
public class AddComponentAnnotationForAllInjectedTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new AddComponentAnnotationForAllInjected()).parser(JavaParser.fromJavaVersion().classpath("javax.inject"));
}
@Test
void addComponentWhenInjectAnnotationIsPresentAtVariableLevel() {
rewriteRun(
//language=java
java("""
import javax.inject.*;
class Test {
@Inject
private MyComponent mc;
}
"""),
//language=java
java("""
class MyComponent {}
""", """
import org.springframework.stereotype.Component;
@Component
class MyComponent {}
"""));
}
@Test
void addComponentWhenInjectAnnotationIsPresentAtConstructorLevel() {
rewriteRun(
//language=java
java("""
import javax.inject.*;
class Test {
private MyComponent mc;
@Inject
public Test(private MyComponent mc){
this.mc = mc;
}
}
"""),
//language=java
java("""
class MyComponent {}
""", """
import org.springframework.stereotype.Component;
@Component
class MyComponent {}
"""));
}
@Test
void addComponentOnCorrespondingClassesWhenInjectAnnotationIsPresentOnItsImplementedInterface() {
rewriteRun(
//language=java
java("""
public interface TestInterface {}
"""),
//language=java
java("""
import javax.inject.*;
class Test {
@Inject
private TestInterface mc;
}
"""),
//language=java
java("""
class MyComponent implements TestInterface {}
""", """
import org.springframework.stereotype.Component;
@Component
class MyComponent implements TestInterface {}
"""));
}
@Test
void addComponentOnClassesThatImplementsDgtStartupListener() {
rewriteRun(
//language=java
java("""
package com.myorg.fmk.base.deploiement;
public interface DgtStartupListener {}
"""),
//language=java
java("""
import com.myorg.fmk.base.deploiement.DgtStartupListener;
class Test implements DgtStartupListener {}
""", """
import com.myorg.fmk.base.deploiement.DgtStartupListener;
import org.springframework.stereotype.Component;
@Component
class Test implements DgtStartupListener {}
"""));
}
}
Sorry for the recipe's name. I have not found better :)
What problem are you trying to solve?
I have to migrate Jboss/JEE Java 8 application to Springboot 2.7.x. running (and compiling) on java 17 I have to create serevals recipes but for this issue i propose to add Spring
@Component
annotation on each class that uses@Inject
or@Path
in the code. I have no idea how to test this recipe with RewriteTest.What precondition(s) should be checked before applying this recipe?
@Inject
or@Path
@Component
annotation on each class injection detectedDescribe the situation before applying the recipe
Describe the situation after applying the recipe
Additionnaly I would be very happy if you explain to me how to create a unit test for this recipe :)
Have you considered any alternatives or workarounds?
No
Any additional context
N/A
Are you interested in contributing this recipe to OpenRewrite?
Yes
Here is my recipe: