use of javax.enterprise.inject.spi.AnnotatedType in project deltaspike by apache.
the class BeanBuilder method readFromType.
/**
* <p>
* Read the {@link AnnotatedType}, creating a bean from the class and it's
* annotations.
* </p>
* <p/>
* <p>
* By default the bean lifecycle will wrap the result of calling
* {@link BeanManager#createInjectionTarget(AnnotatedType)}.
* </p>
* <p/>
* <p>
* {@link BeanBuilder} does <em>not</em> support reading members of the class
* to create producers or observer methods.
* </p>
*
* @param type the type to read
*/
public BeanBuilder<T> readFromType(AnnotatedType<T> type) {
this.beanClass = type.getJavaClass();
if (beanLifecycle == null) {
setDefaultBeanLifecycle(type);
}
this.qualifiers = new HashSet<Annotation>();
this.stereotypes = new HashSet<Class<? extends Annotation>>();
this.types = new HashSet<Type>();
for (Annotation annotation : type.getAnnotations()) {
if (beanManager.isQualifier(annotation.annotationType())) {
this.qualifiers.add(annotation);
} else if (beanManager.isScope(annotation.annotationType())) {
this.scope = annotation.annotationType();
} else if (beanManager.isStereotype(annotation.annotationType())) {
this.stereotypes.add(annotation.annotationType());
}
if (annotation instanceof Named) {
this.name = ((Named) annotation).value();
if (name == null || name.length() == 0) {
name = createDefaultBeanName(type);
}
}
if (annotation instanceof Alternative) {
this.alternative = true;
}
}
if (type.isAnnotationPresent(Typed.class)) {
Typed typed = type.getAnnotation(Typed.class);
this.types.addAll(Arrays.asList(typed.value()));
} else {
for (Class<?> c = type.getJavaClass(); c != Object.class && c != null; c = c.getSuperclass()) {
this.types.add(c);
}
Collections.addAll(this.types, type.getJavaClass().getInterfaces());
this.types.add(Object.class);
}
if (qualifiers.isEmpty()) {
qualifiers.add(new DefaultLiteral());
}
qualifiers.add(new AnyLiteral());
this.id = ImmutableBeanWrapper.class.getName() + ":" + Annotateds.createTypeId(type);
return this;
}
use of javax.enterprise.inject.spi.AnnotatedType in project tomee by apache.
the class WebContext method getConstructorInjectionBean.
private ConstructorInjectionBean<Object> getConstructorInjectionBean(final Class beanClass, final WebBeansContext webBeansContext) {
if (webBeansContext == null) {
return null;
}
ConstructorInjectionBean<Object> beanDefinition = constructorInjectionBeanCache.get(beanClass);
if (beanDefinition == null) {
synchronized (this) {
beanDefinition = constructorInjectionBeanCache.get(beanClass);
if (beanDefinition == null) {
final AnnotatedType annotatedType = webBeansContext.getAnnotatedElementFactory().newAnnotatedType(beanClass);
if (isWeb(beanClass)) {
beanDefinition = new ConstructorInjectionBean<>(webBeansContext, beanClass, annotatedType, false);
} else {
beanDefinition = new ConstructorInjectionBean<>(webBeansContext, beanClass, annotatedType);
}
constructorInjectionBeanCache.put(beanClass, beanDefinition);
}
}
}
return beanDefinition;
}
use of javax.enterprise.inject.spi.AnnotatedType in project tomee by apache.
the class BValInterceptor method computeIsConstructorValidated.
private <T> boolean computeIsConstructorValidated(Class<T> targetClass, Constructor<T> ctor) {
final AnnotatedType<T> annotatedType = CDI.current().getBeanManager().createAnnotatedType(ctor.getDeclaringClass());
final ValidateOnExecution annotation = annotatedType.getConstructors().stream().filter(ac -> ctor.equals(ac.getJavaMember())).findFirst().map(ac -> ac.getAnnotation(ValidateOnExecution.class)).orElseGet(() -> ctor.getAnnotation(ValidateOnExecution.class));
final Set<ExecutableType> validatedExecutableTypes = annotation == null ? classConfiguration : ExecutableTypes.interpret(annotation.type());
return validatedExecutableTypes.contains(ExecutableType.CONSTRUCTORS);
}
use of javax.enterprise.inject.spi.AnnotatedType 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;
}
use of javax.enterprise.inject.spi.AnnotatedType in project meecrowave by apache.
the class InterceptorBase method intercept.
protected Object intercept(final InvocationContext ic) throws Exception {
final boolean forbidsUt = doesForbidUtUsage();
final RuntimeException oldEx;
final IllegalStateException illegalStateException;
if (forbidsUt) {
illegalStateException = ILLEGAL_STATE_EXCEPTION;
oldEx = error(illegalStateException);
} else {
illegalStateException = null;
oldEx = null;
}
State state = null;
try {
state = start();
final Object proceed = ic.proceed();
// force commit there to ensure we can catch synchro exceptions
commit(state);
return proceed;
} catch (final Exception e) {
if (illegalStateException == e) {
throw e;
}
Exception error = unwrap(e);
if (error != null && (!config.isHandleExceptionOnlyForClient() || isNewTransaction(state))) {
final Method method = ic.getMethod();
if (rollback == null) {
synchronized (this) {
if (rollback == null) {
rollback = new ConcurrentHashMap<>();
}
}
}
Boolean doRollback = rollback.get(method);
if (doRollback != null) {
if (doRollback && isTransactionActive(state.current)) {
setRollbackOnly();
}
} else {
// computed lazily but we could cache it later for sure if that's really a normal case
final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(method.getDeclaringClass());
Transactional tx = null;
for (final AnnotatedMethod<?> m : annotatedType.getMethods()) {
if (method.equals(m.getJavaMember())) {
tx = m.getAnnotation(Transactional.class);
break;
}
}
if (tx == null) {
tx = annotatedType.getAnnotation(Transactional.class);
}
if (tx != null) {
doRollback = new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error, method.getExceptionTypes());
rollback.putIfAbsent(method, doRollback);
if (doRollback && isTransactionActive(state.current)) {
setRollbackOnly();
}
}
}
}
try {
commit(state);
} catch (final Exception ex) {
// no-op: swallow to keep the right exception
final Logger logger = Logger.getLogger(getClass().getName());
if (logger.isLoggable(Level.FINE)) {
logger.fine("Swallowing: " + ex.getMessage());
}
}
throw new TransactionalException(e.getMessage(), error);
} finally {
if (forbidsUt) {
resetError(oldEx);
}
}
}
Aggregations