Search in sources :

Example 1 with AnyLiteral

use of org.apache.deltaspike.core.api.literal.AnyLiteral 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 AnyLiteral

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

the class HandlerMethodStorageImpl method getHandlersForException.

@Override
public Collection<HandlerMethod<? extends Throwable>> getHandlersForException(Type exceptionClass, BeanManager bm, Set<Annotation> handlerQualifiers, boolean isBefore) {
    final Collection<HandlerMethod<? extends Throwable>> returningHandlers = new TreeSet<HandlerMethod<? extends Throwable>>(new ExceptionHandlerComparator());
    final HierarchyDiscovery h = new HierarchyDiscovery(exceptionClass);
    final Set<Type> closure = h.getTypeClosure();
    for (Type hierarchyType : closure) {
        if (allHandlers.get(hierarchyType) != null) {
            for (HandlerMethod<?> handler : allHandlers.get(hierarchyType)) {
                if (handler.isBeforeHandler() && isBefore) {
                    if (handler.getQualifiers().contains(new AnyLiteral())) {
                        returningHandlers.add(handler);
                    } else {
                        if (!handlerQualifiers.isEmpty() && handlerQualifiers.equals(handler.getQualifiers())) {
                            returningHandlers.add(handler);
                        }
                    }
                } else if (!handler.isBeforeHandler() && !isBefore) {
                    if (handler.getQualifiers().contains(new AnyLiteral())) {
                        returningHandlers.add(handler);
                    } else {
                        if (!handlerQualifiers.isEmpty() && handlerQualifiers.equals(handler.getQualifiers())) {
                            returningHandlers.add(handler);
                        }
                    }
                }
            }
        }
    }
    log.fine(String.format("Found handlers %s for exception type %s, qualifiers %s", returningHandlers, exceptionClass, handlerQualifiers));
    return Collections.unmodifiableCollection(returningHandlers);
}
Also used : Type(java.lang.reflect.Type) TreeSet(java.util.TreeSet) AnyLiteral(org.apache.deltaspike.core.api.literal.AnyLiteral) HierarchyDiscovery(org.apache.deltaspike.core.util.HierarchyDiscovery) HandlerMethod(org.apache.deltaspike.core.api.exception.control.HandlerMethod)

Example 3 with AnyLiteral

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

the class AnnotatedTypeBuilderTest method modifyAnnotationsOnConstructorParameter.

@Test
public void modifyAnnotationsOnConstructorParameter() throws NoSuchMethodException {
    final AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
    builder.readFromType(Cat.class, true);
    builder.removeFromConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, Default.class);
    builder.addToConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, new AnyLiteral());
    final AnnotatedType<Cat> catAnnotatedType = builder.create();
    Set<AnnotatedConstructor<Cat>> catCtors = catAnnotatedType.getConstructors();
    assertThat(catCtors.size(), is(2));
    for (AnnotatedConstructor<Cat> ctor : catCtors) {
        if (ctor.getParameters().size() == 2) {
            List<AnnotatedParameter<Cat>> ctorParams = ctor.getParameters();
            assertThat(ctorParams.get(1).getAnnotations().size(), is(1));
            assertThat((AnyLiteral) ctorParams.get(1).getAnnotations().toArray()[0], is(new AnyLiteral()));
        }
    }
}
Also used : AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) AnyLiteral(org.apache.deltaspike.core.api.literal.AnyLiteral) Test(org.junit.Test)

Example 4 with AnyLiteral

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

the class BeanProvider method getBeanDefinitions.

private static <T> Set<Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans, BeanManager beanManager) {
    Set<Bean<?>> beans = beanManager.getBeans(type, new AnyLiteral());
    if (beans == null || beans.isEmpty()) {
        if (optional) {
            return Collections.emptySet();
        }
        throw new IllegalStateException("Could not find beans for Type=" + type);
    }
    if (!includeDefaultScopedBeans) {
        beans = filterDefaultScopedBeans(beans);
    }
    Set<Bean<T>> result = new HashSet<Bean<T>>();
    for (Bean<?> bean : beans) {
        //noinspection unchecked
        @SuppressWarnings("unchecked") Bean<T> beanT = (Bean<T>) bean;
        result.add(beanT);
    }
    return result;
}
Also used : AnyLiteral(org.apache.deltaspike.core.api.literal.AnyLiteral) Bean(javax.enterprise.inject.spi.Bean) HashSet(java.util.HashSet)

Aggregations

AnyLiteral (org.apache.deltaspike.core.api.literal.AnyLiteral)4 Type (java.lang.reflect.Type)2 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Alternative (javax.enterprise.inject.Alternative)1 Typed (javax.enterprise.inject.Typed)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 Bean (javax.enterprise.inject.spi.Bean)1 Named (javax.inject.Named)1 HandlerMethod (org.apache.deltaspike.core.api.exception.control.HandlerMethod)1 DefaultLiteral (org.apache.deltaspike.core.api.literal.DefaultLiteral)1 HierarchyDiscovery (org.apache.deltaspike.core.util.HierarchyDiscovery)1 AnnotatedTypeBuilder (org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder)1 Test (org.junit.Test)1