Example usage for java.lang.reflect Field getType

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

Introduction

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

Prototype

public Class<?> getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the field represented by this Field object.

Usage

From source file:com.gisgraphy.webapp.taglib.ConstantsTei.java

/**
 * Return information about the scripting variables to be created.
 * /*from   w w  w.j  a  v a  2s .c  o  m*/
 * @param data
 *                the input data
 * @return VariableInfo array of variable information
 */
@Override
public VariableInfo[] getVariableInfo(TagData data) {
    // loop through and expose all attributes
    List<VariableInfo> vars = new ArrayList<VariableInfo>();

    try {
        String clazz = data.getAttributeString("className");

        if (clazz == null) {
            clazz = Constants.class.getName();
        }

        Class<?> c = Class.forName(clazz);

        // if no var specified, get all
        if (data.getAttributeString("var") == null) {
            Field[] fields = c.getDeclaredFields();

            AccessibleObject.setAccessible(fields, true);

            for (Field field : fields) {
                String type = field.getType().getName();
                vars.add(new VariableInfo(field.getName(),
                        ((field.getType().isArray()) ? type.substring(2, type.length() - 1) + "[]" : type),
                        true, VariableInfo.AT_END));
            }
        } else {
            String var = data.getAttributeString("var");
            String type = c.getField(var).getType().getName();
            vars.add(new VariableInfo(c.getField(var).getName(),
                    ((c.getField(var).getType().isArray()) ? type.substring(2, type.length() - 1) + "[]"
                            : type),
                    true, VariableInfo.AT_END));
        }
    } catch (Exception cnf) {
        log.error(cnf.getMessage());
    }

    return vars.toArray(new VariableInfo[] {});
}

From source file:springfox.documentation.swagger.readers.parameter.SwaggerExpandedParameterBuilder.java

private AllowableValues allowableValues(final Optional<String> optionalAllowable, final Field field) {

    AllowableValues allowable = null;//w w  w .  j a  v a  2  s  . c  om
    if (field.getType().isEnum()) {
        allowable = new AllowableListValues(getEnumValues(field.getType()), "LIST");
    } else if (optionalAllowable.isPresent()) {
        allowable = ApiModelProperties.allowableValueFromString(optionalAllowable.get());
    }

    return allowable;
}

From source file:net.cpollet.jixture.helper.MappingBuilder.java

private Object convertValueToFieldType(Object value, Field field) {
    return conversionService.convert(value, field.getType());
}

From source file:com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java

@Override
public boolean applies(Field field) {
    final Class<?> type = field.getType();
    return type == List.class;
}

From source file:com.erudika.para.core.ParaObjectUtils.java

/**
 * Converts a map of fields/values to a domain object. Only annotated fields are populated. This method forms the
 * basis of an Object/Grid Mapper.//from  w w  w. j av  a 2  s.c  o m
 * <br>
 * Map values that are JSON objects are converted to their corresponding Java types. Nulls and primitive types are
 * preserved.
 *
 * @param <P> the object type
 * @param pojo the object to populate with data
 * @param data the map of fields/values
 * @param filter a filter annotation. fields that have it will be skipped
 * @return the populated object
 */
public static <P extends ParaObject> P setAnnotatedFields(P pojo, Map<String, Object> data,
        Class<? extends Annotation> filter) {
    if (data == null || data.isEmpty()) {
        return null;
    }
    try {
        if (pojo == null) {
            // try to find a declared class in the core package
            pojo = (P) toClass((String) data.get(Config._TYPE)).getConstructor().newInstance();
        }
        List<Field> fields = getAllDeclaredFields(pojo.getClass());
        Map<String, Object> props = new HashMap<String, Object>(data);
        for (Field field : fields) {
            boolean dontSkip = ((filter == null) ? true : !field.isAnnotationPresent(filter));
            String name = field.getName();
            Object value = data.get(name);
            if (field.isAnnotationPresent(Stored.class) && dontSkip) {
                // try to read a default value from the bean if any
                if (value == null && PropertyUtils.isReadable(pojo, name)) {
                    value = PropertyUtils.getProperty(pojo, name);
                }
                // handle complex JSON objects deserialized to Maps, Arrays, etc.
                if (!Utils.isBasicType(field.getType()) && value instanceof String) {
                    // in this case the object is a flattened JSON string coming from the DB
                    value = getJsonReader(field.getType()).readValue(value.toString());
                }
                field.setAccessible(true);
                BeanUtils.setProperty(pojo, name, value);
            }
            props.remove(name);
        }
        // handle unknown (user-defined) fields
        if (!props.isEmpty() && pojo instanceof Sysprop) {
            for (Map.Entry<String, Object> entry : props.entrySet()) {
                String name = entry.getKey();
                Object value = entry.getValue();
                // handle the case where we have custom user-defined properties
                // which are not defined as Java class fields
                if (!PropertyUtils.isReadable(pojo, name)) {
                    if (value == null) {
                        ((Sysprop) pojo).removeProperty(name);
                    } else {
                        ((Sysprop) pojo).addProperty(name, value);
                    }
                }
            }
        }
    } catch (Exception ex) {
        logger.error(null, ex);
        pojo = null;
    }
    return pojo;
}

