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:it.unibas.spicy.persistence.object.ClassUtility.java

public static boolean isAReferenceCollection(Field field) {
    Class type = field.getType();
    return findInterface(type, java.util.Collection.class);
}

From source file:ch.algotrader.util.FieldUtil.java

/**
 * returns true if the {@code field} is a primitive, a primitive wrapper, String, BigDecimal, Date or Class.
 *//*from   w  ww  .j  a va 2s . c  o  m*/
public static boolean isSimpleAttribute(Field field) {
    return immediates.contains(field.getType()) || Enum.class.isAssignableFrom(field.getType());
}

From source file:io.apiman.plugins.keycloak_oauth_policy.ClaimLookup.java

private static boolean hasJsonPropertyAnnotation(Field f) {
    for (Field g : f.getType().getDeclaredFields()) {
        g.setAccessible(true);//  w ww. j a  va2  s  .c o  m
        if (g.getAnnotation(JsonProperty.class) != null)
            return true;
    }
    return false;
}

From source file:br.com.bea.androidtools.api.model.EntityUtils.java

public static final Object convert(final Field field, final JSONObject object) throws Exception {
    if (field.getType().equals(String.class))
        return object.getString(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Integer.class))
        return object.getInt(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Long.class))
        return object.getLong(field.getAnnotation(Metadata.class).value());
    if (field.getType().equals(Date.class))
        return DATETIME_FORMAT.parse(object.getString(field.getAnnotation(Metadata.class).value()));
    return object.get(field.getAnnotation(Metadata.class).value());
}

From source file:Main.java

/**
 * @param type/* w ww.  j  av a2  s.  c o m*/
 * @param object
 * @param field
 * @param value
 */
public static <T> void setFieldValue(Class<? extends T> type, T object, Field field, Object value) {
    try {
        Method m = type.getMethod("set" + getFirstLetterUppercased(field.getName()), field.getType());
        m.invoke(object, value);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static ContentValues objectToContentValues(Object object) {
    if (object == null) {
        throw new IllegalArgumentException("please check your argument");
    }/*from  w w  w .  j a va 2  s .c  o m*/
    ContentValues contentValues = new ContentValues();
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            Type type = field.getType();
            field.setAccessible(true);
            if (type.equals(String.class)) {
                contentValues.put(fieldName, field.get(object).toString());
            } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
                contentValues.put(fieldName, field.getInt(object));
            } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
                contentValues.put(fieldName, field.getFloat(object));
            } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
                contentValues.put(fieldName, field.getLong(object));
            } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
                contentValues.put(fieldName, field.getDouble(object));
            } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
                contentValues.put(fieldName, field.getBoolean(object));
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        e.printStackTrace();
    }
    return contentValues;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Method getSetMethod(Class objectClass, String fieldName) {

    try {/*from  ww  w  .  j a  v  a  2  s . c o  m*/

        Class[] parameterTypes = new Class[1];

        Field field = objectClass.getDeclaredField(fieldName);

        parameterTypes[0] = field.getType();

        StringBuffer sb = new StringBuffer();

        sb.append("set");

        sb.append(fieldName.substring(0, 1).toUpperCase());

        sb.append(fieldName.substring(1));

        Method method = objectClass.getMethod(sb.toString(), parameterTypes);

        return method;

    } catch (Exception e) {

        e.printStackTrace();

    }

    return null;

}

From source file:com.ms.commons.lang.ObjectUtils.java

/**
 * string?trim?//from  w  w w .  j a va2 s.  c  o  m
 * 
 * @param object
 * @throws Exception
 */
private static void trimStringField(Object object, Class<?> clazz) throws Exception {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (field.getType() == String.class) {
            boolean isFoolback = false;
            if (field.isAccessible() == false) {
                isFoolback = true;
                field.setAccessible(true);
            }
            String value = (String) field.get(object);
            if (StringUtils.isNotEmpty(value)) {
                value = value.trim();
                field.set(object, value);
            }
            if (isFoolback) {
                field.setAccessible(false);
            }
        }
    }
}

From source file:ShowClass.java

/** Print the modifiers, type, and name of a field */
public static void print_field(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typename(f.getType()) + " " + f.getName() + ";");
}

From source file:Main.java

private static Method getColumnGetMethod(Class<?> entityType, Field field, String suffix) {
    String fieldName = field.getName();
    Method getMethod = null;//  w ww  .j ava2s  .c om
    if (field.getType() == boolean.class) {
        getMethod = getBooleanColumnGetMethod(entityType, fieldName, suffix);
    }
    if (getMethod == null) {
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault())
                + fieldName.substring(1) + suffix;
        try {
            getMethod = entityType.getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            Log.d("T", methodName + " not exist");
        }
    }
    return getMethod;
}