Search in sources :

Example 6 with NamedLiteral

use of org.jboss.weld.literal.NamedLiteral in project core by weld.

the class ModifyingExtension method registerAdditionalFooAnnotatedType.

public void registerAdditionalFooAnnotatedType(@Observes BeforeBeanDiscovery event, BeanManager manager) {
    final AnnotatedType<Foo> anotherFoo = manager.createAnnotatedType(Foo.class);
    AnnotatedType<Foo> modifiedAnotherFoo = new ForwardingAnnotatedType<Foo>() {

        private final NamedLiteral qualifierInstance = new NamedLiteral("anotherFoo");

        @Override
        public AnnotatedType<Foo> delegate() {
            return anotherFoo;
        }

        @Override
        public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
            if (Named.class.equals(annotationType)) {
                return cast(qualifierInstance);
            }
            return null;
        }

        @Override
        public Set<Annotation> getAnnotations() {
            return Collections.<Annotation>singleton(qualifierInstance);
        }

        @Override
        public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
            return Named.class.equals(annotationType);
        }
    };
    event.addAnnotatedType(modifiedAnotherFoo);
}
Also used : ForwardingAnnotatedType(org.jboss.weld.util.annotated.ForwardingAnnotatedType) NamedLiteral(org.jboss.weld.literal.NamedLiteral) Annotation(java.lang.annotation.Annotation)

Example 7 with NamedLiteral

use of org.jboss.weld.literal.NamedLiteral in project core by weld.

the class ResolvableBuilder method addQualifier.

private ResolvableBuilder addQualifier(Annotation qualifier, InjectionPoint injectionPoint) {
    QualifierInstance qualifierInstance = QualifierInstance.of(qualifier, store);
    final Class<? extends Annotation> annotationType = qualifierInstance.getAnnotationClass();
    // Handle the @New qualifier special case
    if (annotationType.equals(New.class)) {
        New newQualifier = New.class.cast(qualifier);
        if (newQualifier.value().equals(New.class) && rawType == null) {
            throw new IllegalStateException("Cannot transform @New when there is no known raw type");
        } else if (newQualifier.value().equals(New.class)) {
            qualifier = new NewLiteral(rawType);
            qualifierInstance = QualifierInstance.of(qualifier, store);
        }
    } else if (injectionPoint != null && annotationType.equals(Named.class)) {
        Named named = (Named) qualifier;
        if (named.value().equals("")) {
            // WELD-1739
            // This is an injection point with an @Named qualifier, with no value specified, we need to assume the name of the field or parameter is the
            // value
            Member member = injectionPoint.getMember();
            if (member instanceof Executable) {
                // Method or constructor injection
                Executable executable = (Executable) member;
                AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
                Parameter parameter = executable.getParameters()[annotatedParameter.getPosition()];
                named = new NamedLiteral(parameter.getName());
            } else {
                named = new NamedLiteral(injectionPoint.getMember().getName());
            }
            qualifier = named;
            qualifierInstance = QualifierInstance.of(named, store);
        }
    }
    checkQualifier(qualifier, qualifierInstance, annotationType);
    this.qualifierInstances.add(qualifierInstance);
    return this;
}
Also used : New(javax.enterprise.inject.New) Named(javax.inject.Named) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) Parameter(java.lang.reflect.Parameter) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) NewLiteral(org.jboss.weld.literal.NewLiteral) Executable(java.lang.reflect.Executable) Member(java.lang.reflect.Member) NamedLiteral(org.jboss.weld.literal.NamedLiteral)

Example 8 with NamedLiteral

use of org.jboss.weld.literal.NamedLiteral in project core by weld.

the class VetoTest method testSpecializedBeanAvailableAfterSpecializingBeanVetoed.

@Test
public void testSpecializedBeanAvailableAfterSpecializingBeanVetoed(BeanManager manager, @Any Alpha alpha, VerifyingExtension extension) {
    Bean<?> bean = manager.resolve(manager.getBeans(Alpha.class, Any.Literal.INSTANCE));
    assertNotNull(bean);
    assertEquals(Bravo.class, bean.getBeanClass());
    assertEquals("alpha", bean.getName());
    verifyQualifiers(bean, Foo.Literal.INSTANCE, Bar.Literal.INSTANCE, new NamedLiteral("alpha"), Any.Literal.INSTANCE);
    assertNotNull(alpha);
    assertTrue(alpha instanceof Bravo);
    assertFalse(alpha instanceof Charlie);
    assertNull(extension.getAlpha());
    assertNotNull(extension.getBravo());
    assertNotNull(extension.getCharlie());
}
Also used : NamedLiteral(org.jboss.weld.literal.NamedLiteral) Test(org.junit.Test)

Example 9 with NamedLiteral

use of org.jboss.weld.literal.NamedLiteral in project wildfly-swarm by wildfly-swarm.

the class SocketBindingGroupExtension method afterBeanDiscovery.

@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    List<SimpleKey> configuredGroups = this.configView.simpleSubkeys(ROOT);
    for (SimpleKey groupName : configuredGroups) {
        Set<Bean<?>> groups = beanManager.getBeans(SocketBindingGroup.class, AnyLiteral.INSTANCE);
        AtomicBoolean producerRequired = new AtomicBoolean(false);
        if (groups.stream().noneMatch(e -> e.getQualifiers().stream().anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(groupName)))) {
            SocketBindingGroup group = new SocketBindingGroup(groupName.name(), null, "0");
            applyConfiguration(group);
            if (producerRequired.get()) {
                CommonBean<SocketBindingGroup> interfaceBean = CommonBeanBuilder.newBuilder(SocketBindingGroup.class).beanClass(SocketBindingGroupExtension.class).scope(ApplicationScoped.class).addQualifier(AnyLiteral.INSTANCE).addQualifier(new NamedLiteral(group.name())).createSupplier(() -> group).addType(SocketBindingGroup.class).addType(Object.class).build();
                abd.addBean(interfaceBean);
            }
        }
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnyLiteral(org.jboss.weld.literal.AnyLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) List(java.util.List) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) Observes(javax.enterprise.event.Observes) NamedLiteral(org.jboss.weld.literal.NamedLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Named(javax.inject.Named) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) CommonBeanBuilder(org.wildfly.swarm.spi.api.cdi.CommonBeanBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Named(javax.inject.Named) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) NamedLiteral(org.jboss.weld.literal.NamedLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

NamedLiteral (org.jboss.weld.literal.NamedLiteral)9 Named (javax.inject.Named)3 Test (org.junit.Test)3 Annotation (java.lang.annotation.Annotation)2 List (java.util.List)2 Set (java.util.Set)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Observes (javax.enterprise.event.Observes)2 AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)2 Bean (javax.enterprise.inject.spi.Bean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 AnyLiteral (org.jboss.weld.literal.AnyLiteral)2 CommonBean (org.wildfly.swarm.spi.api.cdi.CommonBean)2 CommonBeanBuilder (org.wildfly.swarm.spi.api.cdi.CommonBeanBuilder)2 ConfigKey (org.wildfly.swarm.spi.api.config.ConfigKey)2 ConfigView (org.wildfly.swarm.spi.api.config.ConfigView)2 SimpleKey (org.wildfly.swarm.spi.api.config.SimpleKey)2 Executable (java.lang.reflect.Executable)1 Member (java.lang.reflect.Member)1 Parameter (java.lang.reflect.Parameter)1