Search in sources :

Example 1 with TestAnnotatedTypeBuilder

use of org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder in project core by weld.

the class InterceptorExtension method beforeBeanDiscovery.

/**
 * registers two interceptors via the SPI
 */
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager beanManager) throws SecurityException, NoSuchMethodException {
    event.addInterceptorBinding(Incremented.class);
    event.addInterceptorBinding(FullMarathon.class);
    TestAnnotatedTypeBuilder<IncrementingInterceptor> incBuilder = new TestAnnotatedTypeBuilder<IncrementingInterceptor>(IncrementingInterceptor.class);
    incBuilder.addToClass(new InterceptorLiteral());
    incBuilder.addToClass(new IncrementedLiteral());
    Method around = IncrementingInterceptor.class.getMethod("doAround", InvocationContext.class);
    incBuilder.addToMethod(around, new AroundInvokeLiteral());
    event.addAnnotatedType(incBuilder.create());
    TestAnnotatedTypeBuilder<LifecycleInterceptor> marBuilder = new TestAnnotatedTypeBuilder<LifecycleInterceptor>(LifecycleInterceptor.class);
    marBuilder.addToClass(new InterceptorLiteral());
    marBuilder.addToClass(new FullMarathonImpl());
    Method pre = LifecycleInterceptor.class.getMethod("preDestroy", InvocationContext.class);
    marBuilder.addToMethod(pre, new PreDestroyLiteral());
    Method post = LifecycleInterceptor.class.getMethod("postConstruct", InvocationContext.class);
    marBuilder.addToMethod(post, new PostConstructLiteral());
    event.addAnnotatedType(marBuilder.create());
}
Also used : TestAnnotatedTypeBuilder(org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder) Method(java.lang.reflect.Method)

Example 2 with TestAnnotatedTypeBuilder

use of org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder in project core by weld.

the class MultipleBeansExtension method addNewAnnotatedTypes.

public void addNewAnnotatedTypes(@Observes BeforeBeanDiscovery event) throws SecurityException, NoSuchFieldException, NoSuchMethodException {
    TestAnnotatedTypeBuilder<BlogFormatter> formatter = new TestAnnotatedTypeBuilder<BlogFormatter>(BlogFormatter.class);
    Field content = BlogFormatter.class.getField("content");
    formatter.addToField(content, new InjectLiteral());
    formatter.addToField(content, new AuthorLiteral("Bob"));
    Method format = BlogFormatter.class.getMethod("format");
    formatter.addToMethod(format, new ProducesLiteral());
    formatter.addToMethod(format, new FormattedBlogLiteral("Bob"));
    event.addAnnotatedType(formatter.create());
    TestAnnotatedTypeBuilder<BlogConsumer> consumer = new TestAnnotatedTypeBuilder<BlogConsumer>(BlogConsumer.class);
    consumer.addToClass(new ConsumerLiteral("Bob"));
    content = BlogConsumer.class.getField("blogContent");
    consumer.addToField(content, new InjectLiteral());
    consumer.addToField(content, new FormattedBlogLiteral("Bob"));
    event.addAnnotatedType(consumer.create());
    // two beans that are exactly the same
    // this is not very useful, however should still work
    TestAnnotatedTypeBuilder<UselessBean> uselessBuilder = new TestAnnotatedTypeBuilder<UselessBean>(UselessBean.class);
    event.addAnnotatedType(uselessBuilder.create());
}
Also used : TestAnnotatedTypeBuilder(org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder) Method(java.lang.reflect.Method) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) Field(java.lang.reflect.Field)

Example 3 with TestAnnotatedTypeBuilder

use of org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder in project core by weld.

the class AnnotatedTypesTest method testComparison.

/**
 * tests the AnnotatedTypes.compareAnnotatedTypes
 */
