Skip to content

Commit

Permalink
rework logic for producer methods somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Jul 25, 2010
1 parent bf9c6cf commit e8b0714
Showing 1 changed file with 22 additions and 55 deletions.
Expand Up @@ -69,18 +69,14 @@ class GenericBeanExtension implements Extension
// A map of generic configuration types to generic beans
// Used to track the generic bean found
private final Map<Class<? extends Annotation>, Map<AnnotatedType<?>, Bean<?>>> genericBeans;

// A map of generic configuration types to producer methods on generic beans
// Used to track the generic bean found
private final Map<Class<? extends Annotation>, Map<AnnotatedMethod<?>, Bean<?>>> genericBeanProducerMethods;
private final Map<AnnotatedType<?>, Map<AnnotatedMethod<?>, Bean<?>>> genericBeanProducerMethods;

// A map of generic configuration types to generic bean injection targets
// Used to track the generic bean found
private final Map<Class<? extends Annotation>, Map<AnnotatedType<?>, InjectionTarget<?>>> genericInjectionTargets;

// A map of generic configuration types to generic bean producer method producers
// Used to track the generic bean found
private final Map<Class<? extends Annotation>, Map<AnnotatedMethod<?>, Producer<?>>> genericBeanProducerMethodProducers;

// A map of a generic configuration types to generic configurations
// Used to track the generic configuration producers found
Expand All @@ -92,9 +88,8 @@ class GenericBeanExtension implements Extension
GenericBeanExtension()
{
this.genericBeans = new HashMap<Class<? extends Annotation>, Map<AnnotatedType<?>, Bean<?>>>();
this.genericBeanProducerMethods = new HashMap<Class<? extends Annotation>, Map<AnnotatedMethod<?>,Bean<?>>>();
this.genericBeanProducerMethods = new HashMap<AnnotatedType<?>, Map<AnnotatedMethod<?>, Bean<?>>>();
this.genericInjectionTargets = new HashMap<Class<? extends Annotation>, Map<AnnotatedType<?>, InjectionTarget<?>>>();
this.genericBeanProducerMethodProducers = new HashMap<Class<? extends Annotation>, Map<AnnotatedMethod<?>,Producer<?>>>();
this.genericProducers = new HashMap<Class<? extends Annotation>, Map<Annotation, AnnotatedMember<?>>>();
this.syntheticProvider = new Synthetic.Provider("org.jboss.weld.extensions.bean.generic");
}
Expand Down Expand Up @@ -152,23 +147,22 @@ <X> void registerGenericBean(@Observes ProcessManagedBean<X> event)
}
}
}

<T, X> void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod<T, X> event)
{
AnnotatedMethod<T> method = event.getAnnotatedProducerMethod();
AnnotatedType<T> declaringType = event.getAnnotatedProducerMethod().getDeclaringType();
if (declaringType.isAnnotationPresent(Generic.class))
{
Class<? extends Annotation> genericConfigurationType = declaringType.getAnnotation(Generic.class).value();
if (genericBeanProducerMethods.containsKey(genericConfigurationType))
if (genericBeanProducerMethods.containsKey(declaringType))
{
genericBeanProducerMethods.get(genericConfigurationType).put(method, event.getBean());
genericBeanProducerMethods.get(declaringType).put(method, event.getBean());
}
else
{
Map<AnnotatedMethod<?>, Bean<?>> beans = new HashMap<AnnotatedMethod<?>, Bean<?>>();
beans.put(method, event.getBean());
genericBeanProducerMethods.put(genericConfigurationType, beans);
genericBeanProducerMethods.put(declaringType, beans);
}
}
}
Expand All @@ -191,29 +185,6 @@ <X> void registerGenericBeanInjectionTarget(@Observes ProcessInjectionTarget<X>
}
}
}

<T, X> void registerGenericBeanProducerMethods(@Observes ProcessProducer<T, X> event)
{
if (event.getAnnotatedMember() instanceof AnnotatedMethod<?>)
{
AnnotatedMethod<T> method = (AnnotatedMethod<T>) event.getAnnotatedMember();
AnnotatedType<T> declaringType = event.getAnnotatedMember().getDeclaringType();
if (declaringType.isAnnotationPresent(Generic.class))
{
Class<? extends Annotation> genericConfigurationType = declaringType.getAnnotation(Generic.class).value();
if (genericBeanProducerMethodProducers.containsKey(genericConfigurationType))
{
genericBeanProducerMethodProducers.get(genericConfigurationType).put(method, event.getProducer());
}
else
{
Map<AnnotatedMethod<?>, Producer<?>> injectionTargets = new HashMap<AnnotatedMethod<?>, Producer<?>>();
injectionTargets.put(method, event.getProducer());
genericBeanProducerMethodProducers.put(genericConfigurationType, injectionTargets);
}
}
}
}

