Example usage for java.lang.reflect AccessibleObject getAnnotations

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

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java

/**
 * Fill annots.//w  ww .  ja  va2s. com
 *
 * @param ao the ao
 * @param anots the anots
 */
private static void fillAnnots(AccessibleObject ao, List<Annotation> anots) {
    for (Annotation anot : ao.getAnnotations()) {
        anots.add(anot);
    }
}

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

private static void createModelBean(ModelBean model, AccessibleObject accessibleObject,
        OutputConfig outputConfig) {/*w w w .ja va2 s  . c  o  m*/
    Class<?> javaType = null;
    String name = null;
    Class<?> declaringClass = null;

    if (accessibleObject instanceof Field) {
        Field field = (Field) accessibleObject;
        javaType = field.getType();
        name = field.getName();
        declaringClass = field.getDeclaringClass();
    } else if (accessibleObject instanceof Method) {
        Method method = (Method) accessibleObject;

        javaType = method.getReturnType();
        if (javaType.equals(Void.TYPE)) {
            return;
        }

        if (method.getName().startsWith("get")) {
            name = StringUtils.uncapitalize(method.getName().substring(3));
        } else if (method.getName().startsWith("is")) {
            name = StringUtils.uncapitalize(method.getName().substring(2));
        } else {
            name = method.getName();
        }

        declaringClass = method.getDeclaringClass();
    }

    ModelType modelType = null;
    if (model.isAutodetectTypes()) {
        for (ModelType mt : ModelType.values()) {
            if (mt.supports(javaType)) {
                modelType = mt;
                break;
            }
        }
    } else {
        modelType = ModelType.AUTO;
    }

    ModelFieldBean modelFieldBean = null;

    ModelField modelFieldAnnotation = accessibleObject.getAnnotation(ModelField.class);
    if (modelFieldAnnotation != null) {

        if (StringUtils.hasText(modelFieldAnnotation.value())) {
            name = modelFieldAnnotation.value();
        }

        if (StringUtils.hasText(modelFieldAnnotation.customType())) {
            modelFieldBean = new ModelFieldBean(name, modelFieldAnnotation.customType());
        } else {
            ModelType type = null;
            if (modelFieldAnnotation.type() != ModelType.NOT_SPECIFIED) {
                type = modelFieldAnnotation.type();
            } else {
                type = modelType;
            }

            modelFieldBean = new ModelFieldBean(name, type);
        }

        updateModelFieldBean(modelFieldBean, modelFieldAnnotation);
        model.addField(modelFieldBean);
    } else {
        if (modelType != null) {
            modelFieldBean = new ModelFieldBean(name, modelType);
            model.addField(modelFieldBean);
        }
    }

    ModelId modelIdAnnotation = accessibleObject.getAnnotation(ModelId.class);
    if (modelIdAnnotation != null) {
        model.setIdProperty(name);
    }

    ModelClientId modelClientId = accessibleObject.getAnnotation(ModelClientId.class);
    if (modelClientId != null) {
        model.setClientIdProperty(name);
        model.setClientIdPropertyAddToWriter(modelClientId.configureWriter());
    }

    ModelVersion modelVersion = accessibleObject.getAnnotation(ModelVersion.class);
    if (modelVersion != null) {
        model.setVersionProperty(name);
    }

    ModelAssociation modelAssociationAnnotation = accessibleObject.getAnnotation(ModelAssociation.class);
    if (modelAssociationAnnotation != null) {
        model.addAssociation(AbstractAssociation.createAssociation(modelAssociationAnnotation, model, javaType,
                declaringClass, name));
    }

    if (modelFieldBean != null && outputConfig.getIncludeValidation() != IncludeValidation.NONE) {

        Set<ModelValidation> modelValidationAnnotations = AnnotationUtils
                .getRepeatableAnnotation(accessibleObject, ModelValidations.class, ModelValidation.class);
        if (!modelValidationAnnotations.isEmpty()) {
            for (ModelValidation modelValidationAnnotation : modelValidationAnnotations) {
                AbstractValidation modelValidation = AbstractValidation.createValidation(name,
                        modelValidationAnnotation, outputConfig.getIncludeValidation());
                if (modelValidation != null) {
                    model.addValidation(modelValidation);
                }
            }
        } else {
            Annotation[] fieldAnnotations = accessibleObject.getAnnotations();

            for (Annotation fieldAnnotation : fieldAnnotations) {
                AbstractValidation.addValidationToModel(model, modelFieldBean, fieldAnnotation,
                        outputConfig.getIncludeValidation());
            }

            if (accessibleObject instanceof Field) {
                PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(declaringClass, name);
                if (pd != null && pd.getReadMethod() != null) {
                    for (Annotation readMethodAnnotation : pd.getReadMethod().getAnnotations()) {
                        AbstractValidation.addValidationToModel(model, modelFieldBean, readMethodAnnotation,
                                outputConfig.getIncludeValidation());
                    }
                }
            }
        }
    }

}

From source file:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.java

@Nullable
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
    if (ao.getAnnotations().length > 0) {
        for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
            AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
            if (attributes != null) {
                return attributes;
            }//from  w  w  w. ja v a  2  s .  c om
        }
    }
    return null;
}