|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate; |
| 17 | + |
| 18 | +import org.openrewrite.*; |
| 19 | +import org.openrewrite.internal.ListUtils; |
| 20 | +import org.openrewrite.java.JavaVisitor; |
| 21 | +import org.openrewrite.java.search.FindAnnotations; |
| 22 | +import org.openrewrite.java.search.UsesType; |
| 23 | +import org.openrewrite.java.tree.J; |
| 24 | +import org.openrewrite.java.tree.Space; |
| 25 | +import org.openrewrite.marker.Markers; |
| 26 | +import org.openrewrite.xml.XPathMatcher; |
| 27 | +import org.openrewrite.xml.XmlVisitor; |
| 28 | +import org.openrewrite.xml.tree.Xml; |
| 29 | + |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import static java.util.Collections.emptyList; |
| 34 | + |
| 35 | +public class AddStaticVariableOnProducerSessionBean extends ScanningRecipe<Set<String>> { |
| 36 | + |
| 37 | + private static final XPathMatcher EJB_PATH = new XPathMatcher("ejb-jar/enterprise-beans/session"); |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getDisplayName() { |
| 41 | + return "Adds `static` modifier to `@Produces` fields that are in session beans"; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String getDescription() { |
| 46 | + return "Ensures that the fields annotated with `@Produces` which is inside the session bean (`@Stateless`, `@Stateful`, or `@Singleton`) are declared `static`."; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Set<String> getInitialValue(ExecutionContext ctx) { |
| 51 | + // Class names of session beans found in XML |
| 52 | + return new HashSet<>(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public TreeVisitor<?, ExecutionContext> getScanner(Set<String> acc) { |
| 57 | + return Preconditions.check( |
| 58 | + new FindSourceFiles("**/ejb-jar.xml"), |
| 59 | + new XmlVisitor<ExecutionContext>() { |
| 60 | + @Override |
| 61 | + public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { |
| 62 | + if (EJB_PATH.matches(getCursor())) { |
| 63 | + Xml.Tag ejbClassTag = tag.getChild("ejb-class").orElse(null); |
| 64 | + Xml.Tag sessionTag = tag.getChild("session-type").orElse(null); |
| 65 | + if (ejbClassTag != null && sessionTag != null) { |
| 66 | + String className = ejbClassTag.getValue().orElse(null); |
| 67 | + String sessionType = sessionTag.getValue().orElse(null); |
| 68 | + if (className != null && |
| 69 | + ("Singleton".equalsIgnoreCase(sessionType) || |
| 70 | + "Stateless".equalsIgnoreCase(sessionType) || |
| 71 | + "Stateful".equalsIgnoreCase(sessionType))) { |
| 72 | + acc.add(className); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return super.visitTag(tag, ctx); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public TreeVisitor<?, ExecutionContext> getVisitor(Set<String> acc) { |
| 83 | + return Preconditions.check( |
| 84 | + new UsesType<>("jakarta.enterprise.inject.Produces", false), |
| 85 | + new JavaVisitor<ExecutionContext>() { |
| 86 | + @Override |
| 87 | + public J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { |
| 88 | + if (!multiVariable.hasModifier(J.Modifier.Type.Static) && |
| 89 | + hasAnnotation(multiVariable, "@jakarta.enterprise.inject.Produces") && |
| 90 | + (isInSessionBean() || isInXml())) { |
| 91 | + return multiVariable.withModifiers(ListUtils.concat(multiVariable.getModifiers(), |
| 92 | + new J.Modifier(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, null, J.Modifier.Type.Static, emptyList()))); |
| 93 | + } |
| 94 | + return multiVariable; |
| 95 | + } |
| 96 | + |
| 97 | + private boolean isInSessionBean() { |
| 98 | + J.ClassDeclaration parentClass = getCursor().firstEnclosing(J.ClassDeclaration.class); |
| 99 | + if (parentClass == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + return hasAnnotation(parentClass, "@jakarta.ejb.Singleton") || |
| 103 | + hasAnnotation(parentClass, "@jakarta.ejb.Stateful") || |
| 104 | + hasAnnotation(parentClass, "@jakarta.ejb.Stateless"); |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isInXml() { |
| 108 | + J.ClassDeclaration parentClass = getCursor().firstEnclosing(J.ClassDeclaration.class); |
| 109 | + if (parentClass != null && parentClass.getType() != null) { |
| 110 | + return acc.contains(parentClass.getType().getFullyQualifiedName()); |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + private boolean hasAnnotation(J j, String annotationPattern) { |
| 116 | + return !FindAnnotations.find(j, annotationPattern).isEmpty(); |
| 117 | + } |
| 118 | + } |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments