From f1eb54655de60bf74ef414290d9ce0dee0b4dec4 Mon Sep 17 00:00:00 2001 From: Pete Muir Date: Wed, 28 Jul 2010 23:19:15 +0100 Subject: [PATCH] Refactor out getAnnotation to AnnotationInspector --- .../bean/generic/GenericBeanExtension.java | 20 ++---- .../extensions/util/AnnotationInspector.java | 64 ++++++++++++++----- 2 files changed, 53 insertions(+), 31 deletions(-) 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 336c59a..3b8882e 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 @@ -17,15 +17,14 @@ package org.jboss.weld.extensions.bean.generic; import static org.jboss.weld.extensions.bean.Beans.getQualifiers; +import static org.jboss.weld.extensions.util.AnnotationInspector.getAnnotations; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Type; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; @@ -284,8 +283,7 @@ void registerGenericBean(@Observes ProcessManagedBean event) AnnotatedType type = event.getAnnotatedBeanClass(); if (type.isAnnotationPresent(Generic.class)) { - Class genericConfigurationType = type.getAnnotation(Generic.class).value(); - genericBeans.put(genericConfigurationType, new BeanTypeHolder(event.getAnnotatedBeanClass(), event.getBean())); + genericBeans.put(type.getAnnotation(Generic.class).value(), new BeanTypeHolder(event.getAnnotatedBeanClass(), event.getBean())); } } @@ -295,8 +293,7 @@ void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod method = event.getAnnotatedProducerMethod(); - Class genericConfigurationType = declaringType.getAnnotation(Generic.class).value(); - genericBeanProducerMethods.put(genericConfigurationType, new BeanMethodHolder(method, event.getBean())); + genericBeanProducerMethods.put(declaringType.getAnnotation(Generic.class).value(), new BeanMethodHolder(method, event.getBean())); // Only register a disposer method if it exists // Blocked by WELD-572 // if (event.getAnnotatedDisposedParameter() instanceof AnnotatedMethod) @@ -424,14 +421,7 @@ void createGenericBeans(@Observes AfterBeanDiscovery event, BeanManager beanMana private static Annotation getGenericConfiguration(Annotated annotated) { // Only process the producer as a generic producer, if it has an annotation meta-annotated with GenericConfiguration - List genericConfigurationAnnotiations = new ArrayList(); - for (Annotation annotation : annotated.getAnnotations()) - { - if (annotation.annotationType().isAnnotationPresent(GenericConfiguration.class)) - { - genericConfigurationAnnotiations.add(annotation); - } - } + Set genericConfigurationAnnotiations = getAnnotations(annotated, GenericConfiguration.class); if (genericConfigurationAnnotiations.size() > 1) { @@ -439,7 +429,7 @@ private static Annotation getGenericConfiguration(Annotated annotated) } else if (genericConfigurationAnnotiations.size() == 1) { - return genericConfigurationAnnotiations.get(0); + return genericConfigurationAnnotiations.iterator().next(); } else { diff --git a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java index 0b0cc69..fd819c0 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java +++ b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java @@ -18,6 +18,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; +import java.util.HashSet; +import java.util.Set; import javax.enterprise.inject.Stereotype; import javax.enterprise.inject.spi.Annotated; @@ -25,19 +27,23 @@ public class AnnotationInspector { - - private AnnotationInspector() {} - + + private AnnotationInspector() + { + } + /** - * Discover if a AnnotatedElement element has been annotated with annotationType. This - * also discovers annotations defined through a @{@link Stereotype} and the CDI SPI. - * + * Discover if a AnnotatedElement element has been annotated with + * annotationType. This also discovers annotations defined through a @ + * {@link Stereotype} and the CDI SPI. + * * @param element The element to inspect. * @param annotationType - * @param metaAnnotation Whether the annotation may be used as a meta-annotation or not - * - * @return true if annotation is present either on the method itself. Returns false if the annotation - * is not present + * @param metaAnnotation Whether the annotation may be used as a + * meta-annotation or not + * + * @return true if annotation is present either on the method itself. Returns + * false if the annotation is not present * @throws IllegalArgumentException if element or annotationType is null */ public static boolean isAnnotationPresent(AnnotatedElement element, Class annotationType, boolean metaAnnotation, BeanManager beanManager) @@ -46,14 +52,17 @@ public static boolean isAnnotationPresent(AnnotatedElement element, Classelement for a specific type of annotation. This - * also discovers annotations defined through a @ {@link Stereotype} and the CDI SPI. - * + * Inspect AnnoatedElement element for a specific type of + * annotation. This also discovers annotations defined through a @ + * {@link Stereotype} and the CDI SPI. + * * @param element The element to inspect * @param annotationType The annotation type to check for - * @param metaAnnotation Whether the annotation may be used as a meta-annotation or not - * - * @return The annotation instance found on this method or null if no matching annotation was found. + * @param metaAnnotation Whether the annotation may be used as a + * meta-annotation or not + * + * @return The annotation instance found on this method or null if no + * matching annotation was found. * @throws IllegalArgumentException if element or annotationType is null */ public static A getAnnotation(AnnotatedElement element, final Class annotationType, boolean metaAnnotation, BeanManager beanManager) @@ -103,4 +112,27 @@ public static A getMetaAnnotation(Annotated element, fina return null; } + /** + * Inspects an annotated element for any annotations with the given meta + * annotation. This should only be used for user defined meta annotations, + * where the annotation must be physically present. + * + * @param element The element to inspect + * @param annotationType The meta annotation to search for + * @return The annotation instances found on this method or an empty set if no + * matching meta-annotation was found. + */ + public static Set getAnnotations(Annotated element, final Class metaAnnotationType) + { + Set annotations = new HashSet(); + for (Annotation annotation : element.getAnnotations()) + { + if (annotation.annotationType().isAnnotationPresent(metaAnnotationType)) + { + annotations.add(annotation); + } + } + return annotations; + } + }