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

public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception {
    Class<?> cls = obj.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (data.containsKey(name)) {
            Object objValue = typeConversion(clsType, data.get(name));
            methodSet.invoke(obj, objValue);
        }/*  w ww.  j  a  v a2  s.  c om*/
    }
    return obj;
}

From source file:Main.java

public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception {
    Field[] fields = cls.getDeclaredFields();
    Object obj = cls.newInstance();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        Method methodSet = cls.getDeclaredMethod(strSet, clsType);
        if (map.containsKey(name)) {
            Object objValue = typeConversion(clsType, map.get(name));
            methodSet.invoke(obj, objValue);
        }/*from  w  w w  .j  a  va2 s .c  om*/
    }
    return obj;
}

From source file:Main.java

public static void collectDeviceInfo(Context ctx, HashMap<String, String> infos) {
    try {//from w  ww  .  ja  v a2  s .  c  o  m
        PackageManager pm = ctx.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
        if (pi != null) {
            String versionName = pi.versionName == null ? "null" : pi.versionName;
            String versionCode = pi.versionCode + "";
            infos.put("versionName", versionName);
            infos.put("versionCode", versionCode);
        }
    } catch (NameNotFoundException e) {
        e.getMessage();
    }
    Field[] fields = Build.class.getDeclaredFields();
    for (Field field : fields) {
        try {
            field.setAccessible(true);
            infos.put(field.getName(), field.get(null).toString());
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

From source file:Main.java

public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) {

    Field[] fields = cls.getDeclaredFields();

    T t = null;/*from w  w  w.j  a v a 2  s.  c  o m*/
    try {
        t = (T) cls.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    for (Field f : fields) {
        f.setAccessible(true);
        invokeSet(t, f.getName(), map.get(f.getName()));
    }
    return t;
}

From source file:com.prey.ReflectionsUtils.java

public static Object populateObject(JSONObject object) throws Exception {

    Object newObject = ReflectionsUtils.JSONObjectToObject(object);
    Field[] fields = newObject.getClass().getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        String setterName = "set" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);
        if (object.has(f.getName()))
            ReflectionsUtils.setProperty(setterName, newObject, object.get(f.getName()));
    }/* w  ww. j av  a2 s  .  c  o m*/

    return newObject;
}

From source file:Main.java

public static Map<String, Object> getClassFields(Object obj) {
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    for (Field field : getAllFieldsValues(obj.getClass())) {
        values.put(field.getName(), getField(field, obj));
    }//from   w  w w .  jav a2s .  co  m
    return values;
}

From source file:Main.java

public static Object mergedObject(Object oldObject, Object newObject) throws Exception {
    Class<?> cls = newObject.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        String strGet = "get" + method;
        Method methodGet = cls.getDeclaredMethod(strGet);
        Object object = methodGet.invoke(newObject);
        if (object != null) {
            String strSet = "set" + method;
            Method methodSet = cls.getDeclaredMethod(strSet, clsType);
            Object objValue = typeConversion(clsType, object.toString());
            methodSet.invoke(oldObject, objValue);
        }/*from w w  w.  jav a 2  s  .  c o  m*/
    }
    return oldObject;
}

From source file:Main.java

public static String toString(final Field field) {
    if (field == null) {
        return "null";
    }/* w  w  w . ja v a  2s . com*/
    StringBuffer sb = new StringBuffer(field.getName());
    sb.append("(").append(field.getName()).append(")");
    return sb.toString();
}

From source file:bigbluej.ReflectionUtils.java

public static SortedMap<String, Object> getFieldsAndValuesInSortedMap(Object object)
        throws IllegalAccessException {
    Validate.notNull(object);/*from   www .j  a  va2s . c om*/
    SortedMap<String, Object> fieldAndValues = new TreeMap<>();
    for (Field field : object.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(object);
        if (!field.isSynthetic() && value != null) {
            fieldAndValues.put(name, value);
        }
    }
    return fieldAndValues;
}

From source file:Main.java

public static Method findSetterMethodForField(Field field, Class<?> objectClass, Class<?> paramClass)
        throws NoSuchMethodException {
    String methodName = "set" + field.getName().toUpperCase().substring(0, 1) + field.getName().substring(1);
    return findMethodForField(methodName, objectClass, paramClass);
}