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

public static void printField(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typeName(f.getType()) + " " + f.getName() + ";");
}

From source file:Main.java

public static void print_field(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typename(f.getType()) + " " + f.getName() + ";");
}

From source file:ReflectionTest.java

/**
 * Prints all fields of a class//from   w ww  .j  a v a2 s.  com
 * @param cl a class
 */
public static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for (Field f : fields) {
        Class type = f.getType();
        String name = f.getName();
        System.out.print("   ");
        String modifiers = Modifier.toString(f.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.println(type.getName() + " " + name + ";");
    }
}

From source file:Main.java

public static Class<?> getClassByPropertyName(Class<?> clazz, String propertyName) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getName().equals(propertyName)) {
            return field.getType();
        }//from   w w w.ja v a  2  s.  c  om
    }
    Class<?> superClass = clazz.getSuperclass();
    if (!superClass.equals(Object.class)) {
        return getClassByPropertyName(superClass, propertyName);
    }
    return null;
}

From source file:Main.java

private static boolean equalsType(Field type, Class<?>... cls) {
    for (int i = 0; i < cls.length; i++) {
        Class<?> mClass = cls[i];
        if (type.getType().getCanonicalName().equals(mClass.getName())) {
            return true;
        }//from  ww  w. j  a va  2s  .  c o  m
    }
    return false;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getClassFields(Class clazz, boolean includeParentClass) {

    Map<String, Class> map = new HashMap<String, Class>();

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());// field.getType()

    }//  ww w . j av  a  2 s  . c  o  m

    if (includeParentClass)

        getParentClassFields(map, clazz.getSuperclass());

    return map;

}

From source file:Main.java

public static List<Field> getAllFieldsOfType(List<Field> fields, Class<?> type, Class<?> fieldType) {
    for (Field field : type.getDeclaredFields()) {
        if (fieldType == null || fieldType.isAssignableFrom(field.getType())) {
            fields.add(field);/*  ww  w  . ja  v a2 s .c o  m*/
        }
    }

    if (type.getSuperclass() != null) {
        fields = getAllFieldsOfType(fields, type.getSuperclass(), fieldType);
    }

    return fields;
}

From source file:Main.java

/**
 * Determines if the field is an instance of a primitive.
 * <ul><li>String.class</li>
 * <li>Boolean.class <b>OR</b> boolean.class</li>
 * <li>Integer.class <b>or</b> int.class</li>
 * <li>Double.class <b>or</b> double.class</li>
 * <li>Float.class <b>or</b> float.class</li></ul>
 * @param field The field to verify// www  . j av a 2  s .c  o  m
 * @return The true if primitive
 */
public static boolean isPrimitive(Field field) {

    if (field == null) {
        return false;
    }

    if (field.getType() == String.class) {
        return true;
    } else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
        return true;
    } else if (field.getType() == Integer.class || field.getType() == int.class) {
        return true;
    } else if (field.getType() == Double.class || field.getType() == double.class) {
        return true;
    } else if (field.getType() == Float.class || field.getType() == float.class) {
        return true;
    }
    return false;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Map<String, Class> getParentClassFields(Map<String, Class> map, Class clazz) {

    Field[] fields = clazz.getDeclaredFields();

    for (Field field : fields) {

        map.put(clazz.getName() + "." + field.getName(), field.getType());

    }/*from  w ww  .  ja  va 2  s  .  c o m*/

    if (clazz.getSuperclass() == null) {

        return map;

    }

    getParentClassFields(map, clazz.getSuperclass());

    return map;

}

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

public static final Object convert(final Field field, final Cursor cursor) throws Exception {
    if (field.getType().equals(Long.class))
        return cursor.getLong(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Integer.class))
        return cursor.getInt(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
    if (field.getType().equals(Date.class))
        return DATE_FORMAT
                .parse(cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name())));
    return cursor.getString(cursor.getColumnIndex(field.getAnnotation(Column.class).name()));
}