Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:Main.java

private static Field findField(String attrName, Field[] fields) {

    if (attrName == null || "".equals(attrName.trim()) || fields == null || fields.length == 0)
        return null;

    for (Field f : fields) {
        String fName = f.getName();
        if (attrName.equals(fName))
            return f;
    }/*from   w  ww  .  j  a  v a 2s.com*/
    return null;
}

From source file:Main.java

public static Map<String, String> compareMap(Map<String, String> map, Object obj) {
    Map<String, String> mapValue = new HashMap<String, String>();
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field field : fields) {
        String name = field.getName();
        if (map.containsKey(name)) {
            mapValue.put(name, map.get(name));
        }/*from w  w  w  .j  a  v  a 2s  .  c  om*/
    }
    return mapValue;
}

From source file:Main.java

public static void autoMappingJsonToObject(JSONObject json, Object obj) {
    Field[] fields = obj.getClass().getFields();
    for (Field field : fields) {
        if (json.optString(field.getName()) != null) {
            try {
                field.set(obj, json.optString(field.getName()));
            } catch (Exception e) {
            }// w  ww.  ja  v  a 2  s .c  o m
        }
    }
}

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 w w w.j a  v a2s .  c o m*/
            return field;
        }
    }
    return null;
}

From source file:Main.java

/*********************************************************************************
 * Returns the resource-IDs for all attributes specified in the given
 * <declare-styleable>-resource tag as an int array.
 *
 * @param context/*from   ww w. ja  va 2s  .  c  o m*/
 *            The current application context.
 * @param name
 *            The name of the <declare-styleable>-resource-tag to pick.
 * @return All resource-IDs of the child-attributes for the given
 *         <declare-styleable>-resource or <code>null</code> if this tag
 *         could not be found or an error occured.
 *********************************************************************************/
public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) {
    try {
        Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
        for (Field f : fields2) {
            if (f.getName().equals(name)) {
                int[] ret = (int[]) f.get(null);
                return ret;
            }
        }
    } catch (Throwable t) {
    }

    return null;
}

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();
        }// www. j  a v a 2  s . co m
    }
    Class<?> superClass = clazz.getSuperclass();
    if (!superClass.equals(Object.class)) {
        return getClassByPropertyName(superClass, propertyName);
    }
    return null;
}

From source file:Main.java

/**
 * A helper method to get a String[] out of a fieldArray
 *
 * @param fields R.strings.class.getFields()
 * @return a String[] with the string ids we need
 */// ww w  .j a  va 2  s  . c  o m
private static String[] getDefinedFonts(Context ctx, Field[] fields) {
    ArrayList<String> fieldArray = new ArrayList<String>();
    for (Field field : fields) {
        if (field.getName().contains("define_font_")) {
            fieldArray.add(getStringResourceByName(ctx, field.getName()));
        }
    }
    return fieldArray.toArray(new String[fieldArray.size()]);
}

From source file:Main.java

public static <T> T getVar(@NonNull Object obj, @NonNull String name) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Field f : cls.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                f.setAccessible(true);/*from  w w  w.jav  a  2s  . co  m*/
                //noinspection unchecked
                return (T) f.get(obj);
            }
        }
    }
    throw new RuntimeException("no var matching " + name);
}

From source file:Main.java

public static void setVar(@NonNull Object obj, @NonNull String name, @Nullable Object val) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Field f : cls.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                f.setAccessible(true);//  w  w w  .  j ava2  s.  c o m
                f.set(obj, val);
                return;
            }
        }
    }
    throw new RuntimeException("no var matching " + name);
}

From source file:Main.java

public static String getFieldName(Field field) {
    if (field == null)
        return "";
    return field.getName();
}