Example usage for java.lang Class getFields

List of usage examples for java.lang Class getFields

Introduction

In this page you can find the example usage for java.lang Class getFields.

Prototype

@CallerSensitive
public Field[] getFields() throws SecurityException 

Source Link

Document

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("java.lang.String");
    System.out.println("Fields =");

    // returns the array of Field objects representing the public fields
    Field f[] = cls.getFields();
    for (int i = 0; i < f.length; i++) {
        System.out.println(f[i]);
    }/*  w w  w.  j a v a2  s.c o  m*/

}

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

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

    Class cls = null;
    Field[] fields;/*from w ww  . j  a v a  2 s  .  c om*/

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

public static void main(String args[]) {
    try {/*from ww w. ja  v a2s .  co m*/
        Class c = Class.forName("java.awt.Dimension");
        System.out.println("Constructors:");
        Constructor constructors[] = c.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.println(" " + constructors[i]);
        }

        System.out.println("Fields:");
        Field fields[] = c.getFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println(" " + fields[i]);
        }

        System.out.println("Methods:");
        Method methods[] = c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(" " + methods[i]);
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:SampleField.java

static void printFieldNames(Object o) {
    Class c = o.getClass();
    Field[] publicFields = c.getFields();
    for (int i = 0; i < publicFields.length; i++) {
        String fieldName = publicFields[i].getName();
        Class typeClass = publicFields[i].getType();
        String fieldType = typeClass.getName();
        System.out.println("Name: " + fieldName + ", Type: " + fieldType);
    }/*from  w  ww  . j a v a 2 s .  co m*/
}

From source file:MySuperClass.java

public static ArrayList<String> getFieldsList(Class c) {
    Field[] fields = c.getFields();
    ArrayList<String> fieldsList = getFieldsDesciption(fields);
    return fieldsList;
}

From source file:Main.java

public static boolean ClassHasField(Class<?> cls, String fieldName) {
    Field[] fields = cls.getFields();
    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getName().equals(fieldName)) {
            return true;
        }//from w  w w  .jav a 2  s.  c o  m
    }
    return false;
}

From source file:Main.java

private static void addFields(Class<?> clazz, List<Field> list) {
    for (Field f : clazz.getFields()) {
        list.add(f);/*from   w  w w . j  a  va 2  s . c o  m*/
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass != null) {
        addFields(superClass, list);
    }
}

From source file:SampleArrayReflection.java

static void printArrayNames(Object target) {
    Class targetClass = target.getClass();
    Field[] publicFields = targetClass.getFields();
    for (int i = 0; i < publicFields.length; i++) {
        String fieldName = publicFields[i].getName();
        Class typeClass = publicFields[i].getType();
        String fieldType = typeClass.getName();
        if (typeClass.isArray()) {
            System.out.println("Name: " + fieldName + ", Type: " + fieldType);
        }/*from  ww w.jav a  2  s .co m*/
    }
}

From source file:Main.java

public static void copyBean(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*  w  w w  .jav a  2 s.c  o  m*/
        field.setAccessible(true);
        try {
            Object value = field.get(from);
            field.set(to, value);
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];//www . ja va 2  s .  c om
        field.setAccessible(true);
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }

}