diff --git a/src/main/java/org/openrewrite/java/migrate/jakarta/RemoveBeanIsNullable.java b/src/main/java/org/openrewrite/java/migrate/jakarta/RemoveBeanIsNullable.java new file mode 100644 index 0000000000..a1f2992585 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/jakarta/RemoveBeanIsNullable.java @@ -0,0 +1,60 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.Space;
+import org.openrewrite.marker.Markers;
+import org.openrewrite.staticanalysis.RemoveUnusedLocalVariables;
+import org.openrewrite.staticanalysis.SimplifyConstantIfBranchExecution;
+
+import static org.openrewrite.Tree.randomId;
+
+public class RemoveBeanIsNullable extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Remove `Bean.isNullable()`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "`Bean.isNullable()` has been removed in CDI 4.0.0, and now always returns `false`.";
+ }
+
+ private static final MethodMatcher BEAN_ISNULLABLE = new MethodMatcher("jakarta.enterprise.inject.spi.Bean isNullable()", false);
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+
+public class UpdateAddAnnotatedTypes extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Replace `BeforeBeanDiscovery.addAnnotatedType(AnnotatedType)` with `addAnnotatedType(AnnotatedType, String)`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "`BeforeBeanDiscovery.addAnnotatedType(AnnotatedType)` is deprecated in CDI 1.1. It is Replaced by `BeforeBeanDiscovery.addAnnotatedType(AnnotatedType, String)`.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+
+public class UpdateBeanManagerMethods extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Update `fireEvent()` and `createInjectionTarget()` calls";
+ }
+
+ @Override
+ public String getDescription() {
+ return " Updates `BeanManager.fireEvent()` or `BeanManager.createInjectionTarget()`.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class RemoveBeanIsNullableTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new RemoveBeanIsNullable())
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "jakarta.enterprise.cdi-api-3.0.0-M4"));
+ }
+
+ @Test
+ @DocumentExample
+ void removeBeanIsNullable() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.enterprise.inject.spi.Bean;
+
+ class Test {
+ void test(Bean> bean) {
+ if (bean.isNullable()) {
+ System.out.println("is null");
+ } else {
+ System.out.println("not null");
+ }
+ }
+ }
+ """,
+ """
+ import jakarta.enterprise.inject.spi.Bean;
+
+ class Test {
+ void test(Bean> bean) {
+ System.out.println("not null");
+ }
+ }
+ """
+ )
+ );
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateAddAnnotatedTypesTest.java b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateAddAnnotatedTypesTest.java
new file mode 100644
index 0000000000..e2b438c200
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateAddAnnotatedTypesTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class UpdateAddAnnotatedTypesTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "jakarta.enterprise.cdi-api-3.0.0-M4", "jakarta.enterprise.cdi-api-4.0.1"))
+ .recipe(new UpdateAddAnnotatedTypes());
+ }
+
+ @Test
+ void addAnnotatedType() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.enterprise.inject.spi.AnnotatedType;
+ import jakarta.enterprise.inject.spi.BeanManager;
+ import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
+
+ class Foo {
+ void bar(BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) {
+ AnnotatedType
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate.jakarta;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class UpdateBeanManagerMethodsTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "jakarta.enterprise.cdi-api-3.0.0-M4", "jakarta.enterprise.cdi-api-4.0.1"))
+ .recipe(new UpdateBeanManagerMethods());
+ }
+
+ @Test
+ void fireEvent() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.enterprise.inject.spi.BeanManager;
+ import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
+ import java.util.Set;
+
+ class Foo {
+ void bar(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery) {
+ beanManager.fireEvent(beforeBeanDiscovery);
+ }
+ }
+ """,
+ """
+ import jakarta.enterprise.inject.spi.BeanManager;
+ import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
+ import java.util.Set;
+
+ class Foo {
+ void bar(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery) {
+ beanManager.getEvent().fire(beforeBeanDiscovery);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void createInjectionTarget() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.enterprise.inject.spi.AnnotatedType;
+ import jakarta.enterprise.inject.spi.BeanManager;
+
+ class Foo {
+ void bar(BeanManager beanManager) {
+ AnnotatedType