Example usage for java.lang.reflect Field get

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:Main.java

public static String get_CPU_ABI2() {
    try {//from w ww.  ja v  a2s  .c om
        Field field = Build.class.getDeclaredField("CPU_ABI2");
        if (field == null)
            return null;

        Object fieldValue = field.get(null);
        if (!(fieldValue instanceof String)) {
            return null;
        }

        return (String) fieldValue;
    } catch (Exception e) {

    }

    return null;
}

From source file:Main.java

public static Activity getCurrentTopActivity()
        throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
    activitiesField.setAccessible(true);
    Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread);
    for (Object activityRecord : activities.values()) {
        Class<?> activityRecordClass = activityRecord.getClass();
        Field pausedField = activityRecordClass.getDeclaredField("paused");
        pausedField.setAccessible(true);
        if (!pausedField.getBoolean(activityRecord)) {
            Field activityField = activityRecordClass.getDeclaredField("activity");
            activityField.setAccessible(true);
            Activity activity = (Activity) activityField.get(activityRecord);
            return activity;
        }//from www . ja  v a 2s.c o  m
    }
    return null;
}

From source file:Main.java

public static final String get_CPU_ABI2() {
    try {//from   w w w  .  ja  v a2 s . c  o  m
        Field field = Build.class.getDeclaredField("CPU_ABI2");
        if (field == null)
            return null;

        Object fieldValue = field.get(null);
        if (field == null || !(fieldValue instanceof String)) {
            return null;
        }

        return (String) fieldValue;
    } catch (Exception e) {

    }

    return null;
}

From source file:Main.java

public static void setListViewEdgeEffectColor(AbsListView listView, int color) {
    if (Build.VERSION.SDK_INT >= 21) {
        try {//from  w w  w. ja v  a2  s  .c  o  m
            Field field = AbsListView.class.getDeclaredField("mEdgeGlowTop");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(listView);
            if (mEdgeGlowTop != null) {
                mEdgeGlowTop.setColor(color);
            }

            field = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowBottom = (EdgeEffect) field.get(listView);
            if (mEdgeGlowBottom != null) {
                mEdgeGlowBottom.setColor(color);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static int hashCode(Object paramObject) {
    int i = 1;/* ww w  .  ja  v a2s . c  o  m*/
    Field[] arrayOfField = paramObject.getClass().getDeclaredFields();
    int j = arrayOfField.length;
    for (int k = 0;; k++) {
        int m;
        int i1;
        if (k < j) {
            Field localField = arrayOfField[k];
            localField.setAccessible(true);
            try {
                Object localObject = localField.get(paramObject);
                m = i * 31;
                if (localObject == null) {
                    i1 = 0;
                } else {
                    int n = localObject.hashCode();
                    i1 = n;
                }
            } catch (IllegalArgumentException localIllegalArgumentException) {
                localIllegalArgumentException.printStackTrace();
                continue;
            } catch (IllegalAccessException localIllegalAccessException) {
                localIllegalAccessException.printStackTrace();
                continue;
            }
        } else {
            return i;
        }
        i = m + i1;
    }
}

From source file:Main.java

public static Object getStaticValue(String fieldName) {
    Field field = getField(fieldName);
    if (field == null) {
        return null;
    }//from   w w  w .  j  a  v a  2s  .c om
    Object result = null;

    try {
        result = field.get(null);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Map<String, String> beanToMap(Object object) {
    Map<String, String> params = new HashMap<>();
    if (null == object) {
        return params;
    }//from   w  w  w .  ja v a  2 s .c o  m
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object fieldObj = field.get(object);
            if (fieldObj != null && fieldObj.getClass() == String.class) {
                String fieldValue = (String) fieldObj;
                if (!TextUtils.isEmpty(fieldValue)) {
                    if (!TextUtils.isEmpty(fieldValue)) {
                        fieldValue = fieldValue.replaceAll("\"", "%22");
                    }
                    params.put(fieldName, fieldValue);
                }
            }
        }
        return params;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return params;
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {/*  w w w . j  a  v  a 2  s. c  o m*/
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

From source file:Main.java

private static LongSparseArray<Drawable.ConstantState> hackPreloadDrawablesV15(Resources res) {
    try {/*from  w  w w  .  j  a v  a 2  s  . co  m*/
        Field field = Resources.class.getDeclaredField("sPreloadedDrawables");
        field.setAccessible(true);
        return (LongSparseArray<Drawable.ConstantState>) field.get(res);
    } catch (NoSuchFieldException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static void setListViewEdgeEffectColor(AbsListView listView, int color) {
    if (Build.VERSION.SDK_INT >= 21) {
        try {/* www. j  a v  a 2 s. co m*/
            Field field = AbsListView.class.getDeclaredField("mEdgeGlowTop");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(listView);
            if (mEdgeGlowTop != null) {
                mEdgeGlowTop.setColor(color);
            }

            field = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowBottom = (EdgeEffect) field.get(listView);
            if (mEdgeGlowBottom != null) {
                mEdgeGlowBottom.setColor(color);
            }
        } catch (Exception e) {
            //                FileLog.e("tmessages", e);
        }
    }
}