<T, X> void registerGenericProducer(@Observes ProcessProducer<T, X> event, BeanManager beanManager)
{
Expand All @@ -240,26 +211,22 @@ void createGenericBeans(@Observes AfterBeanDiscovery event, BeanManager beanMana
}
for (Annotation genericConfiguration : genericConfigurationType.getValue().keySet())
{
// For each generic bean that uses this genericConfigurationType, register a generic bean for this generic configuration
for (Entry<AnnotatedType<?>, Bean<?>> type : genericBeans.get(genericConfigurationType.getKey()).entrySet())
{
Bean<?> originalBean = type.getValue();
event.addBean(createGenericBean(originalBean, genericConfiguration, (AnnotatedType) type.getKey(), beanManager));
}

// Add a generic configuration bean for each generic configuration producer (allows us to inject the generic configuration annotation back into the generic bean)
event.addBean(createGenericConfigurationBean(beanManager, genericConfiguration));

// For each of the producer methods which are registered to this generic configuration type, register a generic producer method for this generic configuration
if (genericBeanProducerMethods.containsKey(genericConfiguration.annotationType()))
}
// For each generic bean that uses this genericConfigurationType, register a generic bean for this generic configuration
for (Entry<AnnotatedType<?>, Bean<?>> type : genericBeans.get(genericConfigurationType.getKey()).entrySet())
{
for (Entry<Annotation, AnnotatedMember<?>> genericConfiguration : genericConfigurationType.getValue().entrySet())
{
for (Entry<AnnotatedMethod<?>, Bean<?>> type : genericBeanProducerMethods.get(genericConfigurationType.getKey()).entrySet())
{
AnnotatedMethod<?> method = type.getKey();
if (method.getDeclaringType().getBaseType().equals(genericConfigurationType.getValue().get(genericConfiguration).getBaseType()))
{
Bean<?> originalProducerMethod = type.getValue();
event.addBean(createGenericProducerMethod(originalProducerMethod, genericConfiguration, type.getKey(), beanManager));
Bean<?> originalBean = type.getValue();
event.addBean(createGenericBean(originalBean, genericConfiguration.getKey(), (AnnotatedType) type.getKey(), beanManager));
// Only register producer method beans if the producer field has the same type as the generic bean declaration
if (genericConfiguration.getValue().getBaseType().equals(type.getKey().getBaseType()) && genericBeanProducerMethods.containsKey(type.getKey()))
{
for (Entry<AnnotatedMethod<?>, Bean<?>> method : genericBeanProducerMethods.get(type.getKey()).entrySet())
{
event.addBean(createGenericProducerMethod(method.getValue(), genericConfiguration.getKey(), method.getKey(), beanManager));
}
}
}
Expand Down Expand Up @@ -333,13 +300,13 @@ private <X> Bean<X> createGenericBean(Bean<X> originalBean, Annotation genericCo
{
return new GenericManagedBean<X>(originalBean, genericConfiguration, (InjectionTarget<X>) genericInjectionTargets.get(genericConfiguration.annotationType()).get(type), type, syntheticProvider, beanManager);
}

private <X, T> Bean<T> createGenericProducerMethod(Bean<T> originalBean, Annotation genericConfiguration, AnnotatedMethod<X> method, BeanManager beanManager)
{
Set<Annotation> qualifiers = getQualifiers(genericProducers.get(genericConfiguration.annotationType()).get(genericConfiguration).getAnnotations(), beanManager);
return new GenericProducerMethod<T, X>(originalBean, genericConfiguration, method, qualifiers, syntheticProvider, beanManager);
}

private static Set<Annotation> getQualifiers(Set<Annotation> annotations, BeanManager beanManager)
{
Set<Annotation> qualifiers = new HashSet<Annotation>();
Expand Down

0 comments on commit e8b0714

Please sign in to comment.