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.haulmont.cuba.core.sys.MetaModelLoader.java

@Nullable
protected Datatype getAdaptiveDatatype(AnnotatedElement annotatedElement) {
    com.haulmont.chile.core.annotations.MetaProperty annotation = annotatedElement
            .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class);
    return annotation != null && !annotation.datatype().equals("") ? datatypes.get(annotation.datatype())
            : null;//from  w  w  w  .  j av  a2 s . co  m
}

From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java

protected void loadBeanValidationAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) {
    NotNull notNull = annotatedElement.getAnnotation(NotNull.class);
    if (notNull != null) {
        if (isDefinedForDefaultValidationGroup(notNull)) {
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_MESSAGE,
                    notNull.message());/*from   w  ww .  ja v  a 2 s .c  o m*/
        }
        if (isDefinedForValidationGroup(notNull, UiComponentChecks.class, true)) {
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_MESSAGE,
                    notNull.message());
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_UI_COMPONENT, true);
        }
    }

    Size size = annotatedElement.getAnnotation(Size.class);
    if (size != null && isDefinedForDefaultValidationGroup(size)) {
        metaProperty.getAnnotations().put(Size.class.getName() + VALIDATION_MIN, size.min());
        metaProperty.getAnnotations().put(Size.class.getName() + VALIDATION_MAX, size.max());
    }

    Length length = annotatedElement.getAnnotation(Length.class);
    if (length != null && isDefinedForDefaultValidationGroup(length)) {
        metaProperty.getAnnotations().put(Length.class.getName() + VALIDATION_MIN, length.min());
        metaProperty.getAnnotations().put(Length.class.getName() + VALIDATION_MAX, length.max());
    }

    Min min = annotatedElement.getAnnotation(Min.class);
    if (min != null && isDefinedForDefaultValidationGroup(min)) {
        metaProperty.getAnnotations().put(Min.class.getName(), min.value());
    }

    Max max = annotatedElement.getAnnotation(Max.class);
    if (max != null && isDefinedForDefaultValidationGroup(max)) {
        metaProperty.getAnnotations().put(Max.class.getName(), max.value());
    }

    DecimalMin decimalMin = annotatedElement.getAnnotation(DecimalMin.class);
    if (decimalMin != null && isDefinedForDefaultValidationGroup(decimalMin)) {
        metaProperty.getAnnotations().put(DecimalMin.class.getName(), decimalMin.value());
    }

    DecimalMax decimalMax = annotatedElement.getAnnotation(DecimalMax.class);
    if (decimalMax != null && isDefinedForDefaultValidationGroup(decimalMax)) {
        metaProperty.getAnnotations().put(DecimalMax.class.getName(), decimalMax.value());
    }

    Past past = annotatedElement.getAnnotation(Past.class);
    if (past != null && isDefinedForDefaultValidationGroup(past)) {
        metaProperty.getAnnotations().put(Past.class.getName(), true);
    }

    Future future = annotatedElement.getAnnotation(Future.class);
    if (future != null && isDefinedForDefaultValidationGroup(future)) {
        metaProperty.getAnnotations().put(Future.class.getName(), true);
    }
}

From source file:com.qrmedia.pattern.compositeannotation.CompositeAnnotatedElements.java

@SuppressWarnings("unchecked")
public <T extends Annotation> T getAnnotation(AnnotatedElement annotatedElement, Class<T> annotationClass) {
    checkNotNull("The element and class must be non-null", annotatedElement, annotationClass);

    // if *no* composite provides the type it cannot be present
    if (!leafAnnotationTypeProvidingCompositeTypes.containsKey(annotationClass)) {
        return null;
    }/*from  w  ww  .  j ava  2s .co  m*/

    // throws an IllegalArgumentException if there is more than one providing composite
    Collection<Class<? extends Annotation>> presentProvidingCompositeTypes = getPresentProvidingCompositeTypes(
            annotatedElement, annotationClass);

    if (presentProvidingCompositeTypes.isEmpty()) {
        return null;
    }

    Class<? extends Annotation> presentProvidingCompositeType = presentProvidingCompositeTypes.iterator()
            .next();

    return ((CompositeAnnotationDescriptor<Annotation>) compositeAnnotationTypeDescriptors
            .get(presentProvidingCompositeType)).getLeafAnnotation(
                    annotatedElement.getAnnotation(presentProvidingCompositeType), annotationClass);
}

From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java

protected void loadPropertyAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) {
    for (Annotation annotation : annotatedElement.getAnnotations()) {
        MetaAnnotation metaAnnotation = AnnotationUtils.findAnnotation(annotation.getClass(),
                MetaAnnotation.class);
        if (metaAnnotation != null) {
            Map<String, Object> attributes = new LinkedHashMap<>(
                    AnnotationUtils.getAnnotationAttributes(annotatedElement, annotation));
            metaProperty.getAnnotations().put(annotation.annotationType().getName(), attributes);
        }/*from   ww w  . j  a v  a 2s.co  m*/
    }

    com.haulmont.chile.core.annotations.MetaProperty metaPropertyAnnotation = annotatedElement
            .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class);
    if (metaPropertyAnnotation != null) {
        String[] related = metaPropertyAnnotation.related();
        if (!(related.length == 1 && related[0].equals(""))) {
            metaProperty.getAnnotations().put("relatedProperties", Joiner.on(',').join(related));
        }
    }

    loadBeanValidationAnnotations(metaProperty, annotatedElement);
}

From source file:com.qrmedia.pattern.compositeannotation.CompositeAnnotatedElements.java

@SuppressWarnings("unchecked")
private Annotation[] getAnnotations(AnnotatedElement annotatedElement, boolean includeInherited) {
    checkNotNull("The element must be non-null", annotatedElement);

    Set<Annotation> leafAnnotations = new HashSet<Annotation>();
    Set<Class<? extends Annotation>> presentLeafAnnotationTypes = new HashSet<Class<? extends Annotation>>();

    for (Class<? extends Annotation> compositeAnnotationType : getCompositeAnnotationTypes(annotatedElement,
            includeInherited)) {//from   w  w w .ja v a 2  s .  com
        CompositeAnnotationDescriptor<Annotation> compositeAnnotationDescriptor = (CompositeAnnotationDescriptor<Annotation>) compositeAnnotationTypeDescriptors
                .get(compositeAnnotationType);
        Set<Class<? extends Annotation>> newLeafAnnotationTypes = compositeAnnotationDescriptor
                .getLeafAnnotationTypes();

        // multiple composites may not return leaf annotations of the same type
        if (CollectionUtils.containsAny(presentLeafAnnotationTypes, newLeafAnnotationTypes)) {
            throw new IllegalArgumentException(annotatedElement
                    + " is annotated with multiple composite annotations that declare leaves of the same type");
        }

        presentLeafAnnotationTypes.addAll(newLeafAnnotationTypes);
        leafAnnotations.addAll(compositeAnnotationDescriptor
                .getLeafAnnotations(annotatedElement.getAnnotation(compositeAnnotationType)));
    }

    return leafAnnotations.toArray(new Annotation[0]);
}

From source file:net.sf.json.JSONObject.java

private static boolean isTransient(AnnotatedElement element, JsonConfig jsonConfig) {
    for (Iterator annotations = jsonConfig.getIgnoreFieldAnnotations().iterator(); annotations.hasNext();) {
        try {//ww w  .ja v  a  2s.  c  o  m
            String annotationClassName = (String) annotations.next();
            if (element.getAnnotation((Class) Class.forName(annotationClassName)) != null)
                return true;
        } catch (Exception e) {
            log.info("Error while inspecting " + element + " for transient status.", e);
        }
    }
    return false;
}

From source file:org.apache.axis2.jaxws.description.impl.OperationDescriptionImpl.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//from  w  ww  .j a v a  2  s  .  c  o  m
 */
private static Annotation getAnnotation(final AnnotatedElement element, final Class annotation) {
    return (Annotation) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return element.getAnnotation(annotation);
        }
    });
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

private static Annotation getAnnotation(final AnnotatedElement element, final Class annotation) {
    return (Annotation) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return element.getAnnotation(annotation);
        }//from  w ww  .jav a2 s.  c o  m
    });
}

From source file:org.apache.axis2.jaxws.runtime.description.marshal.impl.ArtifactProcessor.java

/**
 * Get an annotation.  This is wrappered to avoid a Java2Security violation.
 * @param cls Class that contains annotation 
 * @param annotation Class of requested Annotation
 * @return annotation or null//from   w w  w . j ava 2s  .  c  om
 */
private static Annotation getAnnotation(final AnnotatedElement element, final Class annotation) {
    Annotation anno = null;
    try {
        anno = (Annotation) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return element.getAnnotation(annotation);
            }
        });
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("Problem occurred.  Continuing.  The problem is " + t);
        }
    }
    return anno;
}

From source file:org.apache.camel.component.bean.MethodInfo.java

/**
 * Returns the pattern annotation on the given annotated element; either as a direct annotation or
 * on an annotation which is also annotated
 *
 * @param annotatedElement the element to look for the annotation
 * @param depth the current depth//ww  w  . j  a v a2 s.  c  om
 * @return the first matching annotation or null if none could be found
 */
protected Pattern getPatternAnnotation(AnnotatedElement annotatedElement, int depth) {
    Pattern answer = annotatedElement.getAnnotation(Pattern.class);
    int nextDepth = depth - 1;

    if (nextDepth > 0) {
        // lets look at all the annotations to see if any of those are annotated
        Annotation[] annotations = annotatedElement.getAnnotations();
        for (Annotation annotation : annotations) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (annotation instanceof Pattern || annotationType.equals(annotatedElement)) {
                continue;
            } else {
                Pattern another = getPatternAnnotation(annotationType, nextDepth);
                if (pattern != null) {
                    if (answer == null) {
                        answer = another;
                    } else {
                        LOG.warn("Duplicate pattern annotation: " + another + " found on annotation: "
                                + annotation + " which will be ignored");
                    }
                }
            }
        }
    }
    return answer;
}