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

public static void setDataEnabled(Context context, boolean enabled) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Class<?> conMgrClass = null;
    Field iConMgrField = null;//from   ww  w .  ja  v a2 s .co  m
    Object iConMgr = null;
    Class<?> iConMgrClass = null;
    Method setMobileDataEnabledMethod = null;
    try {
        conMgrClass = Class.forName(conMgr.getClass().getName());
        iConMgrField = conMgrClass.getDeclaredField("mService");
        iConMgrField.setAccessible(true);
        iConMgr = iConMgrField.get(conMgr);
        iConMgrClass = Class.forName(iConMgr.getClass().getName());
        setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConMgr, enabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.google.gdt.eclipse.designer.ie.util.ReflectionUtils.java

/**
 * Returns the {@link Method} defined in {@link Class} (exactly).
 * /*from w  w  w  .j  a  va 2  s .  co m*/
 * @see #getMethodBySignature(Class, String).
 */
private static Method getMethodBySignature0(Class<?> clazz, String signatureName, String signature) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(signatureName) && getMethodSignature(method).equals(signature)) {
            method.setAccessible(true);
            return method;
        }
    }
    // not found
    return null;
}

From source file:ReflectionUtils.java

/**
 * ,private/protected.//from w w  w  . j a  v  a2 s  .  c o  m
 */
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
        final Object[] parameters) throws InvocationTargetException {
    Method method = getDeclaredMethod(object, methodName, parameterTypes);
    if (method == null)
        throw new IllegalArgumentException(
                "Could not find method [" + methodName + "] on target [" + object + "]");

    method.setAccessible(true);

    try {
        return method.invoke(object, parameters);
    } catch (IllegalAccessException e) {
        logger.error(":{}", e);
    }

    return null;
}

From source file:com.enation.eop.sdk.utils.ReflectionUtils.java

/**
 * , private/protected.//from  ww  w  . java  2s .c  o m
 */
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
        final Object[] parameters) {
    Method method = getDeclaredMethod(object, methodName, parameterTypes);
    if (method == null)
        throw new IllegalArgumentException(
                "Could not find method [" + methodName + "] on target [" + object + "]");

    method.setAccessible(true);

    try {
        return method.invoke(object, parameters);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:net.drgnome.virtualpack.util.Util.java

public static <T extends Cloneable> T copy(T object) {
        if (object != null) {
            try {
                Method m = object.getClass().getMethod("clone");
                m.setAccessible(true);
                return (T) m.invoke(object);
            } catch (Exception e) {
                e.printStackTrace();//from  w  ww . ja v a 2  s .com
            }
        }
        return null;
    }

From source file:com.abssh.util.ReflectionUtils.java

/**
 * , private/protected./*from   ww  w  .  j a  v  a 2s  . co  m*/
 */
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
        final Object[] parameters) {
    Method method = getDeclaredMethod(object, methodName, parameterTypes);
    if (method == null) {
        throw new IllegalArgumentException(
                "Could not find method [" + methodName + "] on target [" + object + "]");
    }

    method.setAccessible(true);

    try {
        return method.invoke(object, parameters);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

private static Object getValueWithMethod(Method getterMethod, Object bean) {

    try {//ww w .  j a v a2 s.  c  o m
        getterMethod.setAccessible(true);
        return getterMethod.invoke(bean);
    } catch (Exception ex) {
        throw new RuntimeException("Unable to use get method " + getterMethod.getName() + " on instance of "
                + bean.getClass().getName(), ex);
    }
}

From source file:com.theonespy.util.Util.java

public static Boolean isMobileDataEnabled() {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) MyApplication.getAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    try {//from w w w  .j ava 2s  .com
        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) {
        Util.Log("Can't check if mobile data is enabled: " + e);
    }

    return mobileDataEnabled;
}

From source file:com.project.framework.util.ReflectionUtils.java

/**
 * , private/protected./* w w  w. jav a 2 s  .co  m*/
 */
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
        final Object[] parameters) {
    Method method = getDeclaredMethod(object, methodName, parameterTypes);

    Assert.notNull(method, "Could not find field [" + methodName + "] on target [" + object + "]");

    method.setAccessible(true);

    try {
        return method.invoke(object, parameters);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.psiphon3.psiphonlibrary.WebViewProxySettings.java

@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyICS(Context ctx, String host, int port) {
    try {//  w w w . j a v a 2  s. co m
        Class webViewCoreClass = Class.forName("android.webkit.WebViewCore");

        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        if (webViewCoreClass != null && proxyPropertiesClass != null) {
            Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE, Object.class);
            Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class);

            if (m != null && c != null) {
                m.setAccessible(true);
                c.setAccessible(true);
                Object properties = c.newInstance(host, port, null);

                // android.webkit.WebViewCore.EventHub.PROXY_CHANGED = 193;
                m.invoke(null, 193, properties);
                return true;
            }
        }
    } catch (Exception e) {
        MyLog.d("Exception setting WebKit proxy through android.webkit.Network: " + e.toString());
    } catch (Error e) {
        MyLog.d("Exception setting WebKit proxy through android.webkit.Network: " + e.toString());
    }

    return false;
}