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:com.squarespace.gibson.GibsonUtils.java

/**
 * {@link Throwable#getStackTrace()} returns a copy. We can try to use the getOurStackTrace()
 * method instead but it's a private.//from w  ww .ja  v  a 2  s .co  m
 */
private static Method getOurStackTrace() {
    Method method = null;
    try {
        method = Throwable.class.getDeclaredMethod("getOurStackTrace");
        method.setAccessible(true);
    } catch (NoSuchMethodException err) {
        LOG.error(Gibson.MARKER, "NoSuchMethodException", err);
    }
    return method;
}

From source file:cn.hxh.springside.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredMethod,?./*from  w w  w  . j  a v a2s  .co m*/
 * ?Object?, null.
 * 
 * ?. ?Method,?Method.invoke(Object obj, Object... args)
 */
public static Method getAccessibleMethod(final Object obj, final String methodName,
        final Class<?>... parameterTypes) {
    AssertUtils.notNull(obj, "object?");

    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Method method = superClass.getDeclaredMethod(methodName, parameterTypes);

            method.setAccessible(true);

            return method;

        } catch (NoSuchMethodException e) {//NOSONAR
            // Method??,?
        }
    }
    return null;
}

From source file:com.baomidou.framework.common.util.BeanUtil.java

/**
 * ?/*ww  w  .j  a v  a  2s.  co  m*/
 * 
 * @param source
 *            ?
 * @param target
 *            ?
 */
public static void copy(Object source, Object target) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");
    Class<?> actualEditable = target.getClass();
    PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null) {
            PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    /*
                     * no copy null properties
                     */
                    Object value = readMethod.invoke(source);
                    if (value != null) {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:Main.java

/**
 * switch data to on/off/* ww  w.j  a v a2  s  .c  om*/
 * 
 * @param context
 * @param status
 */
public static void setData(Context context, boolean status) {
    ConnectivityManager dataManager;
    dataManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Method dataMtd = null;
    try {
        dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    dataMtd.setAccessible(true);
    try {
        dataMtd.invoke(dataManager, status);
    } 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

private static boolean setWebkitProxyICS(Context ctx, String host, int port) throws Exception {

    // PSIPHON: added support for Android 4.x WebView proxy
    try {/*from  w  ww.j  ava  2 s .c  o  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;
            } else
                return false;
        }
    } catch (Exception e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.net.ProxyProperties: " + e.toString());
    } catch (Error e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.webkit.Network: " + e.toString());
    }

    return false;

}

From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java

@SuppressWarnings("unchecked")
public static <T> T callMethod(String name, Object target) throws Exception {
    Class<?> clazz = target.getClass();
    Method method = ReflectionUtils.findMethod(clazz, name);

    if (method == null)
        throw new IllegalArgumentException(
                "Cannot find method '" + method + "' in the class hierarchy of " + target.getClass());
    method.setAccessible(true);
    return (T) ReflectionUtils.invokeMethod(method, target);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Method getMethod(Object target, String name, Class... parameterTypes) {
    String key = target.getClass().getSimpleName() + ":" + name + ":"
            + (parameterTypes == null ? 0 : parameterTypes.length);
    Method method = methods.get(key);
    if (method == null) {
        for (Class obj = target.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass()) {
            try {
                method = obj.getDeclaredMethod(name, parameterTypes);
                method.setAccessible(true);
                methods.put(key, method);
                break;
            } catch (SecurityException e) {
                e.printStackTrace();/*w  w  w .  ja v  a  2  s.  c  o  m*/
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
    return method;
}

From source file:com.reactive.hzdfs.io.MemoryMappedChunkHandler.java

private static boolean unmap(MappedByteBuffer bb) {
    /*//  w w w.ja  va 2s .c om
     * From  sun.nio.ch.FileChannelImpl
     * private static void  unmap(MappedByteBuffer bb) {
                  
           Cleaner cl = ((DirectBuffer)bb).cleaner();
           if (cl != null)
       cl.clean();
            
            
       }
     */
    try {
        Method cleaner_method = ReflectionUtils.findMethod(bb.getClass(), "cleaner");
        if (cleaner_method != null) {
            cleaner_method.setAccessible(true);
            Object cleaner = cleaner_method.invoke(bb);
            if (cleaner != null) {
                Method clean_method = ReflectionUtils.findMethod(cleaner.getClass(), "clean");
                clean_method.setAccessible(true);
                clean_method.invoke(cleaner);
                return true;
            }
        }

    } catch (Exception ex) {
        log.debug("", ex);
    }
    return false;
}

From source file:io.codis.nedis.util.NedisUtils.java

public static EventExecutor getEventExecutor(Future<?> future) {
    Class<?> clazz = future.getClass();
    for (;;) {// w  ww . j  a va  2s . c  o  m
        try {
            Method method = clazz.getDeclaredMethod("executor");
            method.setAccessible(true);
            return (EventExecutor) method.invoke(future);
        } catch (NoSuchMethodException e) {
        } catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        clazz = clazz.getSuperclass();
        if (clazz == null) {
            return null;
        }
    }
}

From source file:io.github.seleniumquery.functions.jquery.forms.ValFunction.java

private static void changeContentEditableValueInHtmlUnit(WebDriver driver, WebElement element, String value) {
    // we resort to JavaScript when it is enabled
    if (((HtmlUnitDriver) driver).isJavascriptEnabled()) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(// ww  w.ja v a2  s.  c o  m
                "arguments[0]['innerText' in arguments[0] ? 'innerText' : 'textContent'] = arguments[1];",
                element, value);
    } else {
        // or use reflection if JS is not enabled
        // (this method is not preferred as it relies on HtmlUnit's internals, which can change without notice)
        try {
            // #HtmlUnit #reflection #hack
            Method getElementMethod = org.openqa.selenium.htmlunit.HtmlUnitWebElement.class
                    .getDeclaredMethod("getElement");
            getElementMethod.setAccessible(true);

            HtmlElement he = (HtmlElement) getElementMethod.invoke(element);
            HTMLElement e = (HTMLElement) he.getScriptObject();

            e.setInnerText(value);
        } catch (Exception e) {
            LOGGER.warn("Unable to set HtmlUnitWebElement's innerText.", e);
        }
    }
}