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:cop.raml.mocks.MockUtils.java

private static <T extends AnnotatedConstructMock> T setAnnotations(AnnotatedConstructMock mock,
        AnnotatedElement element) {
    if (mock != null && element != null)
        Arrays.stream(element.getAnnotations()).forEach(mock::addAnnotation);
    return (T) mock;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

@Nullable
private static Object getAnnotationValue(AnnotatedElement annotated, String name)
        throws ReflectiveOperationException {
    for (Annotation annotation : annotated.getAnnotations()) {
        Class annotationType = annotation.annotationType();
        if (name.equals(ClassUtils.getShortClassName(annotationType))) {
            Method method = annotationType.getMethod("value");
            return method.invoke(annotation);
        }// ww  w.  ja va  2s .c  o m
    }
    return null;
}

From source file:com.icfcc.cache.annotation.SpringCacheAnnotationParser.java

private static <T extends Annotation> Collection<T> getAnnotations(AnnotatedElement ae,
        Class<T> annotationType) {
    Collection<T> anns = new ArrayList<T>(2);

    // look at raw annotation
    T ann = ae.getAnnotation(annotationType);
    if (ann != null) {
        anns.add(ann);/*from  www .jav a2s. c o m*/
    }

    // scan meta-annotations
    for (Annotation metaAnn : ae.getAnnotations()) {
        ann = metaAnn.annotationType().getAnnotation(annotationType);
        if (ann != null) {
            anns.add(ann);
        }
    }

    return (anns.isEmpty() ? null : anns);
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Get a single {@link Annotation} of {@code annotationType} from the supplied Method, Constructor or Field.
 * Meta-annotations will be searched if the annotation is not declared locally on the supplied element.
 * // w  ww . j av a  2 s.  c o  m
 * @param ae
 *            the Method, Constructor or Field from which to get the annotation
 * @param annotationType
 *            the annotation class to look for, both locally and as a meta-annotation
 * @return the matching annotation or {@code null} if not found
 * @since 3.1
 */
public static <T extends Annotation> T getAnnotation(final AnnotatedElement ae, final Class<T> annotationType) {
    T ann = ae.getAnnotation(annotationType);
    if (ann == null) {
        for (final Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(annotationType);
            if (ann != null) {
                break;
            }
        }
    }
    return ann;
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

/**
 * Get a single {@link java.lang.annotation.Annotation} of {@code annotationType} from the supplied
 * Method, Constructor or Field. Meta-annotations will be searched if the annotation
 * is not declared locally on the supplied element.
 * @param ae the Method, Constructor or Field from which to get the annotation
 * @param annotationType the annotation class to look for, both locally and as a meta-annotation
 * @return the matching annotation or {@code null} if not found
 * @since 3.1//from   ww  w  .  j  a v  a  2s . com
 */
public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) {
    T ann = ae.getAnnotation(annotationType);
    if (ann == null) {
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(annotationType);
            if (ann != null) {
                break;
            }
        }
    }
    return ann;
}

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

/**
 * Get all {@link Annotation Annotations} that are <em>present</em> on the
 * supplied {@link AnnotatedElement}.//from w w  w  .ja v a2  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
 */
public static Annotation[] getAnnotations(AnnotatedElement annotatedElement) {
    try {
        return annotatedElement.getAnnotations();
    } catch (Exception ex) {
        // Assuming nested Class values not resolvable within annotation attributes...
        logIntrospectionFailure(annotatedElement, ex);
    }
    return null;
}

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

/**
 * Get the possibly repeating {@link Annotation}s of {@code annotationType} from the
 * supplied {@link AnnotatedElement}.//from  ww w .  j a  va2s  . c  o  m
 * <p>Deals with both a single direct annotation and repeating annotations
 * nested within a containing annotation.
 * <p>Meta-annotations will be searched if the annotation is not
 * <em>directly present</em> on the supplied element.
 * @param annotatedElement the element to look for annotations on
 * @param containerAnnotationType the class of the container that holds the annotations
 * @param annotationType the annotation type to look for
 * @return the annotations found or an empty set; never {@code null}
 * @since 4.0
 * @see java.lang.annotation.Repeatable
 */
public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedElement annotatedElement,
        Class<? extends Annotation> containerAnnotationType, Class<A> annotationType) {

    try {
        if (annotatedElement.getAnnotations().length > 0) {
            return new AnnotationCollector<A>(containerAnnotationType, annotationType)
                    .getResult(annotatedElement);
        }
    } catch (Exception ex) {
        // Assuming nested Class values not resolvable within annotation attributes...
        logIntrospectionFailure(annotatedElement, ex);
    }
    return Collections.emptySet();
}

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

/**
 * Get a single {@link Annotation} of {@code annotationType} from the supplied
 * {@link AnnotatedElement}, where the {@code AnnotatedElement} is either
 * directly annotated or meta-annotated with the {@code annotationType}.
 * <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 matching annotation, or {@code null} if not found
 * @since 3.1//from  w w w .j  av a2  s  . co m
 */
public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement,
        Class<A> annotationType) {
    try {
        A ann = annotatedElement.getAnnotation(annotationType);
        if (ann == null) {
            for (Annotation metaAnn : annotatedElement.getAnnotations()) {
                ann = metaAnn.annotationType().getAnnotation(annotationType);
                if (ann != null) {
                    break;
                }
            }
        }
        return ann;
    } catch (Exception ex) {
        // Assuming nested Class values not resolvable within annotation attributes...
        logIntrospectionFailure(annotatedElement, ex);
        return null;
    }
}

From source file:com.sf.ddao.chain.ChainInvocationHandler.java

private void addChainMemebers(AnnotatedElement annotatedElement, List<Command> list) {
    for (Annotation annotation : annotatedElement.getAnnotations()) {
        final CommandAnnotation memberAnnotation = annotation.annotationType()
                .getAnnotation(CommandAnnotation.class);
        if (memberAnnotation == null || memberAnnotation.value() == null) {
            continue;
        }//from  w w  w  . j a  v  a2s  .  c om
        final Command chainCommand = injector.getInstance(memberAnnotation.value());
        if (chainCommand instanceof Intializible) {
            Intializible intializible = (Intializible) chainCommand;
            intializible.init(annotatedElement, annotation);
        }
        list.add(chainCommand);
    }
}

From source file:com.gradecak.alfresco.mvc.aop.RunAsAdvice.java

private AlfrescoRunAs parseRunAsAnnotation(AnnotatedElement ae) {
    AlfrescoRunAs ann = ae.getAnnotation(AlfrescoRunAs.class);
    if (ann == null) {
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(AlfrescoRunAs.class);
            if (ann != null) {
                break;
            }/*from w w  w  .j  a  v a 2s . com*/
        }
    }
    if (ann != null) {
        return parseAnnotation(ann);
    } else {
        return null;
    }
}