Search in sources :

Example 1 with BeanBuilder

use of org.apache.deltaspike.core.util.bean.BeanBuilder in project deltaspike by apache.

the class PartialBeanBindingExtension method createPartialProducersDefinedIn.

private List<Bean> createPartialProducersDefinedIn(Bean partialBean, AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager, Class currentClass) {
    List<Bean> result = new ArrayList<Bean>();
    while (currentClass != null && !Object.class.getName().equals(currentClass.getName())) {
        for (Class interfaceClass : currentClass.getInterfaces()) {
            if (interfaceClass.getName().startsWith("java.") || interfaceClass.getName().startsWith("javax.")) {
                continue;
            }
            result.addAll(createPartialProducersDefinedIn(partialBean, afterBeanDiscovery, beanManager, interfaceClass));
        }
        for (Method currentMethod : currentClass.getDeclaredMethods()) {
            if (currentMethod.isAnnotationPresent(Produces.class)) {
                if (currentMethod.getParameterTypes().length > 0) {
                    afterBeanDiscovery.addDefinitionError(new IllegalStateException("Producer-methods in partial-beans currently don't support injection-points. " + "Please remove the parameters from " + currentMethod.toString() + " in " + currentClass.getName()));
                }
                DeltaSpikePartialProducerLifecycle lifecycle = new DeltaSpikePartialProducerLifecycle(partialBean.getBeanClass(), currentMethod);
                Class<? extends Annotation> scopeClass = extractScope(currentMethod.getDeclaredAnnotations(), beanManager);
                Class<?> producerResultType = currentMethod.getReturnType();
                boolean passivationCapable = Serializable.class.isAssignableFrom(producerResultType) || producerResultType.isPrimitive();
                Set<Annotation> qualifiers = extractQualifiers(currentMethod.getDeclaredAnnotations(), beanManager);
                BeanBuilder<?> beanBuilder = new BeanBuilder(beanManager).beanClass(producerResultType).types(Object.class, producerResultType).qualifiers(qualifiers).passivationCapable(passivationCapable).scope(scopeClass).id(createPartialProducerId(currentClass, currentMethod, qualifiers)).beanLifecycle(lifecycle);
                result.add(beanBuilder.create());
            }
        }
        currentClass = currentClass.getSuperclass();
    }
    return result;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean) BeanBuilder(org.apache.deltaspike.core.util.bean.BeanBuilder)

Example 2 with BeanBuilder

use of org.apache.deltaspike.core.util.bean.BeanBuilder in project deltaspike by apache.

the class BeanBuilderTest method assertNonNullInjectionPointsFromBeanBuilder.

@Test
public void assertNonNullInjectionPointsFromBeanBuilder() {
    final BeanBuilder beanBuilder = new BeanBuilder(beanManager);
    final AnnotatedType<?> at = beanManager.createAnnotatedType(WithInjectionPoint.class);
    final Bean<?> newInjectionBean = beanBuilder.readFromType(at).create();
    for (final InjectionPoint ip : newInjectionBean.getInjectionPoints()) {
        Assert.assertNotNull(ip);
    }
}
Also used : BeanBuilder(org.apache.deltaspike.core.util.bean.BeanBuilder) ImmutableInjectionPoint(org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Test(org.junit.Test)

Example 3 with BeanBuilder

use of org.apache.deltaspike.core.util.bean.BeanBuilder in project deltaspike by apache.

the class BeanBuilderTest method assertNonNullInjectionPointsWhenOverriding.

@Test
public void assertNonNullInjectionPointsWhenOverriding() {
    final BeanBuilder beanBuilder = new BeanBuilder(beanManager);
    final AnnotatedType<?> at = beanManager.createAnnotatedType(WithInjectionPoint.class);
    beanBuilder.readFromType(at);
    // It's not easy to actually create these in the state we need, so we have to get the InjectionPonits,
    // create new ones with the correct info, null out the bean, set them as the new injection points
    // then move on.
    final Set<InjectionPoint> origInjectionPoints = beanBuilder.getInjectionPoints();
    beanBuilder.injectionPoints(beanBuilder.getInjectionPoints());
    final Set<InjectionPoint> newInjectionPoints = new HashSet<InjectionPoint>();
    for (InjectionPoint ip : origInjectionPoints) {
        newInjectionPoints.add(new ImmutableInjectionPoint((AnnotatedField) ip.getAnnotated(), ip.getQualifiers(), null, ip.isTransient(), ip.isDelegate()));
    }
    beanBuilder.injectionPoints(newInjectionPoints);
    final Bean<?> newInjectionBean = beanBuilder.create();
    for (final InjectionPoint ip : newInjectionBean.getInjectionPoints()) {
        Assert.assertNotNull(ip);
    }
}
Also used : BeanBuilder(org.apache.deltaspike.core.util.bean.BeanBuilder) ImmutableInjectionPoint(org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) ImmutableInjectionPoint(org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

BeanBuilder (org.apache.deltaspike.core.util.bean.BeanBuilder)3 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)2 ImmutableInjectionPoint (org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 Bean (javax.enterprise.inject.spi.Bean)1