Example usage for java.lang.reflect AccessibleObject getAnnotation

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

private void gatherPkColumn(Class entityClass) {
    Set<AccessibleObject> fieldsOrMethods = getAnnotatedFieldOrMethod(new HashSet<AccessibleObject>(), Id.class,
            entityClass, true);/* w ww. j a  v a  2  s. c o  m*/

    if (entityClass.isAnnotationPresent(PrimaryKeyJoinColumn.class)) {
        pkColnames = Arrays
                .asList(((PrimaryKeyJoinColumn) entityClass.getAnnotation(PrimaryKeyJoinColumn.class)).name());
    } else if (fieldsOrMethods.size() > 0) {
        pkColnames = new ArrayList<String>();
        for (AccessibleObject fieldOrMethod : fieldsOrMethods) {
            String colName = null;
            if (fieldOrMethod.isAnnotationPresent(Column.class)) {
                colName = fieldOrMethod.getAnnotation(Column.class).name();
            }
            if (colName == null && fieldOrMethod instanceof Field) {
                colName = ((Field) fieldOrMethod).getName();
            }
            if (colName != null) {
                pkColnames.add(colName);
            }
        }
    }
}

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

private void gatherCascade(final Map<String, String> attr2colName, final String attrPath, Class type) {
    //        HACK: we are using field annotations, need to skip methods otherwise we would have wrong constraints
    traverseFieldsAndMethods(type, true, false, new GatherFieldOrMethodInfoAction() {
        @Override/*ww w .  ja v a2s. com*/
        public void gatherInfo(AccessibleObject fOm) {
            String attributeName = getAttributeName(fOm);
            String attrPathField = attrPath + "." + attributeName;
            Class attributeType = getAttributeType(fOm);
            boolean onDeleteCascade = fOm.isAnnotationPresent(OnDelete.class)
                    ? fOm.getAnnotation(OnDelete.class).action() == OnDeleteAction.CASCADE
                    : false;

            if (fOm.isAnnotationPresent(Embedded.class)) {
                addAttributeOverrides(attr2colName, attrPathField, fOm);
                gatherCascade(attr2colName, attrPathField, attributeType);
            }

            if (fOm.isAnnotationPresent(CascadeNullable.class)) {
                if (onDeleteCascade) {
                    throw new IllegalStateException(
                            "conflicting declaration of @CascadeNullable and CascadeType on relation " + fOm);
                }
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(JoinColumn.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(JoinColumn.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                    columnNamesIsCascadeNullable.add(colName);
                }
            }

            if (fOm.isAnnotationPresent(NoForeignKeyConstraint.class)) {
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(JoinColumn.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(JoinColumn.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                    columnNamesIsNoFK.add(colName);
                }
            }

            Set<String> fkCols = new HashSet<String>();
            String fkTableName = null;
            if (fOm.isAnnotationPresent(JoinTable.class)) {
                JoinTable joinTable = fOm.getAnnotation(JoinTable.class);
                fkCols.add(getIdentifierForReference(joinTable.joinColumns()[0].name()));
                if (joinTable.inverseJoinColumns().length > 0) {
                    fkCols.add(getIdentifierForReference(joinTable.inverseJoinColumns()[0].name()));
                }
                fkTableName = joinTable.name();
            } else if (fOm.isAnnotationPresent(OneToMany.class)) {
                JoinColumn joinColumn = getJoinColumn(fOm);
                if (joinColumn != null) {
                    Class<?> targetEntityClass = MappingUtils.determineTargetEntity(fOm,
                            fOm.getAnnotation(OneToMany.class).targetEntity());
                    fkTableName = getTableAnnotation(targetEntityClass).name();
                    fkCols.add(getIdentifierForReference(joinColumn.name()));
                }
            } else if (fOm.isAnnotationPresent(ManyToOne.class) || fOm.isAnnotationPresent(OneToOne.class)) {
                JoinColumn joinColumn = getJoinColumn(fOm);
                if (joinColumn != null) {
                    fkTableName = tableName;
                    fkCols.add(getIdentifierForReference(joinColumn.name()));
                }
            }

            if (fkTableName != null && (onDeleteCascade || fOm.isAnnotationPresent(CascadeDelete.class))) {
                TableConfig fkConfig = ddlEnhancer
                        .getConfigForTableName(getIdentifierForReference(fkTableName));
                if (fkConfig != null) {
                    fkConfig.columnNamesIsCascadeDelete.addAll(fkCols);
                }
            }
        }
    });
}

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

private void gatherDeferrable(final Map<String, String> attr2colName, final String attrPath, Class type) {
    traverseFieldsAndMethods(type, true, true, new GatherFieldOrMethodInfoAction() {
        @Override/*from   ww w  .j a  v  a 2s  . com*/
        public void gatherInfo(AccessibleObject fOm) {
            String attributeName = getAttributeName(fOm);
            String attrPathField = attrPath + "." + attributeName;
            Class attributeType = getAttributeType(fOm);

            if (fOm.isAnnotationPresent(Embedded.class)) {
                addAttributeOverrides(attr2colName, attrPathField, fOm);
                gatherDeferrable(attr2colName, attrPathField, attributeType);
            } else if (fOm.isAnnotationPresent(Deferrable.class)) {
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(JoinColumn.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(JoinColumn.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                    columnNameToDeferrable.put(colName, fOm.getAnnotation(Deferrable.class));
                }
            }
        }
    });
}

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

private void gatherEnumCheckConstraints(final Map<String, String> attr2colName, final String attrPath,
        Class type) {//ww  w  . j  a v  a2  s.c om
    //        HACK: we are using field annotations, need to skip methods otherwise we would have wrong constraints
    traverseFieldsAndMethods(type, true, false, new GatherFieldOrMethodInfoAction() {
        @Override
        public void gatherInfo(AccessibleObject fOm) {
            String attributeName = getAttributeName(fOm);
            String attrPathField = attrPath + "." + attributeName;
            Class attributeType = getAttributeType(fOm);

            if (fOm.isAnnotationPresent(Embedded.class)) {
                addAttributeOverrides(attr2colName, attrPathField, fOm);
                gatherEnumCheckConstraints(attr2colName, attrPathField, attributeType);
            } else if (attributeType.isEnum()) {
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(Column.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(Column.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                }
                boolean useName = fOm.isAnnotationPresent(Enumerated.class)
                        && fOm.getAnnotation(Enumerated.class).value() == EnumType.STRING;
                StringBuilder cons = new StringBuilder();
                for (Object e : attributeType.getEnumConstants()) {
                    if (cons.length() > 0) {
                        cons.append(", ");
                    }
                    if (useName) {
                        cons.append("'").append(((Enum) e).name()).append("'");
                    } else {
                        cons.append(((Enum) e).ordinal());
                    }
                }
                columnNameToEnumCheckConstraints.put(colName, cons.toString());
            }
        }
    });

    Class superClass = getEntitySuperclass(type);
    if (superClass != null) {
        gatherEnumCheckConstraints(attr2colName, attrPath, superClass);
    }
}

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

private static void createModelBean(ModelBean model, AccessibleObject accessibleObject,
        OutputConfig outputConfig) {//from  w  ww  . j a v  a2 s.  c  om
    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:com.yahoo.elide.core.EntityBinding.java

/**
 * Bind an attribute or relationship./*  w  ww. jav  a 2 s  .  co m*/
 *
 * @param cls Class type to bind fields
 * @param fieldOrMethod Field or method to bind
 */
private void bindAttrOrRelation(Class<?> cls, AccessibleObject fieldOrMethod) {
    boolean manyToMany = fieldOrMethod.isAnnotationPresent(ManyToMany.class);
    boolean manyToOne = fieldOrMethod.isAnnotationPresent(ManyToOne.class);
    boolean oneToMany = fieldOrMethod.isAnnotationPresent(OneToMany.class);
    boolean oneToOne = fieldOrMethod.isAnnotationPresent(OneToOne.class);
    boolean isRelation = manyToMany || manyToOne || oneToMany || oneToOne;

    String fieldName = getFieldName(fieldOrMethod);

    if (fieldName == null || fieldName.equals("id") || fieldName.equals("class")
            || OBJ_METHODS.contains(fieldOrMethod)
            || parameterizedFieldContainsAnnotation(fieldOrMethod, Arrays.asList(Exclude.class))) {
        return; // Reserved. Not attributes. Otherwise, potentially excluded.
    }

    Class<?> fieldType = getFieldType(fieldOrMethod);

    ConcurrentLinkedDeque<String> fieldList;
    if (isRelation) {
        fieldList = relationshipsDeque;
        RelationshipType type;
        String mappedBy;
        if (oneToMany) {
            type = RelationshipType.ONE_TO_MANY;
            mappedBy = fieldOrMethod.getAnnotation(OneToMany.class).mappedBy();
        } else if (oneToOne) {
            type = RelationshipType.ONE_TO_ONE;
            mappedBy = fieldOrMethod.getAnnotation(OneToOne.class).mappedBy();
        } else if (manyToMany) {
            type = RelationshipType.MANY_TO_MANY;
            mappedBy = fieldOrMethod.getAnnotation(ManyToMany.class).mappedBy();
        } else if (manyToOne) {
            type = RelationshipType.MANY_TO_ONE;
            mappedBy = "";
        } else {
            type = RelationshipType.NONE;
            mappedBy = "";
        }
        relationshipTypes.put(fieldName, type);
        relationshipToInverse.put(fieldName, mappedBy);
    } else {
        fieldList = attrsDeque;
    }

    fieldList.push(fieldName);
    fieldsToValues.put(fieldName, fieldOrMethod);
    fieldsToTypes.put(fieldName, fieldType);
}

From source file:net.stickycode.mockwire.spring30.MockwireFieldInjectionAnnotationBeanPostProcessor.java

private Annotation findAutowiredAnnotation(AccessibleObject ao) {
    for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
        Annotation annotation = ao.getAnnotation(type);
        if (annotation != null) {
            return annotation;
        }/*w w w  .  ja  v  a  2  s  .  c  o m*/
    }
    return null;
}

From source file:org.apache.bval.jsr.BeanDescriptorImpl.java

private static void processConvertGroup(final ElementDescriptorImpl edesc, final AccessibleObject accessible) {
    final ConvertGroup.List convertGroupList = accessible.getAnnotation(ConvertGroup.List.class);
    if (convertGroupList != null) {
        for (final ConvertGroup convertGroup : convertGroupList.value()) {
            edesc.addGroupConversion(new GroupConversionDescriptorImpl(new Group(convertGroup.from()),
                    new Group(convertGroup.to())));
        }/* www .j a  v  a2s .co  m*/
    }
    final ConvertGroup convertGroup = accessible.getAnnotation(ConvertGroup.class);
    if (convertGroup != null) {
        edesc.addGroupConversion(new GroupConversionDescriptorImpl(new Group(convertGroup.from()),
                new Group(convertGroup.to())));
    }
}

From source file:org.compass.spring.support.CompassContextBeanPostProcessor.java

private void addIfPresent(List<AnnotatedMember> metadata, AccessibleObject ao) {
    CompassContext compassContext = ao.getAnnotation(CompassContext.class);
    if (compassContext != null) {
        metadata.add(new AnnotatedMember(compassContext.name(), ao));
    }/*from  w w w . j  ava  2  s. c o  m*/
}

From source file:org.mule.module.extension.internal.util.IntrospectionUtils.java

public static boolean isIgnored(AccessibleObject object) {
    return object.getAnnotation(Ignore.class) != null;
}