Example usage for java.lang.reflect Field getAnnotation

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:com.thesett.util.validation.core.JsonSchemaConstraintMapping.java

/**
 * Adds constraints in a json schema to the mapping, against the specified class.
 *
 * @param type       The class to add constraints to.
 * @param jsonSchema The json schema to add constraints from.
 * @param <C>        The type of the class to add constraints to.
 */// ww w  .j a va 2  s  .co m
public <C> void addSchema(Class<C> type, JsonSchema jsonSchema) {
    TypeConstraintMappingContext<C> typeContext = type(type);

    // Scan all of the fields of the class being added to, to check if there are any @JsonProperty mappings,
    // translating field names between Java and JSON.
    Map<String, String> jsonToJava = new HashMap<>();

    for (Field field : type.getDeclaredFields()) {
        JsonProperty annotation = field.getAnnotation(JsonProperty.class);

        if (annotation != null) {
            jsonToJava.put(annotation.value(), field.getName());
        }
    }

    if (jsonSchema.getProperties() != null) {
        for (Map.Entry<String, JsonSchema> property : jsonSchema.getProperties().entrySet()) {
            String jsonPropertyName = property.getKey();
            String javaPropertyName;

            if (jsonToJava.containsKey(jsonPropertyName)) {
                javaPropertyName = jsonToJava.get(jsonPropertyName);
            } else {
                javaPropertyName = jsonPropertyName;
            }

            PropertyConstraintMappingContext propertyContext = typeContext.property(javaPropertyName,
                    ElementType.FIELD);

            addConstraints(propertyContext, javaPropertyName, jsonPropertyName, property.getValue(), type);

            convertRequired(propertyContext, javaPropertyName, jsonPropertyName, jsonSchema);
        }
    }
}

From source file:br.com.bea.androidtools.api.json.JSONContextImpl.java

@Override
public JSONObject marshal(final E vo) {
    final JSONObject object = new JSONObject();
    try {/*from  w  ww .  j ava 2 s. co  m*/
        for (final MetadataObject mdo : metadata) {
            final Field field = targetClass.getDeclaredField(mdo.getField().getName());
            field.setAccessible(true);
            final Object value = field.get(vo);
            object.put(field.getAnnotation(Metadata.class).value(), null == value ? JSONObject.NULL : value);
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return object;
}

From source file:org.querybyexample.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnFields(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Field field : entityClass.getFields()) {
        Column column = field.getAnnotation(Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            values.put(field.getName(), getValueFromField(field, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, field.getName()));
            }/*from w ww.jav a2s .com*/
        }
    }
    return errors;
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private String getLowercaseColumnNameFromMapping(Field field) {
    Column column = field.getAnnotation(Column.class);

    if (null == column) {
        column = getColumnFromGetter(field);
    }//  w  ww  .  ja  v a2  s .  co  m

    if (null == column) {
        return null;
    }

    if (null == column.name() || "".equals(column.name())) {
        return field.getName(); // FIXME this one may be wrong actually...
    }

    return column.name().toLowerCase();
}

From source file:org.xaloon.wicket.component.inject.spring.CdiProxyFieldValueFactory.java

private String getBeanName(final Field field) {
    String name = null;//from  www.  jav a2  s .c  o  m
    Named annotation = field.getAnnotation(Named.class);
    if (annotation != null && !StringUtils.isEmpty(annotation.value())) {
        name = annotation.value();
    }
    if (StringUtils.isEmpty(name)) {
        name = beanNameCache.get(field.getType());
    }
    if (StringUtils.isEmpty(name)) {
        name = resolveBeanName(field);
        if (StringUtils.isEmpty(name)) {
            name = getBeanNameOfClass(contextLocator.getSpringContext(), field.getType());
        }
        if (!StringUtils.isEmpty(name)) {
            beanNameCache.put(field.getType(), name);
        } else {
            beanNameCache.put(field.getType(), NULL);
        }
    } else if (NULL.equals(name)) {
        return null;
    }
    return name;
}

From source file:com.dbs.sdwt.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnFields(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Field field : entityClass.getFields()) {
        Column column = field.getAnnotation(Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            values.put(field.getName(), jpaUtil.getValueFromField(field, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, field.getName()));
            }/*  w ww .j a v a  2s. c  o  m*/
        }
    }
    return errors;
}

