Example usage for java.lang.reflect AnnotatedElement getAnnotations

List of usage examples for java.lang.reflect AnnotatedElement getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect AnnotatedElement getAnnotations.

Prototype

Annotation[] getAnnotations();

Source Link

Document

Returns annotations that are present on this element.

Usage

From source file:org.diorite.cfg.system.TemplateCreator.java

static String[] readComments(final AnnotatedElement element) {
    final String header;
    final String footer;
    final Collection<String> headerTemp = new ArrayList<>(5);
    final Collection<String> footerTemp = new ArrayList<>(5);
    for (final Annotation annotation : element.getAnnotations()) {
        if (annotation instanceof CfgComment) {
            headerTemp.add(((CfgComment) annotation).value());
        }//from  w w w  .ja  va  2 s .  c o  m
        if (annotation instanceof CfgCommentsArray) {
            for (final CfgComment comment : ((CfgCommentsArray) annotation).value()) {
                headerTemp.add(comment.value());
            }
        }
        if (annotation instanceof CfgComments) {
            Collections.addAll(headerTemp, ((CfgComments) annotation).value());
        }
        if (annotation instanceof CfgFooterComment) {
            footerTemp.add(((CfgFooterComment) annotation).value());
        }
        if (annotation instanceof CfgFooterCommentsArray) {
            for (final CfgFooterComment comment : ((CfgFooterCommentsArray) annotation).value()) {
                footerTemp.add(comment.value());
            }
        }
        if (annotation instanceof CfgFooterComments) {
            Collections.addAll(footerTemp, ((CfgFooterComments) annotation).value());
        }
    }
    if (headerTemp.isEmpty()) {
        header = null;
    } else {
        header = StringUtils.join(headerTemp, '\n');
    }
    if (footerTemp.isEmpty()) {
        footer = null;
    } else {
        footer = StringUtils.join(footerTemp, '\n');
    }
    return new String[] { header, footer };
}

From source file:org.nuxeo.common.xmap.XMap.java

protected static Annotation checkMemberAnnotation(AnnotatedElement ae) {
    Annotation[] annos = ae.getAnnotations();
    for (Annotation anno : annos) {
        if (anno.annotationType().isAnnotationPresent(XMemberAnnotation.class)) {
            return anno;
        }/*from   www .j  av a 2 s.  c  o m*/
    }
    return null;
}

From source file:org.omnaest.utils.reflection.ReflectionUtils.java

/**
 * Returns all {@link AnnotatedElement#getAnnotations()} as {@link Map} which has the {@link Annotation} type as key and its
 * related instance as value./*from   w  ww  . j  a v  a  2  s.c  o m*/
 * 
 * @param annotatedElement
 *          {@link Field}
 * @return always a {@link Map} instance, even if the annotatedElement is null
 */
