Skip to content

Commit f32db12

Browse files
karthikNousherKARTHIK NOUSHERrlsanders4timtebeek
authored
Update AddStaticVariableOnProducerSessionBean to modify XML defined EJBs (#765)
* added a new xml scanner * updated the testcase added or check * chnaged to ctx * added negative test case * added a new precheck * updated the imports * Update src/main/java/org/openrewrite/java/migrate/AddStaticVariableOnProducerSessionBean.java * upated the check logic * Slight polish --------- Co-authored-by: KARTHIK NOUSHER <[email protected]> Co-authored-by: Ralph Sanders <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent f63ed63 commit f32db12

File tree

2 files changed

+161
-18
lines changed

2 files changed

+161
-18
lines changed

src/main/java/org/openrewrite/java/migrate/AddStaticVariableOnProducerSessionBean.java

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,25 @@
2121
import org.openrewrite.java.search.FindAnnotations;
2222
import org.openrewrite.java.search.UsesType;
2323
import org.openrewrite.java.tree.J;
24+
import org.openrewrite.java.tree.JavaType;
2425
import org.openrewrite.java.tree.Space;
2526
import org.openrewrite.marker.Markers;
27+
import org.openrewrite.xml.XPathMatcher;
28+
import org.openrewrite.xml.XmlVisitor;
29+
import org.openrewrite.xml.tree.Xml;
30+
31+
import java.util.HashSet;
32+
import java.util.Set;
2633

2734
import static java.util.Collections.emptyList;
2835

29-
public class AddStaticVariableOnProducerSessionBean extends Recipe {
36+
public class AddStaticVariableOnProducerSessionBean extends ScanningRecipe<Set<String>> {
37+
38+
private static final XPathMatcher EJB_PATH = new XPathMatcher("ejb-jar/enterprise-beans/session");
39+
3040
@Override
3141
public String getDisplayName() {
32-
return "Adds `static` modifier to `@Produces` field that are on session bean";
42+
return "Adds `static` modifier to `@Produces` fields that are in session beans";
3343
}
3444

3545
@Override
@@ -38,26 +48,51 @@ public String getDescription() {
3848
}
3949

4050
@Override
41-
public TreeVisitor<?, ExecutionContext> getVisitor() {
51+
public Set<String> getInitialValue(ExecutionContext ctx) {
52+
// Class names of session beans found in XML
53+
return new HashSet<>();
54+
}
55+
56+
@Override
57+
public TreeVisitor<?, ExecutionContext> getScanner(Set<String> acc) {
4258
return Preconditions.check(
43-
Preconditions.and(
44-
new UsesType<>("jakarta.enterprise.inject.Produces", false),
45-
Preconditions.or(
46-
new UsesType<>("jakarta.ejb.Singleton", false),
47-
new UsesType<>("jakarta.ejb.Stateful", false),
48-
new UsesType<>("jakarta.ejb.Stateless", false)
49-
)
50-
),
59+
new FindSourceFiles("**/ejb-jar.xml"),
60+
new XmlVisitor<ExecutionContext>() {
61+
@Override
62+
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
63+
if (EJB_PATH.matches(getCursor())) {
64+
Xml.Tag ejbClassTag = tag.getChild("ejb-class").orElse(null);
65+
Xml.Tag sessionTag = tag.getChild("session-type").orElse(null);
66+
if (ejbClassTag != null && sessionTag != null) {
67+
String className = ejbClassTag.getValue().orElse(null);
68+
String sessionType = sessionTag.getValue().orElse(null);
69+
if (className != null &&
70+
("Singleton".equalsIgnoreCase(sessionType) ||
71+
"Stateless".equalsIgnoreCase(sessionType) ||
72+
"Stateful".equalsIgnoreCase(sessionType))) {
73+
acc.add(className);
74+
}
75+
}
76+
}
77+
return super.visitTag(tag, ctx);
78+
}
79+
});
80+
}
81+
82+
@Override
83+
public TreeVisitor<?, ExecutionContext> getVisitor(Set<String> acc) {
84+
return Preconditions.check(
85+
new UsesType<>("jakarta.enterprise.inject.Produces", false),
5186
new JavaVisitor<ExecutionContext>() {
5287
@Override
5388
public J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
5489
if (!multiVariable.hasModifier(J.Modifier.Type.Static) &&
55-
hasAnnotation(multiVariable, "@jakarta.enterprise.inject.Produces") &&
56-
isInSessionBean()) {
90+
hasAnnotation(multiVariable, "@jakarta.enterprise.inject.Produces") &&
91+
(isInSessionBean() || isInXml())) {
5792
return multiVariable.withModifiers(ListUtils.concat(multiVariable.getModifiers(),
5893
new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Static, emptyList())));
5994
}
60-
return super.visitVariableDeclarations(multiVariable, ctx);
95+
return multiVariable;
6196
}
6297

6398
private boolean isInSessionBean() {
@@ -66,13 +101,22 @@ private boolean isInSessionBean() {
66101
return false;
67102
}
68103
return hasAnnotation(parentClass, "@jakarta.ejb.Singleton") ||
69-
hasAnnotation(parentClass, "@jakarta.ejb.Stateful") ||
70-
hasAnnotation(parentClass, "@jakarta.ejb.Stateless");
104+
hasAnnotation(parentClass, "@jakarta.ejb.Stateful") ||
105+
hasAnnotation(parentClass, "@jakarta.ejb.Stateless");
106+
}
107+
108+
private boolean isInXml() {
109+
J.ClassDeclaration parentClass = getCursor().firstEnclosing(J.ClassDeclaration.class);
110+
if (parentClass != null && parentClass.getType() != null) {
111+
return acc.contains(parentClass.getType().getFullyQualifiedName());
112+
}
113+
return false;
71114
}
72115

73116
private boolean hasAnnotation(J j, String annotationPattern) {
74117
return !FindAnnotations.find(j, annotationPattern).isEmpty();
75118
}
76-
});
119+
}
120+
);
77121
}
78122
}