From source file:com.gemstone.gemfire.test.junit.rules.serializable.SerializableTimeoutTest.java

@Test
public void fieldLookForStuckThreadShouldExist() throws Exception {
    Field field = Timeout.class.getDeclaredField(FIELD_LOOK_FOR_STUCK_THREAD);
    assertThat(field.getType()).isEqualTo(Boolean.TYPE);
}

From source file:mil.army.usace.data.dataquery.rdbms.implementations.OracleConverter.java

@Override
public Object convertType(Object obj, Field field)
        throws ClassNotFoundException, ConversionException, SQLException {
    Class fieldParam = field.getType();
    if (obj == null)
        return null;
    else if (obj instanceof oracle.sql.TIMESTAMPTZ) {
        java.sql.Date sqlDate = ((oracle.sql.TIMESTAMPTZ) obj).dateValue(oc);
        return ConversionUtility.convertType(sqlDate, fieldParam);
    } else {//  ww  w.java2s  .c om
        if (fieldParam.isPrimitive()) {
            return obj;
        } else {
            return ConversionUtility.convertType(obj, fieldParam);
        }
    }
}

From source file:alpha.portal.webapp.taglib.ConstantsTei.java

/**
 * Return information about the scripting variables to be created.
 * /*w  ww .ja v a2  s.c o  m*/
 * @param data
 *            the input data
 * @return VariableInfo array of variable information
 */
@Override
public VariableInfo[] getVariableInfo(final TagData data) {
    // loop through and expose all attributes
    final List<VariableInfo> vars = new ArrayList<VariableInfo>();

    try {
        String clazz = data.getAttributeString("className");

        if (clazz == null) {
            clazz = Constants.class.getName();
        }

        final Class c = Class.forName(clazz);

        // if no var specified, get all
        if (data.getAttributeString("var") == null) {
            final Field[] fields = c.getDeclaredFields();

            AccessibleObject.setAccessible(fields, true);

            for (final Field field : fields) {
                final String type = field.getType().getName();
                vars.add(new VariableInfo(field.getName(),
                        ((field.getType().isArray()) ? type.substring(2, type.length() - 1) + "[]" : type),
                        true, VariableInfo.AT_END));
            }
        } else {
            final String var = data.getAttributeString("var");
            final String type = c.getField(var).getType().getName();
            vars.add(new VariableInfo(c.getField(var).getName(),
                    ((c.getField(var).getType().isArray()) ? type.substring(2, type.length() - 1) + "[]"
                            : type),
                    true, VariableInfo.AT_END));
        }
    } catch (final Exception cnf) {
        this.log.error(cnf.getMessage());
        cnf.printStackTrace();
    }

    return vars.toArray(new VariableInfo[] {});
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.java

/**
* Returns the id property class defined or annotated in the entity class
* @param   clazz   Target entity class/* ww w  . j  a v a 2  s.  com*/
* @return          Property class
*/
protected static <F> Class<?> getIdPropertyClass(Class<F> clazz) {
    // Search for id properties annotations, regardless case
    for (Field field : clazz.getDeclaredFields()) {
        SerializedName serializedName = field.getAnnotation(SerializedName.class);

        if (serializedName != null) {
            if (IdProperties.contains(serializedName.value())) {
                return field.getType();
            }
        } else {
            if (IdProperties.contains(field.getName())) {
                return field.getType();
            }
        }
    }

    return null;
}

From source file:eu.crisis_economics.abm.model.ModelUtils.java

private static ConfigurationModifier findGetterSetterMethods(final Field field, final Object classInstance,
        final String parameterID) throws SecurityException, NoSuchMethodException {
    final Class<?> type = classInstance.getClass();
    final String fieldName = field.getName(), expectedGetterName = "get" + WordUtils.capitalize(fieldName),
            expectedSetterName = "set" + WordUtils.capitalize(fieldName);
    final Method getterMethod = type.getMethod(expectedGetterName),
            setterMethod = type.getMethod(expectedSetterName, field.getType());
    return new ConfigurationModifier(field, getterMethod, setterMethod, classInstance, parameterID);
}