Example usage for java.lang Class getDeclaredFields

List of usage examples for java.lang Class getDeclaredFields

Introduction

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

Prototype

@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException 

Source Link

Document

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

Usage

From source file:Main.java

public static void reflect(Object o) throws Exception {
    Class cls = o.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];/*from   w  w  w. j  av  a 2s  .  co  m*/
        f.setAccessible(true);
    }
}

From source file:Main.java

/**
 * Return fields./*from w  w  w . java 2  s  . c o m*/
 * 
 * @param clazz
 *            the clazz
 * @return the list
 */
public static List<Field> returnFields(final Class<?> clazz) {
    return Arrays.asList(clazz.getDeclaredFields());
}

From source file:Main.java

/**
 * Extract all the fields of an object and its parent classes' fields iterative.
 * @param fields that belong to the class declaration.
 * @param type of the class from where extract the fields.
 * @return list of the fields fo the type class.
 *///from   w w  w.  j  a v  a  2s . c  o m
private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
    Collections.addAll(fields, type.getDeclaredFields());

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

    return fields;
}

From source file:Main.java

public static void printObject(Object object) {
    Class<?> class1 = object.getClass();
    Field[] fields = class1.getDeclaredFields();
    for (Field field : fields) {
        try {//from  www  .j  a va  2  s.c o m
            field.setAccessible(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Field getField(Class<?> clazz, String fieldName) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getName().equals(fieldName)) {
            field.setAccessible(true);/*from ww w. java  2s  .co  m*/
            return field;
        }
    }
    return null;
}

From source file:Main.java

public static void objectCopy(Object from, Object to) throws Exception {

    if (from.getClass() != to.getClass()) {
        throw new IllegalArgumentException("[objectCopy]The left and right must be same class");
    }//  ww w .  jav  a  2 s .c  o m
    Class<?> clz = from.getClass();
    Field[] fs = clz.getDeclaredFields();
    for (int i = 0; i < fs.length; i++) {
        Field field = fs[i];

        field.setAccessible(true);

        Object value = field.get(from);
        field.set(to, value);
    }
}

From source file:Main.java

/**
 * Returns all the unique fields of the classes in the given list.
 * @author Tristan Bepler/* www. j  av  a  2  s .  c o m*/
 */
private static Field[] getAllFields(List<Class<?>> classes) {
    Set<Field> fields = new HashSet<Field>();
    for (Class<?> clazz : classes) {
        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    }
    return fields.toArray(new Field[fields.size()]);
}

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  o m
    }
    Class<?> superClass = clazz.getSuperclass();
    if (!superClass.equals(Object.class)) {
        return getClassByPropertyName(superClass, propertyName);
    }
    return null;
}

From source file:blue.utility.ValuesUtility.java

public static void checkNullString(Object obj, boolean printMessages) {
    Class c = obj.getClass();

    Field[] fields = c.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getType() == String.class) {
            try {
                if (fields[i].get(obj) == null) {
                    if (printMessages) {
                        System.err.println("ValuesUtility: Null String found in " + c.getName() + " field: "
                                + fields[i].getName());
                    }//from w  w  w .j  av  a  2  s  .  c o m
                    fields[i].set(obj, "");
                }
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
            }
        }
    }
}

From source file:Main.java

private static List<Field> getClassFields(Class clazz) {
    ArrayList<Field> fields = new ArrayList<>(Arrays.asList(clazz.getDeclaredFields()));
    if (clazz.getSuperclass() != null) {
        fields.addAll(getClassFields(clazz.getSuperclass()));
    }//from   ww w .  j  a va 2s.c o  m
    return fields;
}