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

WELD-1736 Session bean's subclass constructor used for building interception model #647

Merged
merged 2 commits into from Sep 4, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -103,10 +103,14 @@ protected void initializeInterceptionModel(EnhancedAnnotatedType<T> annotatedTyp
return; // this is a non-producible InjectionTarget (only created to inject existing instances)
}
if (isInterceptionCandidate() && !beanManager.getInterceptorModelRegistry().containsKey(getType())) {
new InterceptionModelInitializer<T>(beanManager, annotatedType, instantiator.getConstructorInjectionPoint().getAnnotated(), getBean()).init();
buildInterceptionModel(annotatedType, instantiator);
}
}

protected void buildInterceptionModel(EnhancedAnnotatedType<T> annotatedType, AbstractInstantiator<T> instantiator) {
new InterceptionModelInitializer<T>(beanManager, annotatedType, instantiator.getConstructorInjectionPoint().getAnnotated(), getBean()).init();
}

@Override
public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
initializeInterceptionModel(annotatedType);
Expand Down
Expand Up @@ -26,6 +26,7 @@
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedConstructor;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler;
Expand All @@ -38,6 +39,7 @@
import org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker;
import org.jboss.weld.injection.producer.Injector;
import org.jboss.weld.injection.producer.Instantiator;
import org.jboss.weld.injection.producer.InterceptionModelInitializer;
import org.jboss.weld.injection.producer.LifecycleCallbackInvoker;
import org.jboss.weld.injection.producer.StatelessSessionBeanInjector;
import org.jboss.weld.injection.producer.SubclassDecoratorApplyingInstantiator;
Expand Down Expand Up @@ -109,6 +111,16 @@ public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType)
setupConstructorInterceptionInstantiator(beanManager.getInterceptorModelRegistry().get(getType()));
}

@Override
protected void buildInterceptionModel(EnhancedAnnotatedType<T> annotatedType, AbstractInstantiator<T> instantiator) {
/*
* instantiator.getConstructorInjectionPoint() may represent the constructor of the SessionBean subclass which may not have annotations applied
* Therefore, use the component class constructor instead of the one from subclass.
*/
EnhancedAnnotatedConstructor<T> constructor = annotatedType.getDeclaredEnhancedConstructor(instantiator.getConstructorInjectionPoint().getSignature());
new InterceptionModelInitializer<T>(beanManager, annotatedType, constructor, getBean()).init();
}

@Override
public T produce(CreationalContext<T> ctx) {
T result = super.produce(ctx);
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.jboss.weld.tests.unit.ejb.subclass;

import javax.annotation.Priority;
import javax.interceptor.AroundConstruct;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
Expand All @@ -26,6 +27,15 @@
@BarInterceptorBinding
public class BarInterceptor {

@AroundConstruct
public void aroundConstruct(InvocationContext ctx) {
try {
ctx.proceed();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@AroundInvoke
public Object alwaysReturnThis(InvocationContext ctx) throws Exception {
return ctx.proceed();
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld.tests.unit.ejb.subclass;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
Expand All @@ -27,7 +28,7 @@

import javax.interceptor.InterceptorBinding;

@Target({ TYPE, METHOD })
@Target({ TYPE, METHOD, CONSTRUCTOR })
@Retention(RUNTIME)
@Documented
@Inherited
Expand Down
Expand Up @@ -29,6 +29,7 @@ public Foo() {
}

@Inject
@BarInterceptorBinding
public Foo(BeanManager manager) {
this.manager = manager;
}
Expand Down
Expand Up @@ -34,10 +34,13 @@
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.ejb.spi.EjbServices;
import org.jboss.weld.ejb.spi.SubclassedComponentDescriptor;
import org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget;
import org.jboss.weld.interceptor.spi.model.InterceptionModel;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.mock.AbstractDeployment;
import org.jboss.weld.mock.MockEjbServices;
import org.jboss.weld.util.reflection.Reflections;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableSet;
Expand All @@ -51,9 +54,10 @@
*/
public class SubclassedComponentDescriptorTest {

@Test
public void testSubclassedComponentDescriptor() {
private BeanManagerImpl manager;

@BeforeClass
public void prepareContainer() {
final EjbDescriptor<Foo> fooDescriptor = new EjbDescriptorImpl<Foo>(Foo.class, Foo.class, EnhancedFoo.class, SessionBeanType.STATEFUL);
final EjbDescriptor<Bar> barDescriptor = new EjbDescriptorImpl<Bar>(Bar.class, BarLocal.class, EnhancedBar.class, SessionBeanType.STATEFUL);
final EjbDescriptor<Baz> bazDescriptor = new EjbDescriptorImpl<Baz>(Baz.class, Baz.class, EnhancedBaz.class, null);
Expand Down Expand Up @@ -87,7 +91,11 @@ protected void configureServices() {
};

final TestContainer container = new TestContainer(deployment).startContainer();
final BeanManagerImpl manager = (BeanManagerImpl) container.getBeanManager(bda);
manager = (BeanManagerImpl) container.getBeanManager(bda);
}

@Test
public void testSubclassedComponentDescriptor() {
Foo foo = (Foo) manager.createInjectionTarget(manager.getEjbDescriptor(Foo.class.getSimpleName())).produce(manager.createCreationalContext(null));
Bar bar = (Bar) manager.createInjectionTarget(manager.getEjbDescriptor(Bar.class.getSimpleName())).produce(manager.createCreationalContext(null));
Baz baz = (Baz) manager.createInjectionTarget(manager.getEjbDescriptor(Baz.class.getSimpleName())).produce(manager.createCreationalContext(null));
Expand All @@ -105,12 +113,21 @@ protected void configureServices() {
assertTrue(baz instanceof Enhanced);
assertTrue(Reflections.<Enhanced>cast(baz).check());

assertEquals(MockEjbServices.getDescriptors().size(), 1);
assertEquals(MockEjbServices.getDescriptors().iterator().next().getBeanClass(), Bar.class);

assertNotNull(foo.getManager());
assertNotNull(bar.getManager());
assertNotNull(baz.getManager());

assertEquals(MockEjbServices.getDescriptors().size(), 2);
assertTrue(MockEjbServices.getDescriptors().contains(manager.getEjbDescriptor(Foo.class.getSimpleName())));
assertTrue(MockEjbServices.getDescriptors().contains(manager.getEjbDescriptor(Bar.class.getSimpleName())));
}

@Test
public void testInterceptionModelForConstructor() {
SessionBeanInjectionTarget<?> it = (SessionBeanInjectionTarget<?>) manager.createInjectionTarget(manager.getEjbDescriptor(Foo.class.getSimpleName()));
InterceptionModel model = manager.getInterceptorModelRegistry().get(it.getAnnotated());
assertNotNull(model);
assertTrue(model.hasExternalConstructorInterceptors());
}

private static class EjbDescriptorImpl<T> implements EjbDescriptor<T>, SubclassedComponentDescriptor<T> {
Expand Down