Skip to content

Commit

Permalink
WELD-485
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Jul 16, 2010
1 parent c2c9259 commit 49bf970
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
22 changes: 22 additions & 0 deletions impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployer.java
Expand Up @@ -19,6 +19,7 @@
import static org.jboss.weld.logging.messages.BootstrapMessage.BEAN_IS_BOTH_INTERCEPTOR_AND_DECORATOR;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.decorator.Decorator;
Expand Down Expand Up @@ -93,6 +94,27 @@ public BeanDeployer addClasses(Iterable<Class<?>> classes)
return this;
}

public void fireProcessAnnotatedTypeForTypesAddedThroughTheSPI()
{
Iterator<WeldClass<?>> it = classes.iterator();
ClassTransformer classTransformer = Container.instance().services().get(ClassTransformer.class);
Set<WeldClass<?>> transformed = new HashSet<WeldClass<?>>();
while (it.hasNext())
{
WeldClass<?> c = it.next();
if (!c.isDiscovered())
{
it.remove();
ProcessAnnotatedTypeImpl<?> event = ProcessAnnotatedTypeImpl.fire(getManager(), c);
if (!event.isVeto())
{
transformed.add(classTransformer.loadClass(event.getAnnotatedType()));
}
}
}
classes.addAll(transformed);
}

public BeanDeployer createBeans()
{
Multimap<Class<?>, WeldClass<?>> otherWeldClasses = HashMultimap.create();
Expand Down
Expand Up @@ -390,6 +390,7 @@ public Bootstrap deployBeans()
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet())
{
entry.getValue().deployBeans(environment);
entry.getValue().getBeanDeployer().fireProcessAnnotatedTypeForTypesAddedThroughTheSPI();
}
AfterBeanDiscoveryImpl.fire(deploymentManager, deployment, beanDeployments);
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet())
Expand Down
Expand Up @@ -21,8 +21,11 @@

import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;

Expand All @@ -37,6 +40,8 @@
public class MultipleBeansExtension implements Extension
{

private boolean addedBlogFormatterSeen = false;

public void addNewAnnotatedTypes(@Observes BeforeBeanDiscovery event) throws SecurityException, NoSuchFieldException, NoSuchMethodException
{
TestAnnotatedTypeBuilder<BlogFormatter> formatter = new TestAnnotatedTypeBuilder<BlogFormatter>(BlogFormatter.class);
Expand All @@ -61,6 +66,30 @@ public void addNewAnnotatedTypes(@Observes BeforeBeanDiscovery event) throws Sec
event.addAnnotatedType(uselessBuilder.create());

}

public void observeProcessBlogFormatter(@Observes ProcessAnnotatedType<BlogFormatter> event)
{
AnnotatedType<BlogFormatter> type = event.getAnnotatedType();
for(AnnotatedField<? super BlogFormatter> f : type.getFields())
{
if(f.getJavaMember().getName().equals("content"))
{
if(f.isAnnotationPresent(Author.class))
{
if(f.getAnnotation(Author.class).name().equals("Bob"))
{
addedBlogFormatterSeen = true;
}
}
}
}
}


public boolean isAddedBlogFormatterSeen()
{
return addedBlogFormatterSeen;
}

private static class InjectLiteral extends AnnotationLiteral<Inject> implements Inject
{
Expand Down
Expand Up @@ -62,6 +62,16 @@ public void testBlogConsumed()
consumer = getReference(BlogConsumer.class, new ConsumerLiteral("Bob"));
assert consumer.blogContent.equals("+Bob's content+");
}
/**
* makes sure that ProcessAnnotatedType is thrown for types
* added through BeforeBeanDiscovery.addAnnotatedType
*/
@Test
public void testProcessAnnotatedTypeEventFiredForSPIAddedType()
{
MultipleBeansExtension ext = getReference(MultipleBeansExtension.class);
assert ext.isAddedBlogFormatterSeen();
}

/**
* Apparently it is not possible to add two beans that are exactly the same.
Expand Down

0 comments on commit 49bf970

Please sign in to comment.