@Test
public void testComparison() throws SecurityException, NoSuchFieldException, NoSuchMethodException {
    // check that two weld classes on the same underlying are equal
    TypeStore ts = new TypeStore();
    ClassTransformer ct = new ClassTransformer(ts, new SharedObjectCache(), ReflectionCacheFactory.newInstance(ts), RegistrySingletonProvider.STATIC_INSTANCE);
    EnhancedAnnotatedType<Chair> chair1 = EnhancedAnnotatedTypeImpl.of(BackedAnnotatedType.of(Chair.class, ct.getSharedObjectCache(), ct.getReflectionCache(), RegistrySingletonProvider.STATIC_INSTANCE, AnnotatedTypeIdentifier.NULL_BDA_ID), ct);
    EnhancedAnnotatedType<Chair> chair2 = EnhancedAnnotatedTypeImpl.of(BackedAnnotatedType.of(Chair.class, ct.getSharedObjectCache(), ct.getReflectionCache(), RegistrySingletonProvider.STATIC_INSTANCE, AnnotatedTypeIdentifier.NULL_BDA_ID), ct);
    Assert.assertTrue(AnnotatedTypes.compareAnnotatedTypes(chair1, chair2));
    // check that a different implementation of annotated type is equal to the weld implementation
    TestAnnotatedTypeBuilder<Chair> builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new DefaultLiteral());
    builder.addToField(Chair.class.getField("legs"), new ProducesLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    AnnotatedType<Chair> chair3 = builder.create();
    Assert.assertTrue(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
    // check that the implementation returns false if a field annotation changes
    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new DefaultLiteral());
    builder.addToField(Chair.class.getField("legs"), new DefaultLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    chair3 = builder.create();
    Assert.assertFalse(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
    // check that the implementation returns false if a class level annotation changes
    builder = new TestAnnotatedTypeBuilder<Chair>(Chair.class);
    builder.addToClass(new ProducesLiteral());
    builder.addToField(Chair.class.getField("legs"), new DefaultLiteral());
    builder.addToMethod(Chair.class.getMethod("sit"), new ProducesLiteral());
    chair3 = builder.create();
    Assert.assertFalse(AnnotatedTypes.compareAnnotatedTypes(chair1, chair3));
}
Also used : SharedObjectCache(org.jboss.weld.resources.SharedObjectCache) TypeStore(org.jboss.weld.metadata.TypeStore) ClassTransformer(org.jboss.weld.resources.ClassTransformer) TestAnnotatedTypeBuilder(org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder) Test(org.testng.annotations.Test)

Example 4 with TestAnnotatedTypeBuilder

use of org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder in project core by weld.

the class AnnotatedTypeEjbExtension method overrideLatheAnnotations.

/**
 * Adds annotations to an EJB
 */
public void overrideLatheAnnotations(@Observes ProcessAnnotatedType<Lathe> event) throws SecurityException, NoSuchMethodException {
    if (!event.getAnnotatedType().isAnnotationPresent(SmallLathe.class) && !event.getAnnotatedType().isAnnotationPresent(BigLathe.class)) {
        TestAnnotatedTypeBuilder<Lathe> builder = new TestAnnotatedTypeBuilder<Lathe>(Lathe.class);
        for (Annotation a : event.getAnnotatedType().getAnnotations()) {
            builder.addToClass(a);
        }
        Method method = Lathe.class.getMethod("doWork");
        builder.addToMethod(method, new AnnotationLiteral<ConveyorShaft>() {
        });
        builder.addToMethod(method, new AnnotationLiteral<Produces>() {
        });
        event.setAnnotatedType(builder.create());
    }
}
Also used : Produces(javax.enterprise.inject.Produces) TestAnnotatedTypeBuilder(org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Aggregations

TestAnnotatedTypeBuilder (org.jboss.weld.test.util.annotated.TestAnnotatedTypeBuilder)4 Method (java.lang.reflect.Method)3 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Produces (javax.enterprise.inject.Produces)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 TypeStore (org.jboss.weld.metadata.TypeStore)1 ClassTransformer (org.jboss.weld.resources.ClassTransformer)1 SharedObjectCache (org.jboss.weld.resources.SharedObjectCache)1 Test (org.testng.annotations.Test)1