Search in sources :

Example 36 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class BeanDiscoveryAlternativeTest method testAnnotatedBeanDiscoveryAlternative.

@Test
public void testAnnotatedBeanDiscoveryAlternative(Plant representative) {
    BeanManager bm = representative.getBeanManager();
    Set<Bean<?>> treeBeans = bm.getBeans(Tree.class);
    assertEquals(2, treeBeans.size());
    assertEquals(AlternativeTree.class, bm.resolve(treeBeans).getBeanClass());
    Set<Bean<?>> stoneBeans = bm.getBeans(Stone.class);
    assertEquals(0, stoneBeans.size());
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 37 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class OptimizedCleanupTest method testDisabled.

@Test
public void testDisabled() {
    TestExtension.PIT_OBSERVED.set(false);
    try (WeldContainer container = new Weld().disableIsolation().addExtension(new TestExtension()).property(Weld.ALLOW_OPTIMIZED_CLEANUP, Boolean.FALSE).initialize()) {
        BeanManagerImpl beanManager = BeanManagerProxy.unwrap(container.getBeanManager());
        Bean<?> fooBean = beanManager.resolve(beanManager.getBeans(Foo.class));
        assertEquals(ApplicationScoped.class, fooBean.getScope());
        assertTrue(TestExtension.PIT_OBSERVED.get());
        // Find TestExtension.observeFooPit
        assertTrue(beanManager.getObservers().stream().anyMatch(o -> o.getBeanClass().equals(TestExtension.class)));
    }
}
Also used : ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Arquillian(org.jboss.arquillian.junit.Arquillian) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Archive(org.jboss.shrinkwrap.api.Archive) BeanManagerProxy(org.jboss.weld.bean.builtin.BeanManagerProxy) ClassPath(org.jboss.arquillian.container.se.api.ClassPath) WeldContainer(org.jboss.weld.environment.se.WeldContainer) BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) Deployment(org.jboss.arquillian.container.test.api.Deployment) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Bean(javax.enterprise.inject.spi.Bean) Assert.assertEquals(org.junit.Assert.assertEquals) Weld(org.jboss.weld.environment.se.Weld) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 38 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class Validator method checkBeanMetadataInjectionPoint.

