Example usage for java.lang.reflect Method setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

/**
 * Check whether this PendingIntent will launch an Activity.
 *//*from  www  .j  a v  a 2s  .  com*/
public static boolean isActivity(@NonNull PendingIntent pi) {
    Method method;
    try {
        method = PendingIntent.class.getDeclaredMethod("isActivity");
        method.setAccessible(true);
        return (boolean) method.invoke(pi);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true; // We must use `true` ss the fallback value
}

From source file:Main.java

private static void closeWifiAp(WifiManager wifiManager) {
    if (isWifiApEnabled(wifiManager)) {
        try {//from   w  w  w.  j a  v  a  2s .  c o m
            Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
            method.setAccessible(true);

            WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);

            Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,
                    boolean.class);
            method2.invoke(wifiManager, config, false);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static <T> T runPrivateMethod(Class<?> targetClass, String methodName, Class<?> paramClasses[],
        Object params[], Class<T> returnType) throws Exception {
    T returnValue = null;/*w  ww.  jav a2  s  .co m*/
    Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
    if (!method.isAccessible())
        method.setAccessible(true);
    returnValue = (T) method.invoke(targetClass, params);
    return returnValue;
}

From source file:Main.java

public static <T> T runPrivateMethod(Class<?> targetClass, Object obj, String methodName,
        Class<?> paramClasses[], Object params[], Class<T> returnType) throws Exception {
    T returnValue = null;//from  w w w  .jav a  2  s  .co  m
    Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
    if (!method.isAccessible())
        method.setAccessible(true);
    returnValue = (T) method.invoke(obj, params);
    return returnValue;
}

From source file:Main.java

public static boolean collapseStatusBar(Context c) {
    try {//from  w ww . j  a v  a  2  s  .c om
        Object service = c.getSystemService("statusbar");
        Class<?> claz = Class.forName("android.app.StatusBarManager");
        Method collapse;

        try {
            collapse = claz.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);

        } catch (Exception e) {
            try {
                collapse = claz.getMethod("collapsePanels");
                collapse.setAccessible(true);
                collapse.invoke(service);
            } catch (Exception err) {
                return false;
            }
        }

    } catch (Exception e) {
        return false;
    }

    return true;
}

From source file:api.Status.java

public static Result getStatus() {
    ObjectNode metrics = Json.newObject();

    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    TreeMap<String, Object> values = new TreeMap<String, Object>();
    for (Method method : os.getClass().getDeclaredMethods()) {
        method.setAccessible(true);
        if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
            Object value;//from   www  .  j a  va  2  s . c  o m
            try {
                value = method.invoke(os);
                values.put(method.getName(), value);
            } catch (Exception e) {
                Logger.warn("Error when invoking " + os.getClass().getName()
                        + " (OperatingSystemMXBean) method " + method.getName() + ": " + e);
            } // try
        } // if
    } // for

    metrics.put("jvmLoad", (Double) values.get("getProcessCpuLoad"));
    metrics.put("cpuLoad", (Double) values.get("getSystemCpuLoad"));
    metrics.put("openFD", (Long) values.get("getOpenFileDescriptorCount"));
    metrics.put("maxFD", (Long) values.get("getMaxFileDescriptorCount"));
    metrics.put("freeMemory", (Long) values.get("getFreePhysicalMemorySize"));
    metrics.put("totalMemory", (Long) values.get("getTotalPhysicalMemorySize"));

    return ok(metrics.toString());
}

From source file:Main.java

public static Object invokeMethod(Object target, Class clazz, String methodName, Class[] paramTypes,
        Object[] paramValues) {/*w  w w  .j av  a 2s.c  o  m*/
    try {
        Method method = clazz.getDeclaredMethod(methodName, paramTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return method.invoke(target, paramValues);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static Method getMethod(Class<?> clz, final String mtdName, Class<?>[] mtdArgs) {
    if (clz == null || TextUtils.isEmpty(mtdName) || mtdArgs == null) {
        return null;
    }/*from w ww  .  j a  va 2s. c  o  m*/
    Method mtd = null;
    try {
        mtd = clz.getDeclaredMethod(mtdName, mtdArgs);
        mtd.setAccessible(true);
    } catch (NoSuchMethodException e) {
        Log.d(TAG, e.getMessage(), e);
    }
    return mtd;
}

From source file:Main.java

protected static Object getITelephony(final Context context) throws SecurityException, NoSuchMethodException,
        IllegalArgumentException, InvocationTargetException, IllegalAccessException {
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    final Method getITelephony = tm.getClass().getDeclaredMethod("getITelephony");
    if (!getITelephony.isAccessible()) {
        getITelephony.setAccessible(true);
    }//  w ww . j ava 2  s .  c om
    return getITelephony.invoke(tm);
}

From source file:Main.java

public static void callInjectViews(Object activity) {
    try {//ww  w .ja va 2 s. c o m
        Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
        Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
        injectViewsMethod.setAccessible(true);
        injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException e) {
        propagateRuntimeException(e);
    } catch (NoSuchMethodException e) {
        propagateRuntimeException(e);
    } catch (SecurityException e) {
        propagateRuntimeException(e);
    } catch (IllegalAccessException e) {
        propagateRuntimeException(e);
    } catch (IllegalArgumentException e) {
        propagateRuntimeException(e);
    } catch (InvocationTargetException e) {
        propagateRuntimeException(e);
    }
}