Search in sources :

Example 1 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project wildfly by wildfly.

the class WeldResourceInjectionServices method resolveResource.

public Object resolveResource(InjectionPoint injectionPoint) {
    final Member member = injectionPoint.getMember();
    AnnotatedMember<?> annotatedMember;
    if (injectionPoint.getAnnotated() instanceof AnnotatedField) {
        annotatedMember = (AnnotatedField<?>) injectionPoint.getAnnotated();
    } else {
        annotatedMember = ((AnnotatedParameter<?>) injectionPoint.getAnnotated()).getDeclaringCallable();
    }
    if (!annotatedMember.isAnnotationPresent(Resource.class)) {
        throw WeldLogger.ROOT_LOGGER.annotationNotFound(Resource.class, member);
    }
    if (member instanceof Method && ((Method) member).getParameterCount() != 1) {
        throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) member);
    }
    String name = getResourceName(injectionPoint);
    for (ResourceInjectionResolver resolver : resourceResolvers) {
        Object result = resolver.resolve(name);
        if (result != null) {
            return result;
        }
    }
    try {
        return context.lookup(name);
    } catch (NamingException e) {
        throw WeldLogger.ROOT_LOGGER.couldNotFindResource(name, injectionPoint.getMember().toString(), e);
    }
}
Also used : Resource(javax.annotation.Resource) ResourceInjectionResolver(org.jboss.as.weld.spi.ResourceInjectionResolver) NamingException(javax.naming.NamingException) Method(java.lang.reflect.Method) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) Member(java.lang.reflect.Member)

Example 2 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project tomee by apache.

the class CdiPlugin method defineSessionBean.

@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
    final BeanContext bc = findBeanContext(webBeansContext, clazz);
    final Class<?> superClass = bc.getManagedClass().getSuperclass();
    if (annotatedType.isAnnotationPresent(Specializes.class)) {
        if (superClass != Object.class && !isSessionBean(superClass)) {
            throw new DefinitionException("You can only specialize another EJB: " + clazz);
        }
        final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
        final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
        for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
            businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
        }
        if (!businessLocalInterfaces.isEmpty()) {
            throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
        }
    }
    final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
    bc.set(CdiEjbBean.class, bean);
    bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
    validateDisposeMethods(bean);
    validateScope(bean);
    final Set<ObserverMethod<?>> observerMethods;
    if (bean.isEnabled()) {
        observerMethods = new ObserverMethodsBuilder<>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean, true);
    } else {
        observerMethods = new HashSet<>();
    }
    final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
    final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
    final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
    final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
    for (final ProducerMethodBean<?> producerMethod : producerMethods) {
        final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
        annotatedMethods.put(producerMethod, method);
    }
    final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
    for (final ProducerFieldBean<?> producerField : producerFields) {
        if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
            throw new DefinitionException("In an EJB all producer fields should be static");
        }
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
        annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
    }
    final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
    for (final ObserverMethod<?> observerMethod : observerMethods) {
        final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
        final AnnotatedMethod<?> method = impl.getObserverMethod();
        observerMethodsMap.put(observerMethod, method);
    }
    validateProduceMethods(bean, producerMethods);
    validateObserverMethods(bean, observerMethodsMap);
    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    // Fires ProcessManagedBean
    final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
    beanManager.fireEvent(event, true);
    event.setStarted();
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
    // Fires ProcessProducerMethod
    webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
    // Fires ProcessProducerField
    webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
    if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
        for (final ProducerMethodBean<?> producerMethod : producerMethods) {
            beanManager.addBean(producerMethod);
        }
        for (final ProducerFieldBean<?> producerField : producerFields) {
            beanManager.addBean(producerField);
        }
    }
    beanManager.addBean(bean);
    return bean;
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) Bean(javax.enterprise.inject.spi.Bean) OwbBean(org.apache.webbeans.component.OwbBean) AbstractOwbBean(org.apache.webbeans.component.AbstractOwbBean) ProducerFieldBeansBuilder(org.apache.webbeans.component.creation.ProducerFieldBeansBuilder) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ObserverMethodsBuilder(org.apache.webbeans.component.creation.ObserverMethodsBuilder) ObserverMethodImpl(org.apache.webbeans.event.ObserverMethodImpl) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) BeanContext(org.apache.openejb.BeanContext) WebBeansUtil(org.apache.webbeans.util.WebBeansUtil) ProducerMethodBeansBuilder(org.apache.webbeans.component.creation.ProducerMethodBeansBuilder) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 3 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project Payara by payara.

