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:Main.java

private static boolean isFieldsEqual(Field one, Field two) {
    return one.getName().equals(two.getName()) && one.getType().equals(two);
}

From source file:Main.java

public static Field findFieldByClassAndTypeAndName(Class<?> targetObject, Class<?> fieldType,
        String fieldName) {/*ww  w .  ja  v a2s.  c o m*/
    Class clazz = targetObject;
    do {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.getType() == fieldType && field.getName().equals(fieldName)) {
                field.setAccessible(true);
                return field;
            }
        }
        clazz = clazz.getSuperclass();
    } while (clazz != null);
    throw new NoSuchFieldError("Field of type " + fieldType.getName() + " in class " + targetObject.getName());
}

From source file:Main.java

static boolean isCollection(Field field) {
    return Collection.class.isAssignableFrom(field.getType());
}

From source file:Main.java

/**
 * Returns the first field of the given type in a class.
 * Might be useful for Proguard'ed classes to identify fields with unique types.
 * If no matching field was not found, a {@link NoSuchFieldError} will be thrown.
 *//*  w  w  w  . java  2s  .c  om*/
public static Field findFirstFieldByExactType(Class<?> clazz, Class<?> type) {
    Class<?> clz = clazz;
    do {
        for (Field field : clz.getDeclaredFields()) {
            if (field.getType() == type) {
                field.setAccessible(true);
                return field;
            }
        }
    } while ((clz = clz.getSuperclass()) != null);

    throw new NoSuchFieldError("Field of type " + type.getName() + " in class " + clazz.getName());
}

From source file:Main.java

public static void keep_setInt(Field field, Object obj, Cursor cursor, int i) {
    try {//  w w  w.  ja  v a2  s  .  co  m
        if (field.getType().equals(Integer.TYPE))
            field.setInt(obj, cursor.getInt(i));
        else
            field.set(obj, Integer.valueOf(cursor.getInt(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void keep_setFloat(Field field, Object obj, Cursor cursor, int i) {
    try {/*from w  ww  . java  2s.  c  om*/
        if (field.getType().equals(Float.TYPE))
            field.setFloat(obj, cursor.getFloat(i));
        else
            field.set(obj, Float.valueOf(cursor.getFloat(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void keep_setShort(Field field, Object obj, Cursor cursor, int i) {
    try {//  w  w w  . j a  v a  2s. co m
        if (field.getType().equals(Short.TYPE))
            field.setShort(obj, cursor.getShort(i));
        else
            field.set(obj, Short.valueOf(cursor.getShort(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void keep_setLong(Field field, Object obj, Cursor cursor, int i) {
    try {//from  www.j a v  a 2s.c o  m
        if (field.getType().equals(Long.TYPE))
            field.setLong(obj, cursor.getLong(i));
        else
            field.set(obj, Long.valueOf(cursor.getLong(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

private static Object getCursorValue(Cursor cursor, Field field, String columnName) {
    Class<?> fieldType = field.getType();

    if (fieldType.equals(int.class) || field.equals(Integer.class)) {
        return cursor.getInt(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(String.class)) {
        return cursor.getString(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(float.class) || field.equals(Float.class)) {
        return cursor.getFloat(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(double.class) || field.equals(Double.class)) {
        return cursor.getDouble(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(Date.class)) {
        long time = cursor.getLong(cursor.getColumnIndex(columnName));
        Date date = new Date(time);
        return date;
    }/* w w w . j  av a 2 s  .  c o m*/
    return cursor.getString(cursor.getColumnIndex(columnName));
}

From source file:Main.java

static Class<? extends Object> getFieldClass(Field field) {
    return isCollection(field) ? getParametricClass(field) : field.getType();
}