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.hst.CreateEntities.java

private String createHistoryEntity(String baseClassPackageName, AccessibleObject fieldOrMethod,
        Reader template) {/*from  ww  w .ja v  a  2  s  .c  o  m*/
    TemporalEntity temporalEntity = fieldOrMethod.getAnnotation(TemporalEntity.class);
    JoinTable joinTable = fieldOrMethod.getAnnotation(JoinTable.class);
    Class targetEntity = null;
    Class declaringClass;
    String packageName;
    String hstEntityClassSimpleName = FieldInfo.toCamelCase(joinTable.name()) + HISTORY_ENTITY_SUFFIX;
    hstEntityClassSimpleName = hstEntityClassSimpleName.substring(0, 1).toUpperCase()
            + hstEntityClassSimpleName.substring(1);

    if (fieldOrMethod.isAnnotationPresent(ManyToMany.class)) {
        targetEntity = MappingUtils.determineTargetEntity(fieldOrMethod,
                fieldOrMethod.getAnnotation(ManyToMany.class).targetEntity());
    } else if (fieldOrMethod.isAnnotationPresent(ManyToOne.class)) {
        targetEntity = MappingUtils.determineTargetEntity(fieldOrMethod,
                fieldOrMethod.getAnnotation(ManyToOne.class).targetEntity());
    } else {
        throw new UnsupportedOperationException(
                "when generating history entity for relation on " + fieldOrMethod);
    }

    if (fieldOrMethod instanceof Field) {
        declaringClass = ((Field) fieldOrMethod).getDeclaringClass();
    } else {
        declaringClass = ((Method) fieldOrMethod).getDeclaringClass();
    }
    packageName = declaringClass.getPackage().getName();

    List<FieldInfo> fieldInfos = new ArrayList<FieldInfo>();

    //      add join columns to both sides of the relation (assumes we have only one each)
    fieldInfos.add(FieldInfo.getIdFieldInfo(declaringClass,
            getFieldNameFromReferencingClassName(declaringClass.getSimpleName()),
            joinTable.joinColumns()[0].name()));
    fieldInfos.add(FieldInfo.getIdFieldInfo(targetEntity,
            getFieldNameFromReferencingClassName(targetEntity.getSimpleName()),
            joinTable.inverseJoinColumns()[0].name()));

    return createHistoryEntity(baseClassPackageName, packageName, hstEntityClassSimpleName,
            temporalEntity.historyTableName(), null, null, template, fieldInfos);
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private boolean hasColumnDeclared(Class clazz, String columnName) {
    for (AccessibleObject fieldOrMethod : getFieldsAndMethods(clazz)) {
        if (fieldOrMethod.isAnnotationPresent(Column.class)
                && fieldOrMethod.getAnnotation(Column.class).name().compareToIgnoreCase(columnName) == 0) {
            return true;
        }/*from   ww w.j  a v a  2  s.com*/
        if (fieldOrMethod.isAnnotationPresent(JoinColumn.class)
                && fieldOrMethod.getAnnotation(JoinColumn.class).name().compareToIgnoreCase(columnName) == 0) {
            return true;
        }
    }
    return false;
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private Map<Class, List<AccessibleObject>> getInverseFKs(Collection<Class> baseEntities) {
    Map<Class, List<AccessibleObject>> inverseFKs = new HashMap<Class, List<AccessibleObject>>();

    for (Class baseEntity : baseEntities) {
        Set<AccessibleObject> fieldOrMethods = getFieldsAndMethods(baseEntity);

        for (AccessibleObject fieldOrMethod : fieldOrMethods) {
            if (fieldOrMethod.isAnnotationPresent(OneToMany.class)) {
                OneToMany o2m = fieldOrMethod.getAnnotation(OneToMany.class);
                Class<?> mappedClass = MappingUtils.determineTargetEntity(fieldOrMethod, o2m.targetEntity());

                if (fieldOrMethod.isAnnotationPresent(JoinColumn.class) && !hasColumnDeclared(mappedClass,
                        fieldOrMethod.getAnnotation(JoinColumn.class).name())) {
                    List<AccessibleObject> fks = inverseFKs.get(mappedClass);

                    if (fks == null) {
                        inverseFKs.put(mappedClass, fks = new ArrayList<AccessibleObject>());
                    }/*from ww w .j a  va2s.c o m*/

                    fks.add(fieldOrMethod);
                }
            }
        }
    }

    return inverseFKs;
}

From source file:grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition.java

protected Annotation findSecuredAnnotation(final AccessibleObject annotatedTarget) {
    Annotation annotation = annotatedTarget
            .getAnnotation(grails.plugin.springsecurity.annotation.Secured.class);
    if (annotation != null) {
        return annotation;
    }/*from  ww w . j a va  2  s. c om*/
    return annotatedTarget.getAnnotation(org.springframework.security.access.annotation.Secured.class);
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private Column getUuidColumn(Class entityClass) {
    AccessibleObject idFieldOrMethod = FieldInfo.getIdFieldOrMethod(entityClass);

    if (idFieldOrMethod == null) {
        throw new IllegalStateException(
                "cannot find field or method with @Id for entity class " + entityClass.getName());
    }/*from   w  w w .  j  a v  a 2  s .  c o m*/

    return idFieldOrMethod.getAnnotation(Column.class);
}

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

private void addI18NInfo(AccessibleObject[] fieldsOrMethods) {
    for (AccessibleObject fieldOrMethod : fieldsOrMethods) {
        if (fieldOrMethod.getAnnotation(I18NDetailEntityRelation.class) != null) {
            JoinColumn joinColumn = getJoinColumn(fieldOrMethod);
            String entityTableName, detailTableName;
            if (fieldOrMethod.getAnnotation(OneToMany.class) != null) {
                OneToMany o2m = fieldOrMethod.getAnnotation(OneToMany.class);
                Class<?> targetEntityClass = MappingUtils.determineTargetEntity(fieldOrMethod,
                        o2m.targetEntity());
                detailTableName = getTableAnnotation(targetEntityClass).name();
                entityTableName = getTableName();
            } else {
                ManyToOne m2o = fieldOrMethod.getAnnotation(ManyToOne.class);
                Class<?> targetEntityClass = MappingUtils.determineTargetEntity(fieldOrMethod,
                        m2o.targetEntity());
                entityTableName = getTableAnnotation(targetEntityClass).name();
                detailTableName = getTableName();
            }//from  w w w . j a  va2  s  .  co  m

            TableConfig detailTableConfig = ddlEnhancer
                    .getConfigForTableName(getIdentifierForReference(detailTableName));

            if (joinColumn != null && detailTableConfig != null) {
                detailTableConfig.i18nBaseEntityFkCol = joinColumn.name();
                detailTableConfig.i18nBaseEntityTblName = entityTableName;
            }
        }
    }
}

From source file:com.github.reinert.jjschema.JsonSchemaGenerator.java

protected <T> ObjectNode generatePropertySchema(Class<T> type, Method method, Field field)
        throws TypeException {
    Class<?> returnType = method != null ? method.getReturnType() : field.getType();

    AccessibleObject propertyReflection = field != null ? field : method;

    SchemaIgnore ignoreAnn = propertyReflection.getAnnotation(SchemaIgnore.class);
    if (ignoreAnn != null)
        return null;

    ObjectNode schema = createInstance();

    JsonManagedReference refAnn = propertyReflection.getAnnotation(JsonManagedReference.class);
    if (refAnn != null) {
        ManagedReference fowardReference;
        Class<?> genericClass;
        Class<?> collectionClass;
        if (Collection.class.isAssignableFrom(returnType)) {
            if (method != null) {
                ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
                genericClass = (Class<?>) genericType.getActualTypeArguments()[0];
            } else {
                genericClass = field.getClass();
            }/*from ww w  .ja v  a2s . c  om*/
            collectionClass = returnType;
        } else {
            genericClass = returnType;
        }
        fowardReference = new ManagedReference(type, refAnn.value(), genericClass);

        if (!isFowardReferencePiled(fowardReference)) {
            pushFowardReference(fowardReference);
        } else
        //           if (isBackwardReferencePiled(fowardReference)) 
        {
            boolean a = pullFowardReference(fowardReference);
            boolean b = pullBackwardReference(fowardReference);
            //return null;
            return createRefSchema("#");
        }
    }

    JsonBackReference backRefAnn = propertyReflection.getAnnotation(JsonBackReference.class);
    if (backRefAnn != null) {
        ManagedReference backReference;
        Class<?> genericClass;
        Class<?> collectionClass;
        if (Collection.class.isAssignableFrom(returnType)) {
            ParameterizedType genericType = (ParameterizedType) method.getGenericReturnType();
            genericClass = (Class<?>) genericType.getActualTypeArguments()[0];
            collectionClass = returnType;
        } else {
            genericClass = returnType;
        }
        backReference = new ManagedReference(genericClass, backRefAnn.value(), type);

        if (isFowardReferencePiled(backReference) && !isBackwardReferencePiled(backReference)) {
            pushBackwardReference(backReference);
        } else {
            //              pullFowardReference(backReference);
            //              pullBackwardReference(backReference);
            return null;
        }
    }

    if (Collection.class.isAssignableFrom(returnType)) {
        processPropertyCollection(method, field, schema);
    } else {
        schema = generateSchema(returnType);
    }

    // Check the field annotations, if the get method references a field, or the
    // method annotations on the other hand, and processSchemaProperty them to
    // the JsonSchema object
    Attributes attrs = propertyReflection.getAnnotation(Attributes.class);
    if (attrs != null) {
        processSchemaProperty(schema, attrs);
        // The declaration of $schema is only necessary at the root object
        schema.remove("$schema");
    }

    // Check if the Nullable annotation is present, and if so, add 'null' to type attr
    Nullable nullable = propertyReflection.getAnnotation(Nullable.class);
    if (nullable != null) {
        if (returnType.isEnum()) {
            ((ArrayNode) schema.get("enum")).add("null");
        } else {
            String oldType = schema.get(TAG_TYPE).asText();
            ArrayNode typeArray = schema.putArray(TAG_TYPE);
            typeArray.add(oldType);
            typeArray.add("null");
        }
    }

    return schema;
}

From source file:com.yahoo.elide.core.EntityDictionary.java

/**
 * Return a single annotation from field or accessor method.
 *
 * @param entityClass the entity class/*w  ww . j ava  2s. c  o  m*/
 * @param annotationClass given annotation type
 * @param identifier the identifier
 * @param <A> genericClass
 * @return annotation found
 */
public <A extends Annotation> A getAttributeOrRelationAnnotation(Class<?> entityClass, Class<A> annotationClass,
        String identifier) {
    AccessibleObject fieldOrMethod = entityBinding(entityClass).fieldsToValues.get(identifier);
    if (fieldOrMethod == null) {
        return null;
    }
    return fieldOrMethod.getAnnotation(annotationClass);
}

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

private void processAnnotations(AccessibleObject[] fieldsOrMethods, boolean selectiveRendering) {
    for (AccessibleObject fieldOrMethod : fieldsOrMethods) {
        JoinTable joinTable = fieldOrMethod.getAnnotation(JoinTable.class);
        CollectionTable collectionTable = fieldOrMethod.getAnnotation(CollectionTable.class);
        String joinTableName = joinTable != null ? joinTable.name()
                : collectionTable != null ? collectionTable.name() : null;
        TableConfig joinTableConfig = joinTableName != null
                ? ddlEnhancer.getConfigForTableName(getIdentifierForReference(joinTableName))
                : null;/*from w w  w  .jav  a 2s . c  o m*/

        if (joinTableName != null && joinTableConfig == null) {
            joinTableConfig = new TableConfig(ddlEnhancer, null,
                    ddlEnhancer.getProfile().getNamingStrategy().tableName(joinTableName));
            ddlEnhancer.addTableConfig(joinTableConfig);
        }

        if (joinTableConfig != null) {
            joinTableConfig.putTableAnnotation(joinTable != null ? joinTable : collectionTable);

            if (fieldOrMethod.isAnnotationPresent(IntervalPartitioning.class)) {
                joinTableConfig.putTableAnnotation(fieldOrMethod.getAnnotation(IntervalPartitioning.class));
            }
            if (fieldOrMethod.isAnnotationPresent(Auditable.class)) {
                joinTableConfig.putTableAnnotation(fieldOrMethod.getAnnotation(Auditable.class));
            }
        }

        if (fieldOrMethod.getAnnotation(Profile.class) != null) {
            if (joinTableConfig == null) {
                throw new IllegalArgumentException("need @" + JoinTable.class.getSimpleName() + " or @"
                        + CollectionTable.class.getSimpleName() + " for @" + Profile.class.getSimpleName()
                        + " on a field");
            }
            Profile annotation = fieldOrMethod.getAnnotation(Profile.class);

            if (selectiveRendering
                    && !Arrays.asList(annotation.value()).contains(ddlEnhancer.getProfile().getName())) {
                joinTableConfig.setTableToBeRendered(false);
            }
        }
        if (fieldOrMethod.getAnnotation(TemporalEntity.class) != null) {
            if (joinTableConfig == null) {
                throw new IllegalArgumentException("need @" + JoinTable.class.getSimpleName() + " or @"
                        + CollectionTable.class.getSimpleName() + " for @"
                        + TemporalEntity.class.getSimpleName() + " on a field");
            }
            TemporalEntity annotation = fieldOrMethod.getAnnotation(TemporalEntity.class);

            if (annotation != null) {
                joinTableConfig.putTableAnnotation(annotation);
            }
        }
        if (fieldOrMethod.getAnnotation(Sequence.class) != null) {
            sequences.add(fieldOrMethod.getAnnotation(Sequence.class));
        } else if (fieldOrMethod.getAnnotation(Embedded.class) != null) {
            Class embed;
            if (fieldOrMethod instanceof Field) {
                Field field = (Field) fieldOrMethod;
                embed = field.getType();
            } else if (fieldOrMethod instanceof Method) {
                Method method = (Method) fieldOrMethod;
                embed = method.getReturnType();
            } else {
                throw new IllegalStateException("AccessibleObject not type of Field or Method");
            }
            processTypeAnnotations(embed, selectiveRendering);
        }
        //            if we have a ManyToMany JoinTable and current entity is part of selective rendering,
        //            we also need to render the JoinTable
        else if (fieldOrMethod.isAnnotationPresent(ManyToMany.class)) {
            if (joinTable == null) {
                String propName = fieldOrMethod instanceof Field ? ((Field) fieldOrMethod).getName()
                        : ((Method) fieldOrMethod).getName().substring(3);
                joinTableConfig = new TableConfig(ddlEnhancer, null, ddlEnhancer.getProfile()
                        .getNamingStrategy().collectionTableName(null, getTableName(), null, null, propName));
                ddlEnhancer.addTableConfig(joinTableConfig);
            }
            joinTableConfig.setTableToBeRendered(true);
        }

        if (fieldOrMethod.isAnnotationPresent(Default.class)) {
            String defaultValue = fieldOrMethod.getAnnotation(Default.class).sqlExpression();
            if (fieldOrMethod.isAnnotationPresent(Column.class)) {
                colNameToDefault.put(
                        getIdentifierForReference(fieldOrMethod.getAnnotation(Column.class).name()),
                        defaultValue);
            } else if (fieldOrMethod instanceof Field) {
                colNameToDefault.put(getIdentifierForReference(ddlEnhancer.getProfile().getNamingStrategy()
                        .columnName(((Field) fieldOrMethod).getName())), defaultValue);
            } else {
                LOG.warn(Default.class + " only supported on fields or @{} annotated methods",
                        Column.class.toString());
            }
        }
    }
}