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:ca.uhn.fhir.context.ModelScanner.java

/**
 * There are two implementations of all of the annotations (e.g. {@link Child} and {@link org.hl7.fhir.instance.model.annotations.Child}) since the HL7.org ones will eventually replace the HAPI
 * ones. Annotations can't extend each other or implement interfaces or anything like that, so rather than duplicate all of the annotation processing code this method just creates an interface
 * Proxy to simulate the HAPI annotations if the HL7.org ones are found instead.
 *//*w  w w  .j  a  v  a  2 s .  co  m*/
static <T extends Annotation> T pullAnnotation(AnnotatedElement theTarget, Class<T> theAnnotationType) {
    T retVal = theTarget.getAnnotation(theAnnotationType);
    return retVal;
}

From source file:com.medsphere.fileman.FMRecord.java

static private void accumulateJavaDomainFields(Map<String, AnnotatedElement> domainFields,
        Class<? extends FMRecord> domainClass) {
    if (domainClass != null && domainClass.getSuperclass() != null) {
        Class<? extends FMRecord> superClass = (Class<? extends FMRecord>) domainClass.getSuperclass();
        accumulateJavaDomainFields(domainFields, superClass);
    }/*  ww  w.j ava2  s .co  m*/
    for (AnnotatedElement e : domainClass.getDeclaredFields()) {
        FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class);
        if (annote != null) {
            domainFields.put(annote.number(), e);
        }
    }
    for (AnnotatedElement e : domainClass.getDeclaredMethods()) {
        FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class);
        if (annote != null) {
            domainFields.put(annote.number(), e);
        }
    }

}

From source file:com.medsphere.fileman.FMRecord.java

private static void accumulateNumericMapping(Map<String, String> numberMap,
        Class<? extends FMRecord> domainClass) {

    if (domainClass != null && domainClass.getSuperclass() != null) {
        Class<? extends FMRecord> superClass = (Class<? extends FMRecord>) domainClass.getSuperclass();
        accumulateNumericMapping(numberMap, superClass);
    }//from  w w  w  .ja  v  a  2 s. co  m

    for (AnnotatedElement e : domainClass.getDeclaredFields()) {
        FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class);
        if (annote != null) {
            numberMap.put(annote.name(), annote.number());
        }
    }
    for (AnnotatedElement e : domainClass.getDeclaredMethods()) {
        FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class);
        if (annote != null) {
            numberMap.put(annote.name(), annote.number());
        }
    }
}

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//  w  ww  .j a va2 s .  c  o  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:acmi.l2.clientmod.xdat.Controller.java

private static List<PropertySheetItem> loadProperties(Object obj) {
    Class<?> objClass = obj.getClass();
    List<PropertySheetItem> list = new ArrayList<>();
    while (objClass != Object.class) {
        try {/*from w ww.java 2  s. c o m*/
            List<String> names = Arrays.stream(objClass.getDeclaredFields())
                    .map(field -> field.getName().replace("Prop", "")).collect(Collectors.toList());
            BeanInfo beanInfo = Introspector.getBeanInfo(objClass, objClass.getSuperclass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            Arrays.sort(propertyDescriptors,
                    (pd1, pd2) -> Integer.compare(names.indexOf(pd1.getName()), names.indexOf(pd2.getName())));
            for (PropertyDescriptor descriptor : propertyDescriptors) {
                if ("metaClass".equals(descriptor.getName()))
                    continue;

                if (Collection.class.isAssignableFrom(descriptor.getPropertyType()))
                    continue;

                AnnotatedElement getter = descriptor.getReadMethod();
                if (getter.isAnnotationPresent(Deprecated.class) || getter.isAnnotationPresent(Hide.class))
                    continue;

                String description = "";
                if (getter.isAnnotationPresent(Description.class))
                    description = getter.getAnnotation(Description.class).value();
                Class<? extends PropertyEditor<?>> propertyEditorClass = null;
                if (descriptor.getPropertyType() == Boolean.class
                        || descriptor.getPropertyType() == Boolean.TYPE) {
                    propertyEditorClass = BooleanPropertyEditor.class;
                } else if (getter.isAnnotationPresent(Tex.class)) {
                    propertyEditorClass = TexturePropertyEditor.class;
                } else if (getter.isAnnotationPresent(Sysstr.class)) {
                    propertyEditorClass = SysstringPropertyEditor.class;
                }
                BeanProperty property = new BeanProperty(descriptor, objClass.getSimpleName(), description,
                        propertyEditorClass);
                list.add(property);
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        objClass = objClass.getSuperclass();
    }
    return list;
}

From source file:com.tojc.ormlite.android.annotation.info.ProjectionMapInfo.java

public ProjectionMapInfo(AnnotatedElement element) {
    ProjectionMap projectionMap = element.getAnnotation(ProjectionMap.class);
    if (projectionMap != null) {
        this.name = projectionMap.value();
        validFlagOn();//from  w  w w.  j a va  2 s  .c  o  m
    }
}

From source file:com.adobe.acs.commons.models.injectors.annotation.impl.JsonValueMapValueAnnotationProcessorFactory.java

@Override
public InjectAnnotationProcessor2 createAnnotationProcessor(AnnotatedElement element) {
    return Optional.ofNullable(element.getAnnotation(JsonValueMapValue.class))
            .map(JsonValueMapValueAnnotationProcessorFactory.InjectAnnotationProcessor2::new).orElse(null);
}

From source file:com.adobe.acs.commons.models.injectors.annotation.impl.HierarchicalPagePropertyAnnotationProcessorFactory.java

@Override
public InjectAnnotationProcessor2 createAnnotationProcessor(AnnotatedElement element) {
    return Optional.ofNullable(element.getAnnotation(HierarchicalPageProperty.class))
            .map(PagePropertyAnnotationProcessor::new).orElse(null);
}

From source file:jetbrains.exodus.entitystore.TestBase.java

private StoreArtifactsPolicy getAnnotationValue(AnnotatedElement element) {
    return element.getAnnotation(StoreArtifacts.class) == null ? null
            : getClass().getAnnotation(StoreArtifacts.class).storagePolicy();
}

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  ww  .j a  va2  s .  c om
        }
    }
    if (ann != null) {
        return parseAnnotation(ann);
    } else {
        return null;
    }
}