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.util.FieldInfo.java

public static FieldInfo getIdFieldInfo(Class type, String namePrefix, String columnName) {
    AccessibleObject id = getIdFieldOrMethod(type);
    Column column = id.getAnnotation(Column.class);
    String suffix;/*from w w w  .j a  v  a  2  s. co m*/
    if (id instanceof Field) {
        type = ((Field) id).getType();
        suffix = ((Field) id).getName().substring(0, 1).toUpperCase() + ((Field) id).getName().substring(1);
    } else {
        type = ((Method) id).getReturnType();
        suffix = ((Method) id).getName().replace("get", "").replace("set", "");
    }
    String name = namePrefix + suffix;
    return new FieldInfo(type, name, columnName, column.length());
}

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

public static List<FieldInfo> convertInverseFKs(List<AccessibleObject> inverseFKfieldsOrMethods) {
    List<FieldInfo> fields = new ArrayList<FieldInfo>();
    for (AccessibleObject inverseFK : inverseFKfieldsOrMethods) {
        String joinColName = inverseFK.getAnnotation(JoinColumn.class).name();
        fields.add(new FieldInfo(String.class, toCamelCase("INV_FK_" + joinColName), joinColName,
                Constants.UUID_LEN));
    }// w w w  .  j a v a2  s .c o m
    return fields;
}

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();
    }/*from   ww w  .  j  a  v a  2 s  . com*/
    return determineTargetEntity(ao, specifiedTargetEntity);
}

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 w w  .  j a v  a 2s.  c  o  m*/
    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:de.micromata.genome.jpa.metainf.MetaInfoUtils.java

/**
 * Gets the column meta data./*  w ww .  j av  a 2s .  c  om*/
 *
 * @param entity the entity
 * @param entityClass the entity class
 * @param at the at
 * @param pdo the pdo
 * @return the column meta data
 */
public static ColumnMetadata getColumnMetaData(EntityMetadataBean entity, Class<?> entityClass,
        Attribute<?, ?> at, Optional<PropertyDescriptor> pdo) {
    ColumnMetadataBean ret = new ColumnMetadataBean(entity);
    ret.setName(at.getName());
    ret.setJavaType(at.getJavaType());
    ret.setAssociation(at.isAssociation());
    ret.setCollection(at.isCollection());

    if (at instanceof PluralAttribute) {
        PluralAttribute pa = (PluralAttribute) at;
        Type eltype = pa.getElementType();
        CollectionType coltype = pa.getCollectionType();
    }

    Member jm = at.getJavaMember();
    // TODO maybe handle this.
    at.getPersistentAttributeType();
    if ((jm instanceof AccessibleObject) == false) {
        LOG.warn("Column " + at.getName() + " ha no valid Java Member");
        return ret;
    }
    AccessibleObject ao = (AccessibleObject) jm;
    getGetterSetter(entityClass, ao, pdo, ret);
    ret.setAnnotations(getFieldAndMemberAnnots(entityClass, ao));
    if (ret.getAnnotations().stream().filter((anot) -> anot.getClass() == Transient.class).count() > 0) {
        return null;
    }
    Column colc = ao.getAnnotation(Column.class);
    if (colc == null) {
        ret.setMaxLength(255);
        return ret;
    }
    String name = colc.name();
    if (StringUtils.isEmpty(name) == true) {
        ret.setDatabaseName(ret.getName());
    } else {
        ret.setDatabaseName(name);
    }
    ret.setMaxLength(colc.length());
    ret.setUnique(colc.unique());
    ret.setColumnDefinition(colc.columnDefinition());
    ret.setInsertable(colc.insertable());
    ret.setNullable(colc.nullable());
    ret.setPrecision(colc.precision());
    ret.setScale(colc.scale());
    return ret;
}

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 ww .j  a  v a2 s .c o m*/

    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.github.tddts.jet.config.spring.postprocessor.MessageAnnotationBeanPostProcessor.java

private String preprocess(AccessibleObject accessibleObject) throws NoSuchMessageException {
    Message messageAnnotation = accessibleObject.getAnnotation(Message.class);
    String messageKey = messageAnnotation.value();
    String message = messageSource.getMessage(messageKey, EMPTY_ARGS, Locale.getDefault());
    accessibleObject.setAccessible(true);
    return message;
}

From source file:com.panet.imeta.core.config.KettleConfig.java

private <E extends AccessibleObject> void inject(E[] elems, TempConfig cfg, ConfigManager<?> parms)
        throws IllegalAccessException, InvocationTargetException {
    for (AccessibleObject elem : elems) {
        Inject inj = elem.getAnnotation(Inject.class);
        if (inj != null) {
            // try to inject property from map.
            elem.setAccessible(true);//from   ww  w  .j  a  v a  2  s.  com
            String property = inj.property();
            // Can't think of any other way
            if (elem instanceof Method) {
                Method meth = (Method) elem;
                // find out what we are going to inject 1st
                property = property.equals("") ? Introspector.decapitalize(meth.getName().substring(3))
                        : property;
                meth.invoke(parms, cfg.parms.get(property));

            } else if (elem instanceof Field) {
                Field field = (Field) elem;
                field.set(parms, cfg.parms.get(property.equals("") ? field.getName() : property));
            }
        }
    }
}

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  www.j  a  v  a  2s.  c  om
        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.github.reinert.jjschema.v1.PropertyWrapper.java

protected void processAttributes(ObjectNode node, AccessibleObject accessibleObject) {
    final Attributes attributes = accessibleObject.getAnnotation(Attributes.class);
    if (attributes != null) {
        //node.put("$schema", SchemaVersion.DRAFTV4.getLocation().toString());
        node.remove("$schema");
        if (!attributes.id().isEmpty()) {
            node.put("id", attributes.id());
        }//from  w  w  w  .j  a v a2s .  c  o m
        if (!attributes.description().isEmpty()) {
            node.put("description", attributes.description());
        }
        if (!attributes.pattern().isEmpty()) {
            node.put("pattern", attributes.pattern());
        }
        if (!attributes.title().isEmpty()) {
            node.put("title", attributes.title());
        }
        if (attributes.maximum() > -1) {
            node.put("maximum", attributes.maximum());
        }
        if (attributes.exclusiveMaximum()) {
            node.put("exclusiveMaximum", true);
        }
        if (attributes.minimum() > -1) {
            node.put("minimum", attributes.minimum());
        }
        if (attributes.exclusiveMinimum()) {
            node.put("exclusiveMinimum", true);
        }
        if (attributes.enums().length > 0) {
            ArrayNode enumArray = node.putArray("enum");
            String[] enums = attributes.enums();
            for (String v : enums) {
                enumArray.add(v);
            }
        }
        if (attributes.uniqueItems()) {
            node.put("uniqueItems", true);
        }
        if (attributes.minItems() > 0) {
            node.put("minItems", attributes.minItems());
        }
        if (attributes.maxItems() > -1) {
            node.put("maxItems", attributes.maxItems());
        }
        if (attributes.multipleOf() > 0) {
            node.put("multipleOf", attributes.multipleOf());
        }
        if (attributes.minLength() > 0) {
            node.put("minLength", attributes.minLength());
        }
        if (attributes.maxLength() > -1) {
            node.put("maxLength", attributes.maxLength());
        }
        if (attributes.required()) {
            setRequired(true);
        }
        if (attributes.readonly()) {
            node.put("readonly", true);
        }
    }
}