Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> List<T> generatePropertyList(Collection<?> collection, String property) {
    assert property != null;
    if (collection == null || collection.isEmpty()) {
        return new ArrayList<T>(0);
    }/*w w  w  .jav a  2  s  . c  o  m*/
    List<T> list = new ArrayList<T>(collection.size());
    try {
        for (Object obj : collection) {
            Field field = obj.getClass().getDeclaredField(property);
            field.setAccessible(true);
            Object object = field.get(obj);
            list.add((T) object);
        }
        return list;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static int getOpValue(String name) {
    try {//www .  ja v  a 2 s . c om
        final Field f = AppOpsManager.class.getField(name);
        f.setAccessible(true);
        return f.getInt(null);
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    }

    return -1;
}

From source file:Main.java

public static int getPid(Process process) {
    int pid = 0;/*  w  ww . j  a v a2s.  c  o  m*/
    String pname = process.getClass().getName();
    if (pname.equals("java.lang.ProcessManager$ProcessImpl")) {

        try {
            java.lang.reflect.Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:nl.ctrlaltdev.harbinger.testutil.TestUtil.java

public static final void resetMockHttpSessionId() {
    try {/*from w w  w. j  a v a  2s  .c  om*/
        Field nextId = MockHttpSession.class.getDeclaredField("nextId");
        nextId.setAccessible(true);
        nextId.set(null, Integer.valueOf(1));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**set dialog dismiss or not*/
public static void setDialogDismiss(DialogInterface dialog, boolean dismiss) {
    try {/* www  .  ja va2s .c o m*/
        Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
        field.setAccessible(true);
        field.set(dialog, dismiss);
        dialog.dismiss();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Get all fields' value and put them to a map.
 * //from  w ww.j av a  2  s .com
 * @param bean
 * @return Map
 */
public static Map<String, Object> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, Object> valueMap = new HashMap<String, Object>();
    // Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        try {
            field.setAccessible(true);
            Object value = field.get(bean);
            //            if(value == null) {
            //               valueMap.put(field.getName(), "");
            //               continue;
            //            }
            valueMap.put(field.getName(), value);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return valueMap;
}

From source file:Main.java

static Unsafe getSMU() {
    try {/*  w w w . ja  v  a 2  s  .c  o m*/
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {
        // ignore
    }
    try {
        return java.security.AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
            Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
            for (Field f : k.getDeclaredFields()) {
                f.setAccessible(true);
                Object x = f.get(null);
                if (k.isInstance(x))
                    return k.cast(x);
            }
            throw new NoSuchFieldError("the Unsafe");
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}

From source file:Main.java

public static void collectDeviceInfo(Context ctx, HashMap<String, String> infos) {
    try {/*from   www .j  a v a 2s.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 void init() {
    try {//from  w  ww  .  j a va  2s .  co m
        Class AppOpsManager = Class.forName("android.app.AppOpsManager");
        Field field = AppOpsManager.getDeclaredField("OP_SYSTEM_ALERT_WINDOW");
        field.setAccessible(true);
        OP_SYSTEM_ALERT_WINDOW = field.getInt(null);
    } catch (ClassNotFoundException e2) {

    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform.
 *///from  w  w  w  . j  a v a  2 s .com
private static sun.misc.Unsafe getUnsafe() {
    sun.misc.Unsafe unsafe = null;
    try {
        unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {
            @Override
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;

                for (Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x)) {
                        return k.cast(x);
                    }
                }
                // The sun.misc.Unsafe field does not exist.
                return null;
            }
        });
    } catch (Throwable e) {
        // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError
        // for Unsafe.
    }
    return unsafe;
}