the class HK2IntegrationUtilities method convertInjectionPointToInjectee.

public static Injectee convertInjectionPointToInjectee(InjectionPoint injectionPoint) {
    InjecteeImpl retVal = new InjecteeImpl(injectionPoint.getType());
    retVal.setRequiredQualifiers(getHK2Qualifiers(injectionPoint));
    // Also sets InjecteeClass
    retVal.setParent((AnnotatedElement) injectionPoint.getMember());
    Annotated annotated = injectionPoint.getAnnotated();
    if (annotated instanceof AnnotatedField) {
        retVal.setPosition(-1);
    } else {
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) annotated;
        retVal.setPosition(annotatedParameter.getPosition());
    }
    return retVal;
}
Also used : Annotated(javax.enterprise.inject.spi.Annotated) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) InjecteeImpl(org.glassfish.hk2.utilities.InjecteeImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 4 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project Payara by payara.

the class HK2IntegrationUtilities method getHK2Qualifiers.

private static Set<Annotation> getHK2Qualifiers(InjectionPoint injectionPoint) {
    Set<Annotation> setQualifiers = injectionPoint.getQualifiers();
    Set<Annotation> retVal = new HashSet<Annotation>();
    for (Annotation anno : setQualifiers) {
        if (anno.annotationType().equals(Default.class))
            continue;
        if (anno.annotationType().equals(Named.class)) {
            Named named = (Named) anno;
            if ("".equals(named.value())) {
                Annotated annotated = injectionPoint.getAnnotated();
                if (annotated instanceof AnnotatedField) {
                    AnnotatedField<?> annotatedField = (AnnotatedField<?>) annotated;
                    Field field = annotatedField.getJavaMember();
                    anno = new NamedImpl(field.getName());
                }
            }
        }
        retVal.add(anno);
    }
    return retVal;
}
Also used : Named(javax.inject.Named) Annotated(javax.enterprise.inject.spi.Annotated) Field(java.lang.reflect.Field) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) NamedImpl(org.glassfish.hk2.utilities.NamedImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 5 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project Payara by payara.

the class ConfigPropertiesProducer method getGenericObject.

@ConfigProperties
public static final Object getGenericObject(InjectionPoint injectionPoint, BeanManager bm) throws InstantiationException, IllegalAccessException {
    Type type = injectionPoint.getType();
    if (!(type instanceof Class)) {
        throw new IllegalArgumentException("Unable to process injection point with @ConfigProperties of type " + type);
    }
    // Initialise the object. This may throw exceptions
    final Object object = ((Class) type).newInstance();
    // Model the class
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType((Class) type);
    // Find the @ConfigProperties annotations, and calculate the property prefix
    final ConfigProperties injectionAnnotation = getQualifier(injectionPoint);
    final ConfigProperties classAnnotation = annotatedType.getAnnotation(ConfigProperties.class);
    final String prefix = parsePrefixes(injectionAnnotation, classAnnotation);
    for (AnnotatedField<?> field : annotatedType.getFields()) {
        // Find the java field and field name
        final Field javaField = field.getJavaMember();
        // Make sure the field is accessible
        javaField.setAccessible(true);
        // Model the field
        final InjectionPoint fieldInjectionPoint = bm.createInjectionPoint(field);
        final ConfigPropertyModel model = new ConfigPropertyModel(fieldInjectionPoint, prefix);
        try {
            final Object value = ConfigPropertyProducer.getGenericPropertyFromModel(model);
            if (value != null) {
                javaField.set(object, value);
            }
        } catch (Exception ex) {
            if (javaField.get(object) == null) {
                LOGGER.log(Level.WARNING, String.format("Unable to inject property with name %s into type %s.", model.getName(), type.getTypeName()), ex);
                throw ex;
            }
        }
    }
    return object;
}
Also used : Field(java.lang.reflect.Field) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) ConfigPropertyModel(fish.payara.microprofile.config.cdi.model.ConfigPropertyModel) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties)

Aggregations

AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)9 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)4 HashSet (java.util.HashSet)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 Member (java.lang.reflect.Member)2 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 Annotated (javax.enterprise.inject.spi.Annotated)2 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)2 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)2 Named (javax.inject.Named)2 ConfigPropertyModel (fish.payara.microprofile.config.cdi.model.ConfigPropertyModel)1 Executable (java.lang.reflect.Executable)1 TypeVariable (java.lang.reflect.TypeVariable)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 WeakHashMap (java.util.WeakHashMap)1