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

private static long[] findDeadlockedThreads(ThreadMXBean threadBean) throws SecurityException,
        NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Method method = threadBean.getClass().getDeclaredMethod("findDeadlockedThreads");
    method.setAccessible(true);
    return (long[]) method.invoke(threadBean);
}

From source file:Main.java

static public Object invokeMethod(Object owner, String methodName, Object... args) throws Exception {
    Class<?> ownerClass = owner.getClass();
    Class<?>[] argsClass = null;

    if (args != null && args.length > 0) {
        argsClass = new Class<?>[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
            if (argsClass[i] == Integer.class) {
                argsClass[i] = int.class;
            } else if (argsClass[i] == Boolean.class) {
                argsClass[i] = boolean.class;
            }//from  ww w  .j av a  2s.com
        }
    }

    Method method = ownerClass.getDeclaredMethod(methodName, argsClass);
    method.setAccessible(true);
    return method.invoke(owner, args);
}

From source file:Main.java

public static <T extends Object, I, P extends I> T bind(final T component, final P param, final Class<I> type) {

    try {/*from w w  w .ja v  a  2s  .c  o m*/
        final Method m = findMethod(component, "bind", param, type);
        m.setAccessible(true);
        m.invoke(component, param);
    } catch (final Exception e) {
        throw new UnsupportedOperationException("Could not bind component", e);
    }

    return component;

}

From source file:Main.java

private static Method initAddMethod() {
    try {//from   ww  w .j  a v a 2  s.  c  o m
        Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        add.setAccessible(true);
        return add;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void setPopupWindowModal(PopupWindow popupWindow, boolean modal) {
    try {/*w  w w  . ja  v  a2 s.com*/
        Method method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class);
        method.setAccessible(true);
        method.invoke(popupWindow, modal);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Retrieves a system property/* w  w w.  ja  va  2s.co m*/
 * @param key the property key
 * @param def the value to be returned if the key could not be resolved
 */
public static String getSystemProperty(String key, String def) {
    try {
        Method getString = Build.class.getDeclaredMethod("getString", String.class);
        getString.setAccessible(true);
        return getString.invoke(null, key).toString();
    } catch (Exception ex) {
        return def;
    }
}

From source file:Main.java

/**
 * Returns the root of the preference hierarchy managed by this class.
 *
 * @return The {@link android.preference.PreferenceScreen} object that is at the root of the hierarchy.
 *///from   ww  w .j a  v a2s. c o m
public static PreferenceScreen getPreferenceScreen(@NonNull PreferenceManager manager) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
        m.setAccessible(true);
        return (PreferenceScreen) m.invoke(manager);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Called by the {@link android.preference.PreferenceManager} to dispatch the activity stop
 * event.//from ww  w. j  a va 2s  .  co  m
 */
public static void dispatchActivityStop(@NonNull PreferenceManager manager) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
        m.setAccessible(true);
        m.invoke(manager);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Called by the {@link android.preference.PreferenceManager} to dispatch the activity destroy
 * event.//w  w  w.  j  a  va 2 s .c  o  m
 */
public static void dispatchActivityDestroy(@NonNull PreferenceManager manager) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
        m.setAccessible(true);
        m.invoke(manager);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static boolean isMobileEnabled(Context context) {
    try {/*from  ww w  .ja va2 s .  c  o  m*/
        Method e = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled", new Class[0]);
        e.setAccessible(true);
        return ((Boolean) e.invoke(getConnManager(context), new Object[0])).booleanValue();
    } catch (Exception var2) {
        var2.printStackTrace();
        return true;
    }
}