Search in sources :

Example 1 with Bean

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

the class BeanManagerImpl method removeUnusedBeans.

private void removeUnusedBeans() {
    String excludeTypeProperty = getServices().get(WeldConfiguration.class).getStringProperty(ConfigurationKey.UNUSED_BEANS_EXCLUDE_TYPE);
    if (UnusedBeans.isEnabled(excludeTypeProperty)) {
        String excludeAnnotationProperty = getServices().get(WeldConfiguration.class).getStringProperty(ConfigurationKey.UNUSED_BEANS_EXCLUDE_ANNOTATION);
        // Init exclude patterns
        Pattern excludeAnnotation = excludeAnnotationProperty.isEmpty() ? null : Pattern.compile(excludeAnnotationProperty);
        Pattern excludeType = UnusedBeans.excludeNone(excludeTypeProperty) ? null : Pattern.compile(excludeTypeProperty);
        Validator validator = getServices().get(Validator.class);
        // Build bean to declared producers and declared observers maps
        SetMultimap<Bean<?>, AbstractProducerBean<?, ?, ?>> beanToDeclaredProducers = SetMultimap.newSetMultimap();
        SetMultimap<Bean<?>, ObserverMethod<?>> beanToDeclaredObservers = SetMultimap.newSetMultimap();
        for (Bean<?> bean : enabledBeans) {
            if (bean instanceof AbstractProducerBean) {
                AbstractProducerBean<?, ?, ?> producer = (AbstractProducerBean<?, ?, ?>) bean;
                beanToDeclaredProducers.put(producer.getDeclaringBean(), producer);
            }
        }
        for (ObserverMethod<?> observerMethod : observers) {
            if (observerMethod instanceof ObserverMethodImpl) {
                ObserverMethodImpl<?, ?> observerMethodImpl = (ObserverMethodImpl<?, ?>) observerMethod;
                beanToDeclaredObservers.put(observerMethodImpl.getDeclaringBean(), observerMethod);
            }
        }
        Set<Bean<?>> removable = new HashSet<>();
        Set<Bean<?>> unusedProducers = new HashSet<>();
        WeldUnusedMetadataExtension metadataExtension = getUnusedMetadataExtension();
        for (Bean<?> bean : enabledBeans) {
            bean = Beans.unwrap(bean);
            // Built-in bean, extension, interceptor, decorator, session bean
            if (bean instanceof AbstractBuiltInBean || bean instanceof ExtensionBean || bean instanceof Interceptor || bean instanceof Decorator || bean instanceof SessionBean) {
                continue;
            }
            // Has a name
            if (bean.getName() != null) {
                continue;
            }
            // The type is excluded
            if (excludeType != null && excludeType.matcher(bean.getBeanClass().getName()).matches()) {
                continue;
            }
            // Declares an observer
            if (beanToDeclaredObservers.containsKey(bean)) {
                continue;
            }
            // Declares a used producer - see also second pass
            if (beanToDeclaredProducers.containsKey(bean)) {
                continue;
            }
            // Is resolved for an injection point
            if (validator.isResolved(bean) || (metadataExtension != null && (metadataExtension.isInjectedByEEComponent(bean, this) || metadataExtension.isInstanceResolvedBean(bean, this)))) {
                continue;
            }
            // The type is annotated with an exclude annotation
            if (excludeAnnotation != null && hasExcludeAnnotation(bean, excludeAnnotation)) {
                continue;
            }
            // This bean is very likely an unused producer
            if (bean instanceof AbstractProducerBean) {
                unusedProducers.add(bean);
            }
            BootstrapLogger.LOG.dropUnusedBeanMetadata(bean);
            removable.add(bean);
        }
        // Second pass to find beans which themselves are unused and declare only unused producers
        if (!unusedProducers.isEmpty()) {
            for (Bean<?> bean : beanToDeclaredProducers.keySet()) {
                bean = Beans.unwrap(bean);
                if (!unusedProducers.containsAll(beanToDeclaredProducers.get(bean))) {
                    continue;
                }
                BootstrapLogger.LOG.dropUnusedBeanMetadata(bean);
                removable.add(bean);
            }
        }
        if (!removable.isEmpty()) {
            // First remove unused beans from BeanManager
            enabledBeans.removeAll(removable);
            sharedBeans.removeAll(removable);
            // Then perform additional cleanup
            beanResolver.clear();
            // Removed beans are skipped in WeldStartup.endInitialization()
            cleanupBeansAfterBoot(removable);
            ((ContextualStoreImpl) getServices().get(ContextualStore.class)).removeAll(removable);
            getServices().get(ClassTransformer.class).removeAll(removable);
        }
    }
}
Also used : ClassTransformer(org.jboss.weld.resources.ClassTransformer) SessionBean(org.jboss.weld.bean.SessionBean) SessionBean(org.jboss.weld.bean.SessionBean) ExtensionBean(org.jboss.weld.bean.builtin.ExtensionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) WeldBean(org.jboss.weld.bean.WeldBean) RIBean(org.jboss.weld.bean.RIBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) Bean(jakarta.enterprise.inject.spi.Bean) WeldUnusedMetadataExtension(org.jboss.weld.bootstrap.WeldUnusedMetadataExtension) ObserverMethodImpl(org.jboss.weld.event.ObserverMethodImpl) Interceptor(jakarta.enterprise.inject.spi.Interceptor) ContainerLifecycleEventObserverMethod(org.jboss.weld.event.ContainerLifecycleEventObserverMethod) ObserverMethod(jakarta.enterprise.inject.spi.ObserverMethod) HashSet(java.util.HashSet) ContextualStoreImpl(org.jboss.weld.serialization.ContextualStoreImpl) Pattern(java.util.regex.Pattern) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) ExtensionBean(org.jboss.weld.bean.builtin.ExtensionBean) Decorator(jakarta.enterprise.inject.spi.Decorator) ContextualStore(org.jboss.weld.serialization.spi.ContextualStore) WeldConfiguration(org.jboss.weld.config.WeldConfiguration) AnnotatedTypeValidator(org.jboss.weld.annotated.AnnotatedTypeValidator) Validator(org.jboss.weld.bootstrap.Validator)

