Example usage for java.lang.reflect AccessibleObject isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.google.code.siren4j.util.ReflectionUtils.java

/**
 * Determine if the field or method is ignored because the <code>SirenPropertyIgnore</code> annotation is present.
 *
 * @param obj/*from   w  w w.j a  v  a 2  s .c  o  m*/
 * @return
 */
public static boolean isIgnored(AccessibleObject obj) {
    return obj.isAnnotationPresent(Siren4JPropertyIgnore.class);
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static Class determineTargetEntity(AccessibleObject ao) {
    Class specifiedTargetEntity = null;
    if (ao.isAnnotationPresent(ManyToOne.class)) {
        specifiedTargetEntity = ao.getAnnotation(ManyToOne.class).targetEntity();
    } else if (ao.isAnnotationPresent(ManyToMany.class)) {
        specifiedTargetEntity = ao.getAnnotation(ManyToMany.class).targetEntity();
    } else if (ao.isAnnotationPresent(OneToOne.class)) {
        specifiedTargetEntity = ao.getAnnotation(OneToOne.class).targetEntity();
    }/*w  ww.j  av  a2s .c  o  m*/
    return determineTargetEntity(ao, specifiedTargetEntity);
}

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

private static void traverseFieldsAndMethods(Class type, boolean fields, boolean methods,
        GatherFieldOrMethodInfoAction action) {
    List<AccessibleObject> fOms = new ArrayList<AccessibleObject>();
    if (fields) {
        fOms.addAll(Arrays.asList(type.getDeclaredFields()));
    }/*from   ww w  .j  a  va2s. c  om*/
    if (methods) {
        fOms.addAll(Arrays.asList(type.getDeclaredMethods()));
    }

    for (AccessibleObject fOm : fOms) {
        if (fOm.isAnnotationPresent(Transient.class)) {
            continue;
        }

        action.gatherInfo(fOm);
    }
}

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

private static JoinColumn getJoinColumn(AccessibleObject fieldOrMethod) {
    if (fieldOrMethod.isAnnotationPresent(JoinColumn.class)) {
        return fieldOrMethod.getAnnotation(JoinColumn.class);
    }/*w ww.j a va 2  s  . c om*/
    if (fieldOrMethod.isAnnotationPresent(OneToMany.class)) {
        OneToMany o2m = fieldOrMethod.getAnnotation(OneToMany.class);
        try {
            if (fieldOrMethod.getAnnotation(JoinColumn.class) != null) {
                return fieldOrMethod.getAnnotation(JoinColumn.class);
            } else if (fieldOrMethod.isAnnotationPresent(JoinColumns.class)) {
                return null; // TODO: implement compound FK
            } else {
                Class<?> targetEntityClass = MappingUtils.determineTargetEntity(fieldOrMethod,
                        o2m.targetEntity());
                return targetEntityClass.getDeclaredField(o2m.mappedBy()).getAnnotation(JoinColumn.class);
            }
        } catch (NoSuchFieldException e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

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

private static void addAttributeOverrides(Map<String, String> attr2colName, String attrPath,
        AccessibleObject fom) {
    List<AttributeOverride> overrides = new ArrayList<AttributeOverride>();
    if (fom.isAnnotationPresent(AttributeOverride.class)) {
        overrides.add(fom.getAnnotation(AttributeOverride.class));
    }//from  w  w  w  .  j  a va 2 s  .com

    if (fom.isAnnotationPresent(AttributeOverrides.class)) {
        overrides.addAll(Arrays.asList(fom.getAnnotation(AttributeOverrides.class).value()));
    }

    for (AttributeOverride override : overrides) {
        String attr = attrPath + "." + override.name();

        if (!attr2colName.containsKey(attr)) {
            attr2colName.put(attr, getIdentifierForReference(override.column().name()));
        }
    }
}

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

private <A extends Annotation> void bindTrigger(Class<A> annotationClass, AccessibleObject fieldOrMethod) {
    if (fieldOrMethod instanceof Method && fieldOrMethod.isAnnotationPresent(annotationClass)) {
        A onTrigger = fieldOrMethod.getAnnotation(annotationClass);
        String value;/*from  w  ww .j ava  2  s.  co  m*/
        try {
            value = (String) annotationClass.getMethod("value").invoke(onTrigger);
        } catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) {
            value = "";
        }
        fieldsToTriggers.put(Pair.of(annotationClass, value), fieldOrMethod);
    }
}

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

/**
 * Bind an attribute or relationship.//w  w w  .j  a va  2  s  .  c  om
 *
 * @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: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;
        }//w  w  w. ja  v a  2 s .c  o m
        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>());
                    }// w  w w.j  a  va 2  s .c  om

                    fks.add(fieldOrMethod);
                }
            }
        }
    }

    return inverseFKs;
}

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

public void processBaseEntityClasses(String baseClassPackageName, Collection<Class> baseEntities) {
    Map<Class, List<AccessibleObject>> inverseFKs = getInverseFKs(baseEntities);

    for (Class baseEntity : baseEntities) {
        Class tableEntity = getTableEntityClass(baseEntity);
        if (baseEntity.isAnnotationPresent(MappedSuperclass.class)
                || (tableEntity != null && tableEntity.isAnnotationPresent(TemporalEntity.class))) {

            String className = createHistoryEntity(baseClassPackageName, baseEntity, new StringReader(template),
                    inverseFKs.get(baseEntity));

            if (baseEntity.isAnnotationPresent(MappedSuperclass.class)) {
                createdMappedSuperClasses.add(className);
            } else {
                createdEntityClasses.add(className);
            }/*w w  w  .j a  v a2 s.  c om*/
        }

        for (AccessibleObject accessibleObject : getFieldsAndMethods(baseEntity)) {
            if (accessibleObject.isAnnotationPresent(TemporalEntity.class)
                    && accessibleObject.isAnnotationPresent(JoinTable.class)) {
                String className = createHistoryEntity(baseClassPackageName, accessibleObject,
                        new StringReader(template));
                createdEntityClasses.add(className);
            }
        }
    }
}