From 4d4bf7e2feffd29304393bd1f380884c508d6bdd Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 11 Nov 2024 23:35:41 +0100 Subject: [PATCH 1/2] Retain BeanManager `firstEvent` annotations Fixes https://github.com/openrewrite/rewrite-migrate-java/issues/597 --- .../jakarta/UpdateBeanManagerMethods.java | 33 ++++++++++--- .../jakarta/UpdateBeanManagerMethodsTest.java | 47 +++++++++++++++++-- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java index 520f18aea7..e348b65100 100644 --- a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java +++ b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java @@ -22,8 +22,12 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; +import java.util.Collections; +import java.util.List; + public class UpdateBeanManagerMethods extends Recipe { @Override public String getDisplayName() { @@ -44,14 +48,30 @@ public TreeVisitor getVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); + List arguments = mi.getArguments(); if (fireEventMatcher.matches(method)) { - return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent().fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})") + if (arguments.size() <= 1) { + return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" + + ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) + .build() + .apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0)); + } + + J[] args = new J[arguments.size() + 1]; + args[0] = mi.getSelect(); + for (int i = 1; i < arguments.size(); i++) { + args[i] = arguments.get(i); + } + args[arguments.size()] = arguments.get(0); + + String template = "#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" + + ".select(" + String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any(java.lang.annotation.Annotation)}")) + ')' + + ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})"; + return JavaTemplate.builder(template) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() - .apply(updateCursor(mi), - mi.getCoordinates().replace(), - mi.getSelect(), - mi.getArguments().get(0)); + .apply(updateCursor(mi), mi.getCoordinates().replace(), args); } else if (createInjectionTargetMatcher.matches(method)) { return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getInjectionTargetFactory(#{any(jakarta.enterprise.inject.spi.AnnotatedType)}).createInjectionTarget(null)") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) @@ -59,10 +79,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu .apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), - mi.getArguments().get(0)); + arguments.get(0)); } return mi; } + }; } } diff --git a/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethodsTest.java b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethodsTest.java index 6a88944aca..fadd15bcda 100644 --- a/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethodsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethodsTest.java @@ -16,22 +16,23 @@ package org.openrewrite.java.migrate.jakarta; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; -@DisabledIfEnvironmentVariable(named = "CI", matches = "true") // Unexplained failure only on GitHub Actions 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")) + .classpathFromResources(new InMemoryExecutionContext(), + "jakarta.enterprise.cdi-api-3.0.0-M4", + "jakarta.enterprise.cdi-api-4.0.1")) .recipe(new UpdateBeanManagerMethods()); } @@ -97,4 +98,44 @@ void bar(BeanManager beanManager) { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/597") + @Test + void fireEventWithClassifiers() { + rewriteRun( + //language=java + java( + """ + import java.lang.annotation.Annotation; + import jakarta.enterprise.inject.spi.BeanManager; + import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; + import java.util.Set; + + class Foo { + void one(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery, Annotation classifier) { + beanManager.fireEvent(beforeBeanDiscovery, classifier); + } + void two(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery, Annotation classifier) { + beanManager.fireEvent(beforeBeanDiscovery, classifier, classifier); + } + } + """, + """ + import java.lang.annotation.Annotation; + import jakarta.enterprise.inject.spi.BeanManager; + import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; + import java.util.Set; + + class Foo { + void one(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery, Annotation classifier) { + beanManager.getEvent().select(classifier).fire(beforeBeanDiscovery); + } + void two(BeanManager beanManager, BeforeBeanDiscovery beforeBeanDiscovery, Annotation classifier) { + beanManager.getEvent().select(classifier, classifier).fire(beforeBeanDiscovery); + } + } + """ + ) + ); + } } From bd25c8eb3717404e35614f7289f46b2b884692ef Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 11 Nov 2024 23:40:34 +0100 Subject: [PATCH 2/2] Reduce warnings --- .../migrate/jakarta/UpdateBeanManagerMethods.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java index e348b65100..fd7c9798a0 100644 --- a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java +++ b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java @@ -49,7 +49,7 @@ public TreeVisitor getVisitor() { public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); List arguments = mi.getArguments(); - if (fireEventMatcher.matches(method)) { + if (fireEventMatcher.matches(method) && mi.getSelect() != null) { if (arguments.size() <= 1) { return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" + ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})") @@ -58,7 +58,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu .apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0)); } - J[] args = new J[arguments.size() + 1]; + Object[] args = new Expression[arguments.size() + 1]; args[0] = mi.getSelect(); for (int i = 1; i < arguments.size(); i++) { args[i] = arguments.get(i); @@ -72,14 +72,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() .apply(updateCursor(mi), mi.getCoordinates().replace(), args); - } else if (createInjectionTargetMatcher.matches(method)) { + } else if (createInjectionTargetMatcher.matches(method) && mi.getSelect() != null) { return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getInjectionTargetFactory(#{any(jakarta.enterprise.inject.spi.AnnotatedType)}).createInjectionTarget(null)") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() - .apply(updateCursor(mi), - mi.getCoordinates().replace(), - mi.getSelect(), - arguments.get(0)); + .apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0)); } return mi; }