From source file:net.eledge.android.toolkit.db.internal.TableBuilder.java

private String createFieldDef(Class<?> clazz, Field field) {
    FieldType type = FieldType.getType(field.getType());
    Column column = field.getAnnotation(Column.class);
    if (StringUtils.isNotEmpty(column.columnDefinition())) {
        return column.columnDefinition();
    }//  w w  w  .ja  v  a2s.c  o m
    StringBuilder sb = new StringBuilder();
    sb.append(SQLBuilder.getFieldName(field, column));
    sb.append(" ").append(type.columnType);
    if (column.unique()) {
        sb.append(" UNIQUE");
    }
    try {
        if (!column.nullable()) {
            sb.append(" NOT NULL");
            sb.append(" DEFAULT ").append(type.defaultValue(clazz.newInstance(), field));
        }
    } catch (Exception e) {
        Log.e(this.getClass().getName(), e.getMessage(), e);
    }
    if (field.isAnnotationPresent(Id.class)) {
        sb.append(" PRIMARY KEY");
    }
    return sb.toString();
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private void createColumnToEmbeddedFieldMapping(String tableName, Field field) {
    EmbeddedId embeddedId = field.getAnnotation(EmbeddedId.class);

    if (null != embeddedId) {
        for (Field embeddedField : field.getType().getDeclaredFields()) {
            String embeddedColumnName = getLowercaseColumnNameFromMapping(embeddedField);
            getOrCreateFieldsByTableName(tableName).put(embeddedColumnName,
                    new MappingField(field, embeddedField));
        }//from   ww w  .  j  av  a 2  s . com
    }
}

From source file:com.jedi.oracle.OracleCall.java

private void fillFieldValuesFromParameters() throws IllegalAccessException {
    List<Field> fields = FieldUtils.getFieldsListWithAnnotation(getClass(), OracleParameterMapping.class);
    if (fields == null || fields.isEmpty()) {
        return;//w w  w. j a v a 2  s .c  o  m
    }

    for (final OracleParameter parameter : this.parameters) {
        switch (parameter.getDirection()) {
        case ReturnValue:
        case InputOutput:
        case Output:
            Field field = Iterables.find(fields, new Predicate<Field>() {
                public boolean apply(Field item) {
                    OracleParameterMapping mapping = item.getAnnotation(OracleParameterMapping.class);
                    return mapping.name().equals(parameter.getName());
                }
            });

            field.setAccessible(true);
            field.set(this, parameter.getValue());
        }
    }
}

From source file:com.webpagebytes.cms.engine.JSONToFromObjectConverter.java

public org.json.JSONObject JSONFromObject(Object object) {
    org.json.JSONObject json = new org.json.JSONObject();
    if (null == object)
        return json;
    Class<? extends Object> objClass = object.getClass();
    Field[] fields = objClass.getDeclaredFields();
    for (Field field : fields) {
        Object storeAdn = field.getAnnotation(WPBAdminFieldStore.class);
        if (storeAdn == null) {
            storeAdn = field.getAnnotation(WPBAdminFieldKey.class);
            if (storeAdn == null) {
                storeAdn = field.getAnnotation(WPBAdminFieldTextStore.class);
                if (storeAdn == null) {
                    storeAdn = field.getAnnotation(WPBAdminField.class);
                }//from  w w w .  j a  va2s . com
            }
        }

        if (storeAdn != null) {
            String fieldName = field.getName();
            try {
                PropertyDescriptor pd = new PropertyDescriptor(fieldName, objClass);
                Object value = pd.getReadMethod().invoke(object);
                String fieldValue = JSONStringFromField(value, field.getType());
                if (fieldValue != null) {
                    json.put(fieldName, fieldValue);
                }
            } catch (Exception e) {
                // do nothing, there is no write method for our field
            }
        }
    }
    return json;
}