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

/**
 * 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.
 *///from   ww  w  .j a  v a  2  s .  c o  m
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 List<Field> listAnnotatedFields(Class<?> cls) {
    ArrayList<Class<?>> clsTree = new ArrayList<Class<?>>();
    boolean enteredDroidParts = false;
    do {/*w  w  w.  ja  va  2s . com*/
        clsTree.add(0, cls);
        boolean inDroidParts = cls.getName().startsWith("org.droidparts");
        if (enteredDroidParts && !inDroidParts) {
            break;
        } else {
            enteredDroidParts = inDroidParts;
            cls = cls.getSuperclass();
        }
    } while (cls != null);
    ArrayList<Field> fields = new ArrayList<Field>();
    for (Class<?> c : clsTree) {
        for (Field f : c.getDeclaredFields()) {
            if (f.getAnnotations().length > 0) {
                fields.add(f);
            }
        }
    }
    return fields;
}

From source file:Main.java

private static void createSetOfFields(Set<Field> collectionFields, Class c) {
    Field[] fields = c.getDeclaredFields();
    for (Field f : fields) {
        collectionFields.add(f);/*from ww w  .jav a2 s . c o  m*/
    }
}

From source file:Main.java

/**
 * Returns the field with the given name on the type lowest in the class hierarchy.
 * /*from  w  w  w  .j  ava 2s .  c  o m*/
 * @param name The name of the field to get
 * @param type The class on which to search for the field.
 * @return The field with this name lowest in the hierarchy, otherwise null if the field does not exist in the class hierarchy.
 */
public static Field getField(String name, Class<?> type) {
    for (Field field : type.getDeclaredFields()) {
        if (field.getName().equals(name)) {
            return field;
        }
    }

    if (type.getSuperclass() != null) {
        return getField(name, type.getSuperclass());
    }

    return null;
}

From source file:Main.java

public static Field findFieldByClassAndTypeAndName(Class<?> targetObject, Class<?> fieldType,
        String fieldName) {/*  w  w w  .  j a  v a2s  .com*/
    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

public static void copyProperties(Object desc, Object obj) {
    Class<?> descClass = desc.getClass();
    Class<?> objClass = obj.getClass();
    Field[] fields = objClass.getDeclaredFields();
    try {/*  w w w.  j  a v a  2  s . c o m*/
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            String getMethodName = "get" + toFirstLetterUpperCase(name);
            String setMethodName = "set" + toFirstLetterUpperCase(name);
            try {
                Object value = objClass.getMethod(getMethodName).invoke(obj);
                descClass.getMethod(setMethodName, value.getClass()).invoke(desc, value);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {

    }
}

From source file:Main.java

private static List<Field> getAllFieads(Object o) {
    ArrayList<Field> fields = new ArrayList<Field>();
    if (o != null) {
        Class<?> type = o.getClass();
        do {// w  w w  . j a v  a2  s .c  o  m
            for (Field f : type.getDeclaredFields()) {
                fields.add(f);
            }
            type = type.getSuperclass();
        } while (type != null);
    }
    return fields;
}

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  w w  .  jav  a 2  s  .c om*/

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

        return map;

    }

    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);/*from   ww w.  jav a2s . com*/
        }
    }

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

    return fields;
}

From source file:Main.java

public static List<Field> getDeclaredFields(Class<?> cls) {
    List<Field> list = new ArrayList<>();
    list.addAll(Arrays.asList(cls.getDeclaredFields()));
    if (!cls.getSuperclass().getName().equals(Object.class.getName())) {
        list.addAll(getDeclaredFields(cls.getSuperclass()));
    }//from w w w.j  a  v  a  2  s  .c  om
    return list;
}