Search in sources :

Example 1 with New

use of javax.enterprise.inject.New in project core by weld.

the class BeanDeployerEnvironment method addNewBeansFromInjectionPoints.

public void addNewBeansFromInjectionPoints(Set<? extends InjectionPoint> injectionPoints) {
    for (InjectionPoint injectionPoint : injectionPoints) {
        WeldInjectionPointAttributes<?, ?> weldInjectionPoint = InjectionPoints.getWeldInjectionPoint(injectionPoint);
        if (weldInjectionPoint.getQualifier(New.class) != null) {
            Class<?> rawType = Reflections.getRawType(weldInjectionPoint.getType());
            if (Event.class.equals(rawType)) {
                continue;
            }
            New _new = weldInjectionPoint.getQualifier(New.class);
            if (_new.value().equals(New.class)) {
                if (rawType.equals(Instance.class)) {
                    // e.g. @Inject @New(ChequePaymentProcessor.class) Instance<PaymentProcessor> chequePaymentProcessor;
                    // see WELD-975
                    Type typeParameter = Reflections.getActualTypeArguments(weldInjectionPoint.getType())[0];
                    addNewBeanFromInjectionPoint(typeParameter);
                } else {
                    addNewBeanFromInjectionPoint(weldInjectionPoint.getType());
                }
            } else {
                addNewBeanFromInjectionPoint(_new.value());
            }
        }
    }
}
Also used : New(javax.enterprise.inject.New) Type(java.lang.reflect.Type) SlimAnnotatedType(org.jboss.weld.annotated.slim.SlimAnnotatedType) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint)

Example 2 with New

use of javax.enterprise.inject.New 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)

Aggregations

New (javax.enterprise.inject.New)2 Executable (java.lang.reflect.Executable)1 Member (java.lang.reflect.Member)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 Named (javax.inject.Named)1 SlimAnnotatedType (org.jboss.weld.annotated.slim.SlimAnnotatedType)1 NamedLiteral (org.jboss.weld.literal.NamedLiteral)1 NewLiteral (org.jboss.weld.literal.NewLiteral)1