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 void methodInvoker(Object receiver, String methodName, HashMap<String, Object> map) {
    for (Class<?> klass = receiver.getClass(); !(Object.class.equals(klass)); klass = klass.getSuperclass()) {
        try {/*from w  w  w .j a v  a2s  .c  om*/
            Method method = klass.getDeclaredMethod(methodName, Map.class);
            method.setAccessible(true);
            method.invoke(receiver, map);
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void forceConvertActivityFromTranslucent(Activity activity) {
    try {/*from   w ww  .ja v a  2 s .  c  o  m*/
        Method method = Activity.class.getDeclaredMethod("convertFromTranslucent", null);
        method.setAccessible(true);
        method.invoke(activity, null);
    } catch (Throwable ignored) {
    }
}

From source file:Main.java

static public Object invokeStaticMethod(Class<?> claxx, String methodName, Object... args) throws Exception {
    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 w  ww  .  ja v a2 s . c  om*/
        }
    }

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

From source file:Main.java

private static boolean isWifiApEnabled(WifiManager wifiManager) {
    try {//from   w w w  .j ava  2s. c o m
        Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(wifiManager);

    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void showInternalDebugLog() {
    try {//from  w ww.j a  v  a 2 s.  co m
        Class<?> clz = Class.forName("com.avos.avoscloud.AVOSCloud");
        Method startMethod = clz.getDeclaredMethod("showInternalDebugLog", Boolean.TYPE);
        startMethod.setAccessible(true);
        startMethod.invoke(null, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setIconEnable(Menu menu, boolean enable) {
    try {/*from ww  w  .  j  a va2  s  .c o  m*/
        Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
        Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
        m.setAccessible(true);
        m.invoke(menu, enable);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getSystemProperties(String property) {
    try {//w ww  .  j  a  va 2  s  .com
        Method getStringMethod = Build.class.getDeclaredMethod("getString", String.class);
        getStringMethod.setAccessible(true);
        return (String) getStringMethod.invoke(null, property);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Check mobile data is enable or not./*from w w w .  ja  va2 s . c o  m*/
 * @param context
 * @return
 */
public static boolean isMobileDataEnable(Context context) {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        // Some problem accessible private API
        e.printStackTrace();
    }
    return mobileDataEnabled;
}

From source file:Main.java

/**
 * Convert a translucent themed Activity
 * {@link android.R.attr#windowIsTranslucent} to a fullscreen opaque
 * Activity.//from  ww  w.j a v  a 2 s . co  m
 * <p>
 * Call this whenever the background of a translucent Activity has changed
 * to become opaque. Doing so will allow the {@link android.view.Surface} of
 * the Activity behind to be released.
 * <p>
 * This call has no effect on non-translucent activities or on activities
 * with the {@link android.R.attr#windowIsFloating} attribute.
 */
public static void convertActivityFromTranslucent(Activity activity) {
    try {
        Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
        method.setAccessible(true);
        method.invoke(activity);
    } catch (Throwable t) {
    }
}

From source file:Main.java

static Method getMethod(Class clazz, String methodName) {
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            method.setAccessible(true);
            return method;
        }/*  w  ww  .j a  va  2  s.  c  om*/
    }
    return null;
}