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);
}
}
}
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));
}
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));
}
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());
}
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)));
}
}
Aggregations