Search in sources :

Example 1 with DefaultLiteral

use of org.apache.deltaspike.core.api.literal.DefaultLiteral 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;
}
Also used : Named(javax.inject.Named) Alternative(javax.enterprise.inject.Alternative) Annotation(java.lang.annotation.Annotation) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) Typed(javax.enterprise.inject.Typed) DefaultLiteral(org.apache.deltaspike.core.api.literal.DefaultLiteral) AnyLiteral(org.apache.deltaspike.core.api.literal.AnyLiteral)

Example 2 with DefaultLiteral

use of org.apache.deltaspike.core.api.literal.DefaultLiteral in project deltaspike by apache.

the class MessageBundleExtension method createMessageBundleBean.

private <T> Bean<T> createMessageBundleBean(AnnotatedType<T> annotatedType, BeanManager beanManager) {
    BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager).readFromType(annotatedType);
    beanBuilder.beanLifecycle(new MessageBundleLifecycle<T>(beanManager));
    beanBuilder.types(annotatedType.getJavaClass(), Object.class, Serializable.class);
    beanBuilder.addQualifier(new DefaultLiteral());
    beanBuilder.passivationCapable(true);
    // needs to be a normalscope due to a bug in older Weld versions
    beanBuilder.scope(ApplicationScoped.class);
    beanBuilder.id("MessageBundleBean#" + annotatedType.getJavaClass().getName());
    return beanBuilder.create();
}
Also used : DefaultLiteral(org.apache.deltaspike.core.api.literal.DefaultLiteral)

Aggregations

DefaultLiteral (org.apache.deltaspike.core.api.literal.DefaultLiteral)2 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 Alternative (javax.enterprise.inject.Alternative)1 Typed (javax.enterprise.inject.Typed)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 Named (javax.inject.Named)1 AnyLiteral (org.apache.deltaspike.core.api.literal.AnyLiteral)1