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