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

public static void main(String[] args) {
    Field[] fields = java.lang.Integer.class.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        Class type = f.getType();
        System.out.println(type.getCanonicalName());
    }/*from  w  w  w .  ja  va2 s. c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    Field[] fields = java.lang.Integer.class.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        Class type = f.getType();
        String name = f.getName();
        System.out.print(Modifier.toString(f.getModifiers()));
        System.out.println(" " + type.getCanonicalName() + " " + name + ";");
    }//from   ww  w  .j av  a2  s.  c  o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object clazz = new TestClass();
    String lookingForValue = "firstValue";

    Field field = clazz.getClass().getField(lookingForValue);
    Class clazzType = field.getType();
    if (clazzType.toString().equals("double"))
        System.out.println(field.getDouble(clazz));
    else if (clazzType.toString().equals("int"))
        System.out.println(field.getInt(clazz));

    //System.out.println(field.get(clazz));
}

From source file:ArrayFind.java

public static void main(String... args) {
    boolean found = false;
    try {//from   w w w .ja va 2s  .co  m
        Class<?> cls = Class.forName(args[0]);
        Field[] flds = cls.getDeclaredFields();
        for (Field f : flds) {
            Class<?> c = f.getType();
            if (c.isArray()) {
                found = true;
                out.format(
                        "%s%n" + "           Field: %s%n" + "            Type: %s%n" + "  Component Type: %s%n",
                        f, f.getName(), c, c.getComponentType());
            }
        }
        if (!found) {
            out.format("No array fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:FieldSpy.java

public static void main(String... args) {
    try {/*from  w  ww  .  j a v  a 2 s  . co  m*/
        Class<?> c = Class.forName("FieldSpy");
        Field f = c.getField("name");
        System.out.format("Type: %s%n", f.getType());
        System.out.format("GenericType: %s%n", f.getGenericType());

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    GetFields object = new GetFields();
    Class clazz = object.getClass();

    // Get all object fields including public, protected, package and private
    // access fields.
    Field[] fields = clazz.getDeclaredFields();
    System.out.println("Number of fields = " + fields.length);
    for (Field field : fields) {
        System.out.println("Field name = " + field.getName());
        System.out.println("Field type = " + field.getType().getName());
    }/*  w w  w  .j a v a2 s.  c  o  m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    GetFields object = new GetFields();
    Class clazz = object.getClass();

    // Get all object accessible public fields.
    Field[] fields = clazz.getFields();
    System.out.println("Number of fields = " + fields.length);
    for (Field field : fields) {
        System.out.println("Field name = " + field.getName());
        System.out.println("Field type = " + field.getType().getName());
    }//w  ww  . ja  v a 2 s. c  o  m

    Field field = clazz.getField("id");
    System.out.println("Field name = " + field.getName());
    System.out.println("Field type = " + field.getType().getName());
}

From source file:gov.nih.nci.cabig.caaers.tools.ObjectDump.java

public static void main(String[] args) {
    ObjectDump target = new ObjectDump();

    Class cls = null;/* w w  w  .jav a2 s . c  o  m*/
    Field[] fields;

    try {
        cls = Class.forName(target.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    write("Class: " + target.getClass().getName() + "\n");
    fields = cls.getFields();

    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        write("\tfield[" + (i + 1) + "]: " + field.getName() + "\r\n");

        Type type = field.getType();
        write(type.toString());
    }

}

From source file:Main.java

public static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        Class type = f.getType();
        String name = f.getName();
        System.out.print(Modifier.toString(f.getModifiers()));
        System.out.println(" " + type.getName() + " " + name + ";");
    }/*from w  ww.  jav  a 2s . c om*/
}

From source file:Main.java

public static Class<?> getComponentType(Field f) {
    return f.getType().getComponentType();
}