Search in sources :

Example 1 with AnnotatedTypeBuilder

use of org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder 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 2 with AnnotatedTypeBuilder

use of org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder in project deltaspike by apache.

the class NamingConventionAwareMetadataFilter method ensureNamingConvention.

public void ensureNamingConvention(@Observes ProcessAnnotatedType processAnnotatedType) {
    Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass();
    Named namedAnnotation = beanClass.getAnnotation(Named.class);
    if (namedAnnotation != null && namedAnnotation.value().length() > 0 && Character.isUpperCase(namedAnnotation.value().charAt(0))) {
        AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder();
        builder.readFromType(beanClass);
        String beanName = namedAnnotation.value();
        String newBeanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
        builder.removeFromClass(Named.class).addToClass(new NamedLiteral(newBeanName));
        processAnnotatedType.setAnnotatedType(builder.create());
    }
}
Also used : Named(javax.inject.Named) AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral)

Example 3 with AnnotatedTypeBuilder

use of org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder in project deltaspike by apache.

the class PartialBeanBindingExtension method createPartialBean.

protected <T> Bean<T> createPartialBean(Class<T> beanClass, PartialBeanDescriptor descriptor, AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
    if (descriptor.getHandler() == null) {
        afterBeanDiscovery.addDefinitionError(new IllegalStateException("A class which implements " + InvocationHandler.class.getName() + " and is annotated with @" + descriptor.getBinding().getName() + " is needed as a handler for " + beanClass.getName() + ". See the documentation about @" + PartialBeanBinding.class.getName() + "."));
        return null;
    }
    AnnotatedType<T> annotatedType = new AnnotatedTypeBuilder<T>().readFromType(beanClass).create();
    DeltaSpikeProxyContextualLifecycle lifecycle = new DeltaSpikeProxyContextualLifecycle(beanClass, descriptor.getHandler(), PartialBeanProxyFactory.getInstance(), beanManager);
    BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager).readFromType(annotatedType).passivationCapable(true).beanLifecycle(lifecycle);
    return beanBuilder.create();
}
Also used : AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) DeltaSpikeProxyContextualLifecycle(org.apache.deltaspike.proxy.api.DeltaSpikeProxyContextualLifecycle)

Example 4 with AnnotatedTypeBuilder

use of org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder in project deltaspike by apache.

the class SecurityExtension method processAnnotatedType.

/**
     * Handles &#064;Secured beans
     */
public <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> event) {
    if (!isActivated) {
        return;
    }
    AnnotatedTypeBuilder<X> builder = null;
    AnnotatedType<X> type = event.getAnnotatedType();
    boolean isSecured = false;
    // with a security binding type
    for (final Annotation annotation : type.getAnnotations()) {
        if (SecurityUtils.isMetaAnnotatedWithSecurityBindingType(annotation)) {
            builder = new AnnotatedTypeBuilder<X>().readFromType(type);
            builder.addToClass(INTERCEPTOR_BINDING);
            getMetaDataStorage().addSecuredType(type);
            isSecured = true;
            break;
        }
    }
    // method
    if (!isSecured) {
        for (final AnnotatedMethod<? super X> m : type.getMethods()) {
            if (m.isAnnotationPresent(Secures.class)) {
                registerAuthorizer(m);
                continue;
            }
            for (final Annotation annotation : m.getAnnotations()) {
                if (SecurityUtils.isMetaAnnotatedWithSecurityBindingType(annotation)) {
                    if (builder == null) {
                        builder = new AnnotatedTypeBuilder<X>().readFromType(type);
                    }
                    builder.addToMethod(m, INTERCEPTOR_BINDING);
                    getMetaDataStorage().addSecuredMethod(m);
                    break;
                }
            }
        }
    }
    if (builder != null) {
        event.setAnnotatedType(builder.create());
    }
}
Also used : AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) Annotation(java.lang.annotation.Annotation)

Example 5 with AnnotatedTypeBuilder

use of org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder in project deltaspike by apache.

the class AnnotatedTypeBuilderTest method testTypeLevelAnnotationRedefinition.

@Test
public void testTypeLevelAnnotationRedefinition() {
    AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
    builder.readFromType(Cat.class);
    AnnotatedType<Cat> cat = builder.create();
    assertNotNull(cat);
    assertNotNull(cat.getAnnotation(Named.class));
    assertEquals("cat", cat.getAnnotation(Named.class).value());
    builder.addToClass(new AlternativeLiteral()).addToClass(new ApplicationScopedLiteral()).removeFromClass(Named.class).addToClass(new NamedLiteral("tomcat"));
    cat = builder.create();
    assertNotNull(cat);
    assertEquals(3, cat.getAnnotations().size());
    assertTrue(cat.isAnnotationPresent(Named.class));
    assertTrue(cat.isAnnotationPresent(Alternative.class));
    assertTrue(cat.isAnnotationPresent(ApplicationScoped.class));
    assertEquals("tomcat", cat.getAnnotation(Named.class).value());
    AnnotatedMethod observerMethod = null;
    for (AnnotatedMethod m : cat.getMethods()) {
        if ("doSomeObservation".equals(m.getJavaMember().getName())) {
            observerMethod = m;
            break;
        }
    }
    assertNotNull(observerMethod);
    observerMethod.isAnnotationPresent(Observes.class);
    {
        // test reading from an AnnotatedType
        AnnotatedTypeBuilder<Cat> builder2 = new AnnotatedTypeBuilder<Cat>();
        builder2.readFromType(cat);
        builder2.removeFromAll(Named.class);
        final AnnotatedType<Cat> noNameCat = builder2.create();
        assertFalse(noNameCat.isAnnotationPresent(Named.class));
        assertEquals(2, noNameCat.getAnnotations().size());
    }
    {
        // test reading from an AnnotatedType in non-overwrite mode
        AnnotatedTypeBuilder<Cat> builder3 = new AnnotatedTypeBuilder<Cat>();
        builder3.readFromType(cat, true);
        builder3.removeFromAll(Named.class);
        builder3.readFromType(cat, false);
        final AnnotatedType<Cat> namedCat = builder3.create();
        assertTrue(namedCat.isAnnotationPresent(Named.class));
        assertEquals(3, namedCat.getAnnotations().size());
    }
}
Also used : Named(javax.inject.Named) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) AnnotatedTypeBuilder(org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder) Alternative(javax.enterprise.inject.Alternative) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) AlternativeLiteral(org.apache.deltaspike.core.api.literal.AlternativeLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral) ApplicationScopedLiteral(org.apache.deltaspike.core.api.literal.ApplicationScopedLiteral) Test(org.junit.Test)

Aggregations

AnnotatedTypeBuilder (org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder)7 Test (org.junit.Test)3 Named (javax.inject.Named)2 NamedLiteral (org.apache.deltaspike.core.api.literal.NamedLiteral)2 DeltaSpikeProxyContextualLifecycle (org.apache.deltaspike.proxy.api.DeltaSpikeProxyContextualLifecycle)2 Annotation (java.lang.annotation.Annotation)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Alternative (javax.enterprise.inject.Alternative)1 Typed (javax.enterprise.inject.Typed)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 Converter (javax.faces.convert.Converter)1 AlternativeLiteral (org.apache.deltaspike.core.api.literal.AlternativeLiteral)1 AnyLiteral (org.apache.deltaspike.core.api.literal.AnyLiteral)1 ApplicationScopedLiteral (org.apache.deltaspike.core.api.literal.ApplicationScopedLiteral)1 TypedLiteral (org.apache.deltaspike.core.api.literal.TypedLiteral)1