public static void checkBeanMetadataInjectionPoint(Object bean, InjectionPoint ip, Type expectedTypeArgument) {
    if (!(ip.getType() instanceof ParameterizedType)) {
        throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
    }
    ParameterizedType parameterizedType = (ParameterizedType) ip.getType();
    if (parameterizedType.getActualTypeArguments().length != 1) {
        throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
    }
    Class<?> rawType = (Class<?>) parameterizedType.getRawType();
    Type typeArgument = parameterizedType.getActualTypeArguments()[0];
    if (bean == null) {
        throw ValidatorLogger.LOG.injectionIntoNonBean(ip, Formats.formatAsStackTraceElement(ip));
    }
    /*
         * If an Interceptor instance is injected into a bean instance other than an interceptor instance, the container
         * automatically detects the problem and treats it as a definition error.
         */
    if (rawType.equals(Interceptor.class) && !(bean instanceof Interceptor<?>)) {
        throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
    }
    /*
         * If a Decorator instance is injected into a bean instance other than a decorator instance, the container automatically
         * detects the problem and treats it as a definition error.
         */
    if (rawType.equals(Decorator.class) && !(bean instanceof Decorator<?>)) {
        throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
    }
    Set<Annotation> qualifiers = ip.getQualifiers();
    if (qualifiers.contains(InterceptedLiteral.INSTANCE)) {
        /*
             * If a Bean instance with qualifier @Intercepted is injected into a bean instance other than an interceptor
             * instance, the container automatically detects the problem and treats it as a definition error.
             */
        if (!(bean instanceof Interceptor<?>)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointQualifier(Intercepted.class, Interceptor.class, ip, Formats.formatAsStackTraceElement(ip));
        }
        /*
             * If the injection point is a field, an initializer method parameter or a bean constructor of an interceptor, with
             * qualifier @Intercepted, then the type parameter of the injected Bean must be an unbounded wildcard.
             */
        if (!rawType.equals(Bean.class)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
        }
        if (!Reflections.isUnboundedWildcard(typeArgument)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointTypeArgument(typeArgument, ip, Formats.formatAsStackTraceElement(ip));
        }
    }
    if (qualifiers.contains(DecoratedLiteral.INSTANCE)) {
        /*
             * If a Bean instance with qualifier @Decorated is injected into a bean instance other than a decorator instance,
             * the container automatically detects the problem and treats it as a definition error.
             */
        if (!(bean instanceof Decorator<?>)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointQualifier(Decorated.class, Decorator.class, ip, Formats.formatAsStackTraceElement(ip));
        }
        Decorator<?> decorator = Reflections.cast(bean);
        /*
             * If the injection point is a field, an initializer method parameter or a bean constructor of a decorator, with
             * qualifier @Decorated, then the type parameter of the injected Bean must be the same as the delegate type.
             */
        if (!rawType.equals(Bean.class)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointType(ip.getType(), ip, Formats.formatAsStackTraceElement(ip));
        }
        if (!typeArgument.equals(decorator.getDelegateType())) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointTypeArgument(typeArgument, ip, Formats.formatAsStackTraceElement(ip));
        }
    }
    if (qualifiers.contains(DefaultLiteral.INSTANCE)) {
        /*
             * If the injection point is a field, an initializer method parameter or a bean constructor, with qualifier
             * @Default, then the type parameter of the injected Bean, Interceptor or Decorator must be the same as the type
             * declaring the injection point.
             *
             * If the injection point is a producer method parameter then the type parameter of the injected Bean must be the
             * same as the producer method return type.
             *
             * If the injection point is a disposer method parameter then the type parameter of the injected Bean must be the
             * same as the disposed parameter.
             */
        if (!expectedTypeArgument.equals(typeArgument)) {
            throw ValidatorLogger.LOG.invalidBeanMetadataInjectionPointTypeArgument(typeArgument, ip, Formats.formatAsStackTraceElement(ip));
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WeldDecorator(org.jboss.weld.bean.WeldDecorator) Decorator(javax.enterprise.inject.spi.Decorator) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) EnhancedAnnotatedType(org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType) Interceptor(javax.enterprise.inject.spi.Interceptor) Annotation(java.lang.annotation.Annotation) SessionBean(org.jboss.weld.bean.SessionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) DecorableBean(org.jboss.weld.bean.DecorableBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) NewBean(org.jboss.weld.bean.NewBean) AbstractBean(org.jboss.weld.bean.AbstractBean) AbstractDecorableBuiltInBean(org.jboss.weld.bean.builtin.AbstractDecorableBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) CommonBean(org.jboss.weld.bean.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Example 39 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class War2Listener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    // validate accessibility from the war1 module
    Set<Bean<?>> accessibleImplementations = manager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War2Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library2Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the war1 library module
    Bean<?> library2ImplBean = getUniqueBean(accessibleImplementations, Library2Impl.class);
    Library2Impl library2Impl = (Library2Impl) manager.getReference(library2ImplBean, Animal.class, manager.createCreationalContext(library2ImplBean));
    BeanManager library2BeanManager = library2Impl.getBeanManager();
    accessibleImplementations = library2BeanManager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War2Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library2Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 1
    Bean<?> sharedLibrary1ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary1Impl.class);
    SharedLibrary1Impl sharedLibrary1Impl = (SharedLibrary1Impl) manager.getReference(sharedLibrary1ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary1ImplBean));
    BeanManager sharedLibrary1BeanManager = sharedLibrary1Impl.getBeanManager();
    accessibleImplementations = sharedLibrary1BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 2
    Bean<?> sharedLibrary2ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary2Impl.class);
    SharedLibrary2Impl sharedLibrary2Impl = (SharedLibrary2Impl) manager.getReference(sharedLibrary2ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary2ImplBean));
    BeanManager sharedLibrary2BeanManager = sharedLibrary2Impl.getBeanManager();
    accessibleImplementations = sharedLibrary2BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 40 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class War1Listener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    // validate accessibility from the war1 module
    Set<Bean<?>> accessibleImplementations = manager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War1Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the war1 library module
    Bean<?> library1ImplBean = getUniqueBean(accessibleImplementations, Library1Impl.class);
    Library1Impl library1Impl = (Library1Impl) manager.getReference(library1ImplBean, Animal.class, manager.createCreationalContext(library1ImplBean));
    BeanManager library1BeanManager = library1Impl.getBeanManager();
    accessibleImplementations = library1BeanManager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War1Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 1
    Bean<?> sharedLibrary1ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary1Impl.class);
    SharedLibrary1Impl sharedLibrary1Impl = (SharedLibrary1Impl) manager.getReference(sharedLibrary1ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary1ImplBean));
    BeanManager sharedLibrary1BeanManager = sharedLibrary1Impl.getBeanManager();
    accessibleImplementations = sharedLibrary1BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 2
    Bean<?> sharedLibrary2ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary2Impl.class);
    SharedLibrary2Impl sharedLibrary2Impl = (SharedLibrary2Impl) manager.getReference(sharedLibrary2ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary2ImplBean));
    BeanManager sharedLibrary2BeanManager = sharedLibrary2Impl.getBeanManager();
    accessibleImplementations = sharedLibrary2BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)259 Test (org.junit.Test)119 CreationalContext (javax.enterprise.context.spi.CreationalContext)110 BeanManager (javax.enterprise.inject.spi.BeanManager)76 URL (java.net.URL)68 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 HashSet (java.util.HashSet)31 Annotation (java.lang.annotation.Annotation)18 ArrayList (java.util.ArrayList)14 Module (org.guvnor.common.services.project.model.Module)13 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)11 Type (java.lang.reflect.Type)9 Set (java.util.Set)9 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)9 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)9 Any (javax.enterprise.inject.Any)8 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)8 Weld (org.jboss.weld.environment.se.Weld)8