Example usage for java.lang.reflect AnnotatedElement getAnnotation

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

Introduction

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

Prototype

<T extends Annotation> T getAnnotation(Class<T> annotationClass);

Source Link

Document

Returns this element's annotation for the specified type if such an annotation is present, else null.

Usage

From source file:com.tojc.ormlite.android.annotation.OrmLiteAnnotationAccessor.java

/**
 * Gets the column name from DatabaseField annotation. If the DatabaseField#columnName is not
 * specified, returns the field name./*from  w  w  w. j a  va  2 s.c  o m*/
 * @param element
 *            Element to be evaluated.
 * @return Returns the column name.
 */
public static String getAnnotationColumnName(AnnotatedElement element) {
    String result = "";
    DatabaseField databaseField = element.getAnnotation(DatabaseField.class);
    if (databaseField != null) {
        result = databaseField.columnName();
        if (StringUtils.isEmpty(result)) {
            result = ((Field) element).getName();
        }
    }
    return result;
}

From source file:elaborate.jaxrs.JAXUtils.java

/**
 * Returns the path of the annotated element,
 * or an empty string if no annotation is present.
 *//*from  www  . jav  a2 s  . c o  m*/
static String pathValueOf(AnnotatedElement element) {
    Path annotation = element.getAnnotation(Path.class);
    String value = (annotation != null) ? annotation.value() : "";
    return StringUtils.removeStart(value, "/");
}

From source file:com.iorga.iraj.json.ObjectContextCaller.java

private static String getContextPath(final String elementName, final AnnotatedElement annotatedElement) {
    final ContextPath contextPathAnnotation = annotatedElement.getAnnotation(ContextPath.class);
    if (contextPathAnnotation != null) {
        return contextPathAnnotation.value();
    } else {//from   www .  j a va 2  s. co  m
        return elementName;
    }
}

From source file:Main.java

/**
 * Get an annotation.  This is wrappered to avoid a Java2Security violation.
 * @param cls Class that contains annotation 
 * @param annotation Class of requrested Annotation
 * @return annotation or null/* w  ww.ja v  a2  s .c  o m*/
 */
private static <T extends Annotation> T getAnnotation(final AnnotatedElement element,
        final Class<T> annotation) {
    return AccessController.doPrivileged(new PrivilegedAction<T>() {
        public T run() {
            return element.getAnnotation(annotation);
        }
    });
}

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);/*  w  w w.ja  v 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:com.oembedler.moon.graphql.engine.StereotypeUtils.java

private static <T extends Annotation> String getAnnotationValue(AnnotatedElement accessibleObject,
        Class<T> annotationClass, String defaultValue) {
    Assert.noNullElements(new Object[] { accessibleObject, annotationClass, defaultValue },
            "input parameters must not be null");

    String result = defaultValue;
    T annotation = accessibleObject.getAnnotation(annotationClass);
    if (annotation != null && StringUtils.hasText((String) AnnotationUtils.getValue(annotation)))
        result = (String) AnnotationUtils.getValue(annotation);

    return result;
}

From source file:com.codelanx.codelanxlib.util.Reflections.java

/**
 * Returns {@code true} if the specified target has the passed
 * {@link Annotation}//  www  . j  a v a 2s  .  c  o  m
 * 
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param target The relevant element to check for an {@link Annotation}
 * @param check The {@link Annotation} class type to check for
 * @return {@code true} if the {@link Annotation} is present
 */
public static boolean hasAnnotation(AnnotatedElement target, Class<? extends Annotation> check) {
    return target.getAnnotation(check) != null;
}

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.
 * /*from  w w w  . j a v a 2  s.  c om*/
 * @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.xemantic.tadedon.guice.matcher.Annotations.java

/**
 * Finds if the {@code element} is annotated with the {@code annotationType}.
 * If the {@code element} is instance of {@link Class} or {@link Method},
 * the {@link AnnotationUtils#findAnnotation(Class, Class)} or
 * {@link AnnotationUtils#findAnnotation(Method, Class)} method will be used respectively. Thus it can
 * also find annotations from extended classes and implemented interfaces.
 *
 * @param element the element//from   www . j  av a  2s  .c o m
 * @param annotationType the annotation type.
 * @return annotation instance or {@code null} if no annotation is found.
 * @throws IllegalArgumentException if the {@code element} is not instance of {@link Class} or {@link Method}.
 */
public static Annotation findAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
    final Annotation annotation;
    if (element instanceof Class<?>) {
        annotation = AnnotationUtils.findAnnotation((Class<?>) element, annotationType);
    } else if (element instanceof Method) {
        annotation = AnnotationUtils.findAnnotation((Method) element, annotationType);
    } else {
        annotation = element.getAnnotation(annotationType);
    }
    return annotation;
}

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/* w w  w.  j a v a2  s .  c  o  m*/
 */
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;
}