Skip to content

Commit

Permalink
ArC: initial support for producer & synthetic bean interception
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jun 19, 2024
1 parent ed77ee2 commit 9b1a13c
Show file tree
Hide file tree
Showing 44 changed files with 3,304 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.arc.test.interceptor.producer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InterceptionProxy;
import io.quarkus.arc.Unremovable;
import io.quarkus.test.QuarkusUnitTest;

public class ProducerWithFinalInterceptedClassTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar
.addClasses(MyBinding.class, MyInterceptor.class, MyNonbean.class, MyProducer.class));

@Test
public void test() {
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get();
assertEquals("intercepted: hello1", nonbean.hello1());
assertEquals("hello2", nonbean.hello2());
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
@InterceptorBinding
@interface MyBinding {
}

@MyBinding
@Priority(1)
@Interceptor
static class MyInterceptor {
@AroundInvoke
Object intercept(InvocationContext ctx) throws Exception {
return "intercepted: " + ctx.proceed();
}
}

static final class MyNonbean {
@MyBinding
String hello1() {
return "hello1";
}

String hello2() {
return "hello2";
}
}

@Dependent
static class MyProducer {
@Produces
@Unremovable
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) {
return proxy.create(new MyNonbean());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.arc.test.interceptor.producer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InterceptionProxy;
import io.quarkus.arc.Unremovable;
import io.quarkus.test.QuarkusUnitTest;

public class ProducerWithFinalInterceptedMethodTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar
.addClasses(MyBinding.class, MyInterceptor.class, MyNonbean.class, MyProducer.class));

@Test
public void test() {
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get();
assertEquals("intercepted: hello1", nonbean.hello1());
assertEquals("hello2", nonbean.hello2());
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
@InterceptorBinding
@interface MyBinding {
}

@MyBinding
@Priority(1)
@Interceptor
static class MyInterceptor {
@AroundInvoke
Object intercept(InvocationContext ctx) throws Exception {
return "intercepted: " + ctx.proceed();
}
}

static class MyNonbean {
@MyBinding
final String hello1() {
return "hello1";
}

final String hello2() {
return "hello2";
}
}

@Dependent
static class MyProducer {
@Produces
@Unremovable
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) {
return proxy.create(new MyNonbean());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.quarkus.arc.test.interceptor.producer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InterceptionProxy;
import io.quarkus.arc.Unremovable;
import io.quarkus.test.QuarkusUnitTest;

public class ProducerWithPrivateZeroParamCtorAndInterceptionTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar
.addClasses(MyBinding.class, MyInterceptor.class, MyNonbean.class, MyProducer.class));

@Test
public void test() {
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get();
assertEquals("intercepted: hello", nonbean.hello());
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
@InterceptorBinding
@interface MyBinding {
}

@MyBinding
@Priority(1)
@Interceptor
static class MyInterceptor {
@AroundInvoke
Object intercept(InvocationContext ctx) throws Exception {
return "intercepted: " + ctx.proceed();
}
}

static class MyNonbean {
private MyNonbean() {
}

@MyBinding
String hello() {
return "hello";
}
}

@Dependent
static class MyProducer {
@Produces
@Unremovable
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) {
return proxy.create(new MyNonbean());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.quarkus.arc.test.interceptor.producer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InterceptionProxy;
import io.quarkus.arc.Unremovable;
import io.quarkus.test.QuarkusUnitTest;

public class ProducerWithoutZeroParamCtorAndInterceptionTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar
.addClasses(MyBinding.class, MyInterceptor.class, MyNonbean.class, MyProducer.class));

@Test
public void test() {
MyNonbean nonbean = Arc.container().instance(MyNonbean.class).get();
assertEquals("intercepted: hello", nonbean.hello());
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
@InterceptorBinding
@interface MyBinding {
}

@MyBinding
@Priority(1)
@Interceptor
static class MyInterceptor {
@AroundInvoke
Object intercept(InvocationContext ctx) throws Exception {
return "intercepted: " + ctx.proceed();
}
}

static class MyNonbean {
MyNonbean(int ignored) {
}

@MyBinding
String hello() {
return "hello";
}
}

@Dependent
static class MyProducer {
@Produces
@Unremovable
MyNonbean produce(InterceptionProxy<MyNonbean> proxy) {
return proxy.create(new MyNonbean(0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static String generatedNameFromTarget(String targetPackage, String baseName, Str
}
}

protected String getBaseName(BeanInfo bean, String beanClassName) {
protected String getBaseName(String beanClassName) {
String name = Types.getSimpleName(beanClassName);
return name.substring(0, name.lastIndexOf(BeanGenerator.BEAN_SUFFIX));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void done() {
.removable(removable)
.forceApplicationClass(forceApplicationClass)
.targetPackageName(targetPackageName)
.startupPriority(startupPriority);
.startupPriority(startupPriority)
.interceptionProxies(interceptionProxies);

if (!injectionPoints.isEmpty()) {
builder.injections(Collections.singletonList(Injection.forSyntheticBean(injectionPoints)));
Expand Down
Loading

0 comments on commit 9b1a13c

Please sign in to comment.