Skip to content

Commit

Permalink
Refactor out getAnnotation to AnnotationInspector
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Jul 28, 2010
1 parent 080ebc2 commit f1eb546
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -284,8 +283,7 @@ <X> void registerGenericBean(@Observes ProcessManagedBean<X> event)
AnnotatedType<X> type = event.getAnnotatedBeanClass();
if (type.isAnnotationPresent(Generic.class))
{
Class<? extends Annotation> genericConfigurationType = type.getAnnotation(Generic.class).value();
genericBeans.put(genericConfigurationType, new BeanTypeHolder<X>(event.getAnnotatedBeanClass(), event.getBean()));
genericBeans.put(type.getAnnotation(Generic.class).value(), new BeanTypeHolder<X>(event.getAnnotatedBeanClass(), event.getBean()));
}
}

Expand All @@ -295,8 +293,7 @@ <X, T> void registerGenericBeanProducerMethod(@Observes ProcessProducerMethod<X,
if (declaringType.isAnnotationPresent(Generic.class))
{
AnnotatedMethod<X> method = event.getAnnotatedProducerMethod();
Class<? extends Annotation> genericConfigurationType = declaringType.getAnnotation(Generic.class).value();
genericBeanProducerMethods.put(genericConfigurationType, new BeanMethodHolder<X, T>(method, event.getBean()));
genericBeanProducerMethods.put(declaringType.getAnnotation(Generic.class).value(), new BeanMethodHolder<X, T>(method, event.getBean()));
// Only register a disposer method if it exists
// Blocked by WELD-572
// if (event.getAnnotatedDisposedParameter() instanceof AnnotatedMethod<?>)
Expand Down Expand Up @@ -424,22 +421,15 @@ 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<Annotation> genericConfigurationAnnotiations = new ArrayList<Annotation>();
for (Annotation annotation : annotated.getAnnotations())
{
if (annotation.annotationType().isAnnotationPresent(GenericConfiguration.class))
{
genericConfigurationAnnotiations.add(annotation);
}
}
Set<Annotation> genericConfigurationAnnotiations = getAnnotations(annotated, GenericConfiguration.class);

if (genericConfigurationAnnotiations.size() > 1)
{
throw new IllegalStateException("Can only have one generic configuration annotation on " + annotated);
}
else if (genericConfigurationAnnotiations.size() == 1)
{
return genericConfigurationAnnotiations.get(0);
return genericConfigurationAnnotiations.iterator().next();
}
else
{
Expand Down
Expand Up @@ -18,26 +18,32 @@

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;
import javax.enterprise.inject.spi.BeanManager;

public class AnnotationInspector
{

private AnnotationInspector() {}


private AnnotationInspector()
{
}

/**
* Discover if a AnnotatedElement <b>element</b> has been annotated with <b>annotationType</b>. This
* also discovers annotations defined through a @{@link Stereotype} and the CDI SPI.
*
* Discover if a AnnotatedElement <b>element</b> has been annotated with
* <b>annotationType</b>. 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<? extends Annotation> annotationType, boolean metaAnnotation, BeanManager beanManager)
Expand All @@ -46,14 +52,17 @@ public static boolean isAnnotationPresent(AnnotatedElement element, Class<? exte
}

/**
* Inspect AnnoatedElement <b>element</b> for a specific <b>type</b> of annotation. This
* also discovers annotations defined through a @ {@link Stereotype} and the CDI SPI.
*
* Inspect AnnoatedElement <b>element</b> for a specific <b>type</b> 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 extends Annotation> A getAnnotation(AnnotatedElement element, final Class<A> annotationType, boolean metaAnnotation, BeanManager beanManager)
Expand Down Expand Up @@ -103,4 +112,27 @@ public static <A extends Annotation> 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<Annotation> getAnnotations(Annotated element, final Class<? extends Annotation> metaAnnotationType)
{
Set<Annotation> annotations = new HashSet<Annotation>();
for (Annotation annotation : element.getAnnotations())
{
if (annotation.annotationType().isAnnotationPresent(metaAnnotationType))
{
annotations.add(annotation);
}
}
return annotations;
}

}

0 comments on commit f1eb546

Please sign in to comment.