Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain BeanManager fireEvent annotations #599

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -44,25 +48,39 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (fireEventMatcher.matches(method)) {
return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent().fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})")
List<Expression> arguments = mi.getArguments();
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)})")
.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));
}

Object[] args = new Expression[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));
} else if (createInjectionTargetMatcher.matches(method)) {
.apply(updateCursor(mi), mi.getCoordinates().replace(), args);
} 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(),
mi.getArguments().get(0));
.apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0));
}
return mi;
}

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
}
}
"""
)
);
}
}