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