Search in sources :

Example 1 with NamedLiteral

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

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

the class AnnotationInstanceProviderTest method assertCreatedAnnotationNotEqualToLiteralWithDifferentMemberValues.

@Test
public void assertCreatedAnnotationNotEqualToLiteralWithDifferentMemberValues() {
    Map<String, String> memberValue = new HashMap<String, String>();
    memberValue.put("value", "test1");
    Named named1 = AnnotationInstanceProvider.of(Named.class, memberValue);
    Named named2 = new NamedLiteral("test");
    assertFalse(named1.equals(named2));
}
Also used : Named(javax.inject.Named) HashMap(java.util.HashMap) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral) Test(org.junit.Test)

Example 3 with NamedLiteral

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

the class AnnotationInstanceProviderTest method assertHashCodeSameAsLiteral.

@Test
public void assertHashCodeSameAsLiteral() {
    Named a1 = AnnotationInstanceProvider.of(Named.class);
    Named a2 = new NamedLiteral();
    assertThat(a2.hashCode(), is(a1.hashCode()));
}
Also used : Named(javax.inject.Named) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral) Test(org.junit.Test)

Example 4 with NamedLiteral

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

the class AnnotationInstanceProviderTest method assertCreatedAnnotationEqualToLiteral.

@Test
public void assertCreatedAnnotationEqualToLiteral() {
    Map<String, String> memberValue = new HashMap<String, String>();
    memberValue.put("value", "test");
    Named named1 = AnnotationInstanceProvider.of(Named.class, memberValue);
    Named named2 = new NamedLiteral("test");
    assertTrue(named2.equals(named1));
    assertTrue(named1.equals(named2));
}
Also used : Named(javax.inject.Named) HashMap(java.util.HashMap) NamedLiteral(org.apache.deltaspike.core.api.literal.NamedLiteral) Test(org.junit.Test)

Example 5 with NamedLiteral

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

Named (javax.inject.Named)5 NamedLiteral (org.apache.deltaspike.core.api.literal.NamedLiteral)5 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 AnnotatedTypeBuilder (org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Alternative (javax.enterprise.inject.Alternative)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 AlternativeLiteral (org.apache.deltaspike.core.api.literal.AlternativeLiteral)1 ApplicationScopedLiteral (org.apache.deltaspike.core.api.literal.ApplicationScopedLiteral)1