Example 2 with Bean

use of jakarta.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(jakarta.enterprise.inject.spi.BeanManager) Bean(jakarta.enterprise.inject.spi.Bean)

Example 3 with Bean

use of jakarta.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(jakarta.enterprise.inject.spi.BeanManager) Bean(jakarta.enterprise.inject.spi.Bean)

Example 4 with Bean

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

the class WeldInternalConstructsTest method testInterceptedProxiedBean.

@Test
public void testInterceptedProxiedBean() {
    InterceptedProxiedBean interceptedBean = holder.getInterceptedProxiedBean();
    // trigger interception and assert it works
    Assert.assertTrue(interceptedBean.ping());
    // should be instance of WeldConstruct and WeldClientProxy
    Assert.assertTrue(interceptedBean instanceof WeldConstruct);
    Assert.assertTrue(interceptedBean instanceof WeldClientProxy);
    // cast to WeldClientProxy and test the methods
    WeldClientProxy wcp = (WeldClientProxy) interceptedBean;
    WeldClientProxy.Metadata cm = wcp.getMetadata();
    Object contextualInstance = cm.getContextualInstance();
    // kind of indirect check that this is the actual contextual instance
    Assert.assertTrue(contextualInstance instanceof InterceptedProxiedBean);
    Assert.assertFalse(contextualInstance instanceof WeldClientProxy);
    // NOTE - contextual instance is still a Weld subclass because of interception/decoration
    Assert.assertTrue(contextualInstance instanceof WeldConstruct);
    Bean<?> bean = cm.getBean();
    Set<Bean<?>> beans = bm.getBeans(InterceptedProxiedBean.class);
    Assert.assertEquals(1, beans.size());
    Assert.assertEquals(((WeldBean) beans.iterator().next()).getIdentifier().asString(), ((WeldBean) bean).getIdentifier().asString());
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) WeldClientProxy(org.jboss.weld.proxy.WeldClientProxy) WeldConstruct(org.jboss.weld.proxy.WeldConstruct) Bean(jakarta.enterprise.inject.spi.Bean) WeldBean(org.jboss.weld.bean.WeldBean) Test(org.junit.Test)

Example 5 with Bean

use of jakarta.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().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) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) 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) Bean(jakarta.enterprise.inject.spi.Bean) BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) Deployment(org.jboss.arquillian.container.test.api.Deployment) 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)

Aggregations

Bean (jakarta.enterprise.inject.spi.Bean)57 Test (org.junit.Test)22 BeanManager (jakarta.enterprise.inject.spi.BeanManager)19 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)9 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)9 Annotation (java.lang.annotation.Annotation)9 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)9 HashSet (java.util.HashSet)8 SessionBean (org.jboss.weld.bean.SessionBean)8 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)7 AbstractProducerBean (org.jboss.weld.bean.AbstractProducerBean)6 Type (java.lang.reflect.Type)5 AbstractClassBean (org.jboss.weld.bean.AbstractClassBean)5 ExtensionBean (org.jboss.weld.bean.builtin.ExtensionBean)5 Named (jakarta.inject.Named)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 CommonBean (org.jboss.weld.bean.CommonBean)4 WeldBean (org.jboss.weld.bean.WeldBean)4 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)3