Search in sources :

Example 1 with CreationalContext

use of jakarta.enterprise.context.spi.CreationalContext in project core by weld.

the class GlueDecoratorExtension method registerDecorator.

void registerDecorator(@Observes AfterBeanDiscovery event, BeanManager manager) {
    AnnotatedType<GlueDecorator> annotatedType = manager.createAnnotatedType(GlueDecorator.class);
    final BeanAttributes<GlueDecorator> attributes = manager.createBeanAttributes(annotatedType);
    final InjectionPoint delegateInjectionPoint = manager.createInjectionPoint(annotatedType.getConstructors().iterator().next().getParameters().get(0));
    Decorator<GlueDecorator> decorator = new DecoratorImpl<GlueDecorator>() {

        @Override
        public Type getDelegateType() {
            return Glue.class;
        }

        @Override
        public Set<Annotation> getDelegateQualifiers() {
            return Collections.<Annotation>singleton(Any.Literal.INSTANCE);
        }

        @Override
        public Set<Type> getDecoratedTypes() {
            return Collections.emptySet();
        }

        @Override
        public Class<?> getBeanClass() {
            return GlueDecorator.class;
        }

        @Override
        public Set<InjectionPoint> getInjectionPoints() {
            final Decorator<GlueDecorator> decorator = this;
            InjectionPoint wrappedInjectionPoint = new ForwardingInjectionPoint() {

                @Override
                public Bean<?> getBean() {
                    return decorator;
                }

                @Override
                protected InjectionPoint delegate() {
                    return delegateInjectionPoint;
                }
            };
            return Collections.singleton(wrappedInjectionPoint);
        }

        @Override
        public GlueDecorator create(CreationalContext<GlueDecorator> creationalContext) {
            return new GlueDecorator(null);
        }

        @Override
        public void destroy(GlueDecorator instance, CreationalContext<GlueDecorator> creationalContext) {
            creationalContext.release();
        }

        @Override
        protected BeanAttributes<GlueDecorator> attributes() {
            return attributes;
        }
    };
    event.addBean(decorator);
}
Also used : CreationalContext(jakarta.enterprise.context.spi.CreationalContext) Type(java.lang.reflect.Type) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) ForwardingInjectionPoint(org.jboss.weld.injection.ForwardingInjectionPoint) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) ForwardingInjectionPoint(org.jboss.weld.injection.ForwardingInjectionPoint) Annotation(java.lang.annotation.Annotation)

Example 2 with CreationalContext

use of jakarta.enterprise.context.spi.CreationalContext in project core by weld.

the class Ejb3InterceptionModelTest method testSimpleInterceptor3.

@Test
public void testSimpleInterceptor3() {
    reset();
    Bean bean = beanManager.getBeans(Ball.class).iterator().next();
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    Ball ball = (Ball) bean.create(creationalContext);
    ball.lob();
    assert !Defender.defended;
    assert Ball.played;
    assert Goalkeeper.caught;
    assert Ball.aroundInvoke;
}
Also used : CreationalContext(jakarta.enterprise.context.spi.CreationalContext) Bean(jakarta.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 3 with CreationalContext

use of jakarta.enterprise.context.spi.CreationalContext in project core by weld.

the class Ejb3InterceptionModelTest method testSimpleInterceptor.

@Test
public void testSimpleInterceptor() {
    reset();
    Bean bean = beanManager.getBeans(Ball.class).iterator().next();
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    Ball ball = (Ball) bean.create(creationalContext);
    ball.shoot();
    assert Defender.defended;
    assert Ball.played;
    assert !Goalkeeper.caught;
    assert Ball.aroundInvoke;
}
Also used : CreationalContext(jakarta.enterprise.context.spi.CreationalContext) Bean(jakarta.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 4 with CreationalContext

use of jakarta.enterprise.context.spi.CreationalContext in project core by weld.

the class Ejb3InterceptionModelTest method testSimpleInterceptor2.

@Test
public void testSimpleInterceptor2() {
    reset();
    Bean bean = beanManager.getBeans(Ball.class).iterator().next();
    CreationalContext creationalContext = beanManager.createCreationalContext(bean);
    Ball ball = (Ball) bean.create(creationalContext);
    ball.pass();
    assert Defender.defended;
    assert Ball.played;
    assert Goalkeeper.caught;
    assert Ball.aroundInvoke;
}
Also used : CreationalContext(jakarta.enterprise.context.spi.CreationalContext) Bean(jakarta.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 5 with CreationalContext

use of jakarta.enterprise.context.spi.CreationalContext in project core by weld.

the class PreDestroyTest method testLifecycle.

@Test
@SuppressWarnings({ "unchecked" })
public void testLifecycle(BeanManager bm) throws Exception {
    for (Bean b : bm.getBeans(TestBean.class)) {
        CreationalContext ctx = bm.createCreationalContext(null);
        TestBean testBean = (TestBean) bm.getReference(b, b.getBeanClass(), ctx);
        b.destroy(testBean, ctx);
        Assert.assertEquals("PreDestroy invocation number.", 1, testBean.getCounter());
    }
}
Also used : CreationalContext(jakarta.enterprise.context.spi.CreationalContext) Bean(jakarta.enterprise.inject.spi.Bean) Test(org.junit.Test)

Aggregations

CreationalContext (jakarta.enterprise.context.spi.CreationalContext)15 Bean (jakarta.enterprise.inject.spi.Bean)7 Test (org.junit.Test)6 AnnotatedType (jakarta.enterprise.inject.spi.AnnotatedType)5 Contextual (jakarta.enterprise.context.spi.Contextual)3 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)3 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 BeanManager (jakarta.enterprise.inject.spi.BeanManager)2 InjectionTarget (jakarta.enterprise.inject.spi.InjectionTarget)2 FacesContext (jakarta.faces.context.FacesContext)2 Annotation (java.lang.annotation.Annotation)2 List (java.util.List)2 Util.getCdiBeanManager (com.sun.faces.util.Util.getCdiBeanManager)1 NativeImageHelper (io.helidon.common.NativeImageHelper)1 ConfigException (io.helidon.config.ConfigException)1 MpConfig (io.helidon.config.mp.MpConfig)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 ContextNotActiveException (jakarta.enterprise.context.ContextNotActiveException)1 Dependent (jakarta.enterprise.context.Dependent)1