diff --git a/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java index 443b834..c1caf68 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java +++ b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java @@ -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, Map, Bean>> genericBeans; - + // A map of generic configuration types to producer methods on generic beans // Used to track the generic bean found - private final Map, Map, Bean>> genericBeanProducerMethods; + private final Map, Map, Bean>> genericBeanProducerMethods; // A map of generic configuration types to generic bean injection targets // Used to track the generic bean found private final Map, Map, InjectionTarget>> genericInjectionTargets; - -// A map of generic configuration types to generic bean producer method producers - // Used to track the generic bean found - private final Map, Map, Producer>> genericBeanProducerMethodProducers; // A map of a generic configuration types to generic configurations // Used to track the generic configuration producers found @@ -92,9 +88,8 @@ class GenericBeanExtension implements Extension GenericBeanExtension() { this.genericBeans = new HashMap, Map, Bean>>(); - this.genericBeanProducerMethods = new HashMap, Map,Bean>>(); + this.genericBeanProducerMethods = new HashMap, Map, Bean>>(); this.genericInjectionTargets = new HashMap, Map, InjectionTarget>>(); - this.genericBeanProducerMethodProducers = new HashMap, Map,Producer>>(); this.genericProducers = new HashMap, Map>>(); this.syntheticProvider = new Synthetic.Provider("org.jboss.weld.extensions.bean.generic"); } @@ -152,23 +147,22 @@ void registerGenericBean(@Observes ProcessManagedBean event) } } } - + void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod event) { AnnotatedMethod method = event.getAnnotatedProducerMethod(); AnnotatedType declaringType = event.getAnnotatedProducerMethod().getDeclaringType(); if (declaringType.isAnnotationPresent(Generic.class)) { - Class 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, Bean> beans = new HashMap, Bean>(); beans.put(method, event.getBean()); - genericBeanProducerMethods.put(genericConfigurationType, beans); + genericBeanProducerMethods.put(declaringType, beans); } } } @@ -191,29 +185,6 @@ void registerGenericBeanInjectionTarget(@Observes ProcessInjectionTarget } } } - - void registerGenericBeanProducerMethods(@Observes ProcessProducer event) - { - if (event.getAnnotatedMember() instanceof AnnotatedMethod) - { - AnnotatedMethod method = (AnnotatedMethod) event.getAnnotatedMember(); - AnnotatedType declaringType = event.getAnnotatedMember().getDeclaringType(); - if (declaringType.isAnnotationPresent(Generic.class)) - { - Class genericConfigurationType = declaringType.getAnnotation(Generic.class).value(); - if (genericBeanProducerMethodProducers.containsKey(genericConfigurationType)) - { - genericBeanProducerMethodProducers.get(genericConfigurationType).put(method, event.getProducer()); - } - else - { - Map, Producer> injectionTargets = new HashMap, Producer>(); - injectionTargets.put(method, event.getProducer()); - genericBeanProducerMethodProducers.put(genericConfigurationType, injectionTargets); - } - } - } - } void registerGenericProducer(@Observes ProcessProducer event, BeanManager beanManager) { @@ -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, 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, Bean> type : genericBeans.get(genericConfigurationType.getKey()).entrySet()) + { + for (Entry> genericConfiguration : genericConfigurationType.getValue().entrySet()) { - for (Entry, 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, Bean> method : genericBeanProducerMethods.get(type.getKey()).entrySet()) + { + event.addBean(createGenericProducerMethod(method.getValue(), genericConfiguration.getKey(), method.getKey(), beanManager)); } } } @@ -333,13 +300,13 @@ private Bean createGenericBean(Bean originalBean, Annotation genericCo { return new GenericManagedBean(originalBean, genericConfiguration, (InjectionTarget) genericInjectionTargets.get(genericConfiguration.annotationType()).get(type), type, syntheticProvider, beanManager); } - + private Bean createGenericProducerMethod(Bean originalBean, Annotation genericConfiguration, AnnotatedMethod method, BeanManager beanManager) { Set qualifiers = getQualifiers(genericProducers.get(genericConfiguration.annotationType()).get(genericConfiguration).getAnnotations(), beanManager); return new GenericProducerMethod(originalBean, genericConfiguration, method, qualifiers, syntheticProvider, beanManager); } - + private static Set getQualifiers(Set annotations, BeanManager beanManager) { Set qualifiers = new HashSet();