Search in sources :

Example 1 with Typed

use of javax.enterprise.inject.Typed 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 Typed

use of javax.enterprise.inject.Typed in project deltaspike by apache.

the class MockExtension method onProcessInjectionTarget.

public <X> void onProcessInjectionTarget(@Observes ProcessInjectionTarget<X> processInjectionTarget, BeanManager beanManager) {
    if (!isActivated) {
        return;
    }
    for (MockFilter mockFilter : mockFilters) {
        if (!mockFilter.isMockedImplementationSupported(beanManager, processInjectionTarget.getAnnotatedType())) {
            return;
        }
    }
    List<Annotation> qualifiers = new ArrayList<Annotation>();
    for (Annotation annotation : processInjectionTarget.getAnnotatedType().getAnnotations()) {
        if (beanManager.isQualifier(annotation.annotationType())) {
            qualifiers.add(annotation);
        }
    }
    Typed typed = processInjectionTarget.getAnnotatedType().getAnnotation(Typed.class);
    List<Type> foundTypes = new ArrayList<Type>();
    if (typed != null) {
        Collections.addAll(foundTypes, typed.value());
    } else {
        foundTypes.addAll(extractTypes(processInjectionTarget.getAnnotatedType().getJavaClass()));
    }
    if (foundTypes.isEmpty()) {
        return;
    }
    final InjectionTarget<X> originalInjectionTarget = processInjectionTarget.getInjectionTarget();
    processInjectionTarget.setInjectionTarget(new MockAwareInjectionTargetWrapper<X>(beanManager, originalInjectionTarget, foundTypes, qualifiers));
}
Also used : Typed(javax.enterprise.inject.Typed) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) MockFilter(org.apache.deltaspike.testcontrol.spi.mock.MockFilter) Annotation(java.lang.annotation.Annotation)

Example 3 with Typed

use of javax.enterprise.inject.Typed in project deltaspike by apache.

the class AbstractMockManager method addMock.

@Override
public void addMock(Object mockInstance, Annotation... qualifiers) {
    //check if this method gets used without changing the default-config
    if (!TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS && !TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS) {
        throw new IllegalStateException("The support for mocked CDI-Beans is disabled " + "due to a reduced portability across different CDI-implementations. " + "Please set '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS_KEY + "' and/or '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS_KEY + "' to 'true' " + "(in 'META-INF/apache-deltaspike.properties') on your test-classpath.");
    }
    Class<?> mockClass = mockInstance.getClass();
    Class<?> beanClass = mockClass.getSuperclass();
    if (beanClass == null) {
        beanClass = mockClass;
    }
    if (Object.class.equals(beanClass)) {
        throw new IllegalArgumentException(mockInstance.getClass().getName() + " isn't a supported approach for mocking -> please extend from the original class.");
    }
    TypedMock typedMock = mockClass.getAnnotation(TypedMock.class);
    if (typedMock == null) {
        typedMock = beanClass.getAnnotation(TypedMock.class);
    }
    Class[] specifiedTypes = null;
    if (typedMock != null) {
        specifiedTypes = typedMock.value();
    } else {
        Typed typed = mockClass.getAnnotation(Typed.class);
        if (typed == null || typed.value().length == 0) {
            typed = beanClass.getAnnotation(Typed.class);
        }
        if (typed != null && typed.value().length > 0) {
            specifiedTypes = typed.value();
        }
    }
    if (specifiedTypes != null) {
        for (Class typedClass : specifiedTypes) {
            this.registeredMocks.put(new BeanCacheKey(typedClass, qualifiers), mockInstance);
        }
    } else {
        this.registeredMocks.put(new BeanCacheKey(beanClass, qualifiers), mockInstance);
    }
}
Also used : Typed(javax.enterprise.inject.Typed) TypedMock(org.apache.deltaspike.testcontrol.api.mock.TypedMock)

Example 4 with Typed

use of javax.enterprise.inject.Typed in project deltaspike by apache.

the class MockExtension method onProcessProducer.

public <X, T> void onProcessProducer(@Observes ProcessProducer<X, T> processProducer, BeanManager beanManager) {
    if (!isActivated) {
        return;
    }
    for (MockFilter mockFilter : mockFilters) {
        if (!mockFilter.isMockedImplementationSupported(beanManager, processProducer.getAnnotatedMember())) {
            return;
        }
    }
    final Producer<T> originalProducer = processProducer.getProducer();
    AnnotatedMember<X> annotatedMember = processProducer.getAnnotatedMember();
    List<Annotation> qualifiers = new ArrayList<Annotation>();
    for (Annotation annotation : annotatedMember.getAnnotations()) {
        if (beanManager.isQualifier(annotation.annotationType())) {
            qualifiers.add(annotation);
        }
    }
    Typed typed = annotatedMember.getAnnotation(Typed.class);
    List<Type> foundTypes = new ArrayList<Type>();
    if (typed != null) {
        Collections.addAll(foundTypes, typed.value());
    } else if (annotatedMember.getBaseType() instanceof Class) {
        foundTypes.addAll(extractTypes((Class) annotatedMember.getBaseType()));
    }
    if (foundTypes.isEmpty()) {
        return;
    }
    processProducer.setProducer(new MockAwareProducerWrapper<T>(beanManager, originalProducer, foundTypes, qualifiers));
}
Also used : ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Typed(javax.enterprise.inject.Typed) Type(java.lang.reflect.Type) MockFilter(org.apache.deltaspike.testcontrol.spi.mock.MockFilter)

Aggregations

Typed (javax.enterprise.inject.Typed)4 Annotation (java.lang.annotation.Annotation)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)2 MockFilter (org.apache.deltaspike.testcontrol.spi.mock.MockFilter)2 Alternative (javax.enterprise.inject.Alternative)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 Named (javax.inject.Named)1 AnyLiteral (org.apache.deltaspike.core.api.literal.AnyLiteral)1 DefaultLiteral (org.apache.deltaspike.core.api.literal.DefaultLiteral)1 TypedMock (org.apache.deltaspike.testcontrol.api.mock.TypedMock)1