src/test/java/org/openrewrite/java/migrate/AddStaticVariableOnProducerSessionBeanTest.java

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.test.RewriteTest;
2323

2424
import static org.openrewrite.java.Assertions.java;
25+
import static org.openrewrite.xml.Assertions.xml;
2526

2627
class AddStaticVariableOnProducerSessionBeanTest implements RewriteTest {
2728

@@ -55,8 +56,8 @@ public class SomeDependency {}
5556
);
5657
}
5758

58-
@Test
5959
@DocumentExample
60+
@Test
6061
void addStaticOnProducesMarkedStateless() {
6162
rewriteRun(
6263
//language=java
@@ -93,6 +94,104 @@ void exampleMethod() {
9394
);
9495
}
9596

97+
@Test
98+
void addStaticToProducesFieldFromXml() {
99+
rewriteRun(
100+
xml(
101+
//language=xml
102+
"""
103+
<?xml version="1.0" encoding="UTF-8"?>
104+
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
105+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
106+
<display-name>testTransaction</display-name>
107+
<enterprise-beans>
108+
<session>
109+
<ejb-name>MySessionBean</ejb-name>
110+
<ejb-class>com.test.MySessionBean</ejb-class>
111+
<session-type>Stateless</session-type>
112+
<transaction-type>Container</transaction-type>
113+
</session>
114+
</enterprise-beans>
115+
</ejb-jar>
116+
""",
117+
sourceSpecs -> sourceSpecs.path("ejb-jar.xml")
118+
),
119+
//language=java
120+
java(
121+
"""
122+
package com.test;
123+
import jakarta.enterprise.inject.Produces;
124+
125+
public class MySessionBean {
126+
@Produces
127+
private SomeDependency someDependency;
128+
void exampleMethod() {
129+
return;
130+
}
131+
}
132+
""",
133+
"""
134+
package com.test;
135+
import jakarta.enterprise.inject.Produces;
136+
137+
public class MySessionBean {
138+
@Produces
139+
private static SomeDependency someDependency;
140+
void exampleMethod() {
141+
return;
142+
}
143+
}
144+
"""
145+
)
146+
);
147+
}
148+
149+
@Test
150+
void noChangeWhenBeanNotMentionedInXml() {
151+
rewriteRun(
152+
xml(
153+
//language=xml
154+
"""
155+
<?xml version="1.0" encoding="UTF-8"?>
156+
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
157+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
158+
<display-name>testTransaction</display-name>
159+
<enterprise-beans>
160+
<session>
161+
<ejb-name>TestProducerFieldStaticOnSessionBean</ejb-name>
162+
<ejb-class>org.test.ejb.TestProducerFieldStaticOnSessionBean</ejb-class>
163+
<session-type>Stateless</session-type>
164+
<transaction-type>Container</transaction-type>
165+
</session>
166+
<session>
167+
<ejb-name>TestProducerFieldNonStaticOnSessionBean</ejb-name>
168+
<ejb-class>org.test.ejb.TestProducerFieldNonStaticOnSessionBean</ejb-class>
169+
<session-type>Singleton</session-type>
170+
<transaction-type>Container</transaction-type>
171+
</session>
172+
</enterprise-beans>
173+
</ejb-jar>
174+
""",
175+
sourceSpecs -> sourceSpecs.path("ejb-jar.xml")
176+
),
177+
//language=java
178+
java(
179+
"""
180+
package com.test;
181+
import jakarta.enterprise.inject.Produces;
182+
183+
public class MySessionBean {
184+
@Produces
185+
private SomeDependency someDependency;
186+
void exampleMethod() {
187+
return;
188+
}
189+
}
190+
"""
191+
)
192+
);
193+
}
194+
96195
@Test
97196
void addStaticOnProducesMarkedStateful() {
98197
rewriteRun(

0 commit comments

Comments
 (0)