Skip to content

Commit

Permalink
Use child CreationalContext for shared lifecycle interceptor instances
Browse files Browse the repository at this point in the history
- resolves quarkusio#4545
  • Loading branch information
mkouba authored and olivier dufour committed Oct 23, 2019
1 parent 5ae9dfd commit 29ce4cc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,11 @@ protected void implementCreate(ClassOutput classOutput, ClassCreator beanCreator
create.getThis());
ResultHandle interceptorProvider = create.invokeInterfaceMethod(
MethodDescriptors.SUPPLIER_GET, interceptorProviderSupplier);
ResultHandle childCtxHandle = create.invokeStaticMethod(MethodDescriptors.CREATIONAL_CTX_CHILD,
create.getMethodParam(0));
ResultHandle interceptorInstanceHandle = create.invokeInterfaceMethod(
MethodDescriptors.INJECTABLE_REF_PROVIDER_GET, interceptorProvider,
create.getMethodParam(0));
childCtxHandle);
interceptorToResultHandle.put(interceptor, interceptorInstanceHandle);
ResultHandle wrapHandle = create.invokeStaticMethod(
MethodDescriptor.ofMethod(InitializedInterceptor.class, "of",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public class InterceptedBeanMetadataProvider implements InjectableReferenceProvi

@Override
public Contextual<?> get(CreationalContext<Contextual<?>> creationalContext) {
// First attempt to obtain the creational context of the interceptor bean and then the creational context of the intercepted bean
CreationalContextImpl<?> parent = unwrap(creationalContext).getParent();
if (parent != null) {
// Intercepted bean creational context
return parent.getContextual();
parent = parent.getParent();
return parent != null ? parent.getContextual() : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.test.ArcTestContainer;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.Bean;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
Expand All @@ -14,14 +15,20 @@ public class InterceptedBeanInjectionTest {

@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(Simple.class,
SimpleInterceptor.class, InterceptedBean.class);
SimpleInterceptor.class, InterceptedBean.class, InterceptedDependent.class);

@Test
public void testInterception() {
InterceptedBean bean = Arc.container().instance(InterceptedBean.class).get();
assertEquals(InterceptedBean.class.getName() + InterceptedBean.class.getName(), bean.ping());
assertEquals(InterceptedBean.class.getName(), SimpleInterceptor.aroundConstructResult);
assertEquals(InterceptedBean.class.getName(), SimpleInterceptor.postConstructResult);
assertEquals(
InterceptedBean.class.getName() + InterceptedDependent.class.getName() + InterceptedDependent.class.getName(),
bean.pong());
InterceptedDependent dependent = Arc.container().instance(InterceptedDependent.class).get();
assertEquals(InterceptedDependent.class.getName() + InterceptedDependent.class.getName(),
dependent.pong());
}

@ApplicationScoped
Expand All @@ -31,10 +38,27 @@ static class InterceptedBean {
@Inject
Bean<?> bean;

public String ping() {
@Inject
InterceptedDependent dependent;

String ping() {
return bean.getBeanClass().getName();
}

String pong() {
return dependent.pong();
}

}

@Dependent
static class InterceptedDependent {

@Simple
String pong() {
return InterceptedDependent.class.getName();
}

}

}

0 comments on commit 29ce4cc

Please sign in to comment.