@SuppressWarnings("unchecked")
public static Map<Class<Annotation>, Annotation> annotationTypeToAnnotationMap(
        AnnotatedElement annotatedElement) {
    final Map<Class<Annotation>, Annotation> retmap = new LinkedHashMap<Class<Annotation>, Annotation>();
    if (annotatedElement != null) {
        final Set<Annotation> annotationSet = SetUtils.valueOf(annotatedElement.getAnnotations());
        for (Annotation annotation : annotationSet) {
            if (annotation != null) {
                final Class<Annotation> annotationType = (Class<Annotation>) annotation.annotationType();
                retmap.put(annotationType, annotation);
            }
        }
    }
    return retmap;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * This methods take as parameter an annotatedElement (Class, Field, Method,
 * Constructor ) and an annotation class. It returns an annotation instance
 * that meet the requirement : instance of the class annotationClassToFind.
 * <p>/*  w  ww  .  j  ava  2 s .com*/
 * If annotatedElement is a class the method will reach all super type and
 * for each of those type, it will reach all implemented interfaces and for
 * each implemented interfaces it will reach all super interfaces
 * <p>
 * If annotatedElement is not a class the method will look for the
 * annotation class only in this element, other all the hierarchy.
 * <p>
 * From this list, the method will reach for the annotation class in all
 * annotation hierarchy.
 *
 * @param <T> the annotation type to retrieve
 * @param annotatedElement      The annotated element from where to start.
 * @param annotationClassToFind The annotation class to find.
 * @return ? extends Annotation
 */
@SuppressWarnings("unchecked")
@Magic(type = WHITE)
public static <T extends Annotation> T getMetaAnnotationFromAncestors(AnnotatedElement annotatedElement,
        Class<T> annotationClassToFind) {
    List<AnnotatedElement> list = Lists.newArrayList();

    if (annotatedElement instanceof Class<?>) {
        list.addAll(Arrays.asList(getAllInterfacesAndClasses((Class<?>) annotatedElement)));
    } else {
        list.add(annotatedElement);
    }

    for (AnnotatedElement element : list) {
        // element search
        for (Annotation anno : element.getAnnotations()) {
            if (anno.annotationType().equals(annotationClassToFind)) {
                return (T) anno;
            }
        }

        // deep search
        for (Annotation anno : element.getAnnotations()) {
            T result = (T) getAnnotationDeep(anno, annotationClassToFind);
            if (result != null) {
                return result;
            }
        }
    }

    return null;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * Finds a annotation meta-annotated on the given annotated element.
 *
 * @param <T> the annotation type to retrieve
 * @param annotatedElementClass the annotated element
 * @param metaAnnotationToFind  the meta annotation to find
 * @return the annotation meta annotated if exist, null otherwise
 *//* w w  w  . ja va  2  s  .c om*/
public static <T extends Annotation> Annotation getAnnotationMetaAnnotated(
        AnnotatedElement annotatedElementClass, Class<T> metaAnnotationToFind) {
    Annotation[] annotations = annotatedElementClass.getAnnotations();
    for (Annotation annotation : annotations) {
        Annotation[] metaAnnotations = annotation.annotationType().getAnnotations();
        for (Annotation metaAnnotation : metaAnnotations) {
            if (metaAnnotation.annotationType().equals(metaAnnotationToFind)) {
                return annotation;
            }
        }
    }
    return null;
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Get a single {@link Annotation} of {@code annotationType} from the supplied
 * {@link AnnotatedElement}, where the annotation is either <em>present</em> or
 * <em>meta-present</em> on the {@code AnnotatedElement}.
 * <p>Note that this method supports only a single level of meta-annotations.
 * For support for arbitrary levels of meta-annotations, use
 * {@link #findAnnotation(AnnotatedElement, Class)} instead.
 * @param annotatedElement the {@code AnnotatedElement} from which to get the annotation
 * @param annotationType the annotation type to look for, both locally and as a meta-annotation
 * @return the first matching annotation, or {@code null} if not found
 * @since 3.1/*from  w  ww . j  a v  a 2s . c  o  m*/
 */
@Nullable
public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement,
        Class<A> annotationType) {
    try {
        A annotation = annotatedElement.getAnnotation(annotationType);
        if (annotation == null) {
            for (Annotation metaAnn : annotatedElement.getAnnotations()) {
                annotation = metaAnn.annotationType().getAnnotation(annotationType);
                if (annotation != null) {
                    break;
                }
            }
        }
        return (annotation != null ? synthesizeAnnotation(annotation, annotatedElement) : null);
    } catch (Throwable ex) {
        handleIntrospectionFailure(annotatedElement, ex);
        return null;
    }
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Get all {@link Annotation Annotations} that are <em>present</em> on the
 * supplied {@link AnnotatedElement}.//from  www .j a  va2  s . c o  m
 * <p>Meta-annotations will <em>not</em> be searched.
 * @param annotatedElement the Method, Constructor or Field to retrieve annotations from
 * @return the annotations found, an empty array, or {@code null} if not
 * resolvable (e.g. because nested Class values in annotation attributes
 * failed to resolve at runtime)
 * @since 4.0.8
 * @see AnnotatedElement#getAnnotations()
 */
@Nullable
public static Annotation[] getAnnotations(AnnotatedElement annotatedElement) {
    try {
        return synthesizeAnnotationArray(annotatedElement.getAnnotations(), annotatedElement);
    } catch (Throwable ex) {
        handleIntrospectionFailure(annotatedElement, ex);
        return null;
    }
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Get the annotation with the supplied {@code annotationName} on the
 * supplied {@code element}.//from  w  w w  .  j a va 2  s. com
 * @param element the element to search on
 * @param annotationName the fully qualified class name of the annotation
 * type to find
 * @return the annotation if found; {@code null} otherwise
 * @since 4.2
 */
@Nullable
static Annotation getAnnotation(AnnotatedElement element, String annotationName) {
    for (Annotation annotation : element.getAnnotations()) {
        if (annotation.annotationType().getName().equals(annotationName)) {
            return annotation;
        }
    }
    return null;
}