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:io.github.seleniumquery.utils.SelectorUtils.java

private static boolean isHtmlUnitWebElementReallyDisplayed(WebElement webElement) {
    try {/*w w  w .  ja va 2  s.c o  m*/
        // #Cross-Driver #HtmlUnit #reflection #hack
        HtmlUnitWebElement htmlUnitWebElement = (HtmlUnitWebElement) webElement;
        Method getElementMethod = HtmlUnitWebElement.class.getDeclaredMethod("getElement");
        getElementMethod.setAccessible(true);
        HtmlElement htmlElement = (HtmlElement) getElementMethod.invoke(htmlUnitWebElement);
        return !(htmlElement instanceof HtmlHiddenInput) && htmlElement.isDisplayed();
    } catch (Exception e) {
        LOGGER.debug("Unable to retrieve real HtmlUnitWebElement#isDisplayed().", e);
        return true;
    }
}

From source file:com.xlson.standalonewar.Starter.java

/**
 * Add url to the system class loader.//from  w  w w  .j  av  a 2  s. co m
 */
public static void appendClasspath(URL url) throws IOException {

    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { url });
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException("Error, could not add URL to system classloader");
    }

}

From source file:com.framework.infrastructure.utils.ReflectionUtils.java

/**
 * , DeclaredMethod,. Object, null.// w ww.ja va2 s  .  c om
 * 
 * . Method,Method.invoke(Object obj, Object...
 * args)
 */
public static Method getAccessibleMethod(final Object obj, final String methodName,
        final Class<?>... parameterTypes) {

    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:ml.shifu.shifu.util.ClassUtils.java

public static Object invokeMethod(String name, Class<?> clazz, Object instance, Object... parameters) {
    try {/*ww w.j a  v  a  2s .c  om*/
        Method method = getFirstMethodWithName(name, clazz);
        method.setAccessible(true);
        return method.invoke(instance, parameters);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:brooklyn.entity.software.SshEffectorTasksTest.java

public static Integer getMyPid() {
    try {/*from   w  w  w  . j  av a  2 s.  c om*/
        java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        //            sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
        Object mgmt = jvm.get(runtime);
        java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);

        return (Integer) pid_method.invoke(mgmt);
    } catch (Exception e) {
        throw new PropagatedRuntimeException(
                "Test depends on (fragile) getMyPid method which does not work here", e);
    }
}

From source file:com.searchbox.framework.web.SystemController.java

/**
 * Try to run a getter function. This is useful because java 1.6 has a few
 * extra useful functions on the <code>OperatingSystemMXBean</code>
 *
 * If you are running a sun jvm, there are nice functions in:
 * UnixOperatingSystemMXBean and com.sun.management.OperatingSystemMXBean
 *
 * it is package protected so it can be tested...
 *///from   w  w  w .j  av  a 2s. c o  m
static void addGetterIfAvaliable(Object obj, String getter, Map<String, Object> info) {
    // This is a 1.6 function, so lets do a little magic to *try* to make it
    // work
    try {
        String n = Character.toUpperCase(getter.charAt(0)) + getter.substring(1);
        Method m = obj.getClass().getMethod("get" + n);
        m.setAccessible(true);
        Object v = m.invoke(obj, (Object[]) null);
        if (v != null) {
            info.put(getter, v);
        }
    } catch (Exception ex) {
    } // don't worry, this only works for 1.6
}

From source file:com.silverpeas.bootstrap.SilverpeasContextBootStrapper.java

private static void addURLs(URL[] urls) throws NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    if (cl instanceof URLClassLoader) {
        URLClassLoader urlClassloader = (URLClassLoader) cl;
        // addURL is a protected method, but we can use reflection to call it
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        // change access to true, otherwise, it will throw exception
        method.setAccessible(true);
        for (URL url : urls) {
            method.invoke(urlClassloader, new Object[] { url });
        }/*from www  .ja  v a  2  s.  c o m*/
    } else {
        // SystemClassLoader is not URLClassLoader....
    }
}

From source file:com.dosport.system.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredMethod,?. ?Object?, null.
 * /*from w  w w .  ja v  a  2 s .c o  m*/
 * ?. ?Method,?Method.invoke(Object obj, Object...
 * args)
 */
public static Method getAccessibleMethod(final Object obj, final String methodName,
        final Class<?>... parameterTypes) {
    Assert.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:ml.shifu.shifu.util.ClassUtils.java

public static Object invokeMethod(Method method, Object instance, Object... parameters) {
    try {// w  w w  .  j a v  a2s  . c  o  m
        method.setAccessible(true);
        return method.invoke(instance, parameters);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.stratosphere.client.minicluster.NepheleMiniCluster.java

private static void initializeIOFormatClasses() {
    try {//  www.  ja v a 2  s  . co m
        Method im = FileInputFormat.class.getDeclaredMethod("initDefaultsFromConfiguration");
        im.setAccessible(true);
        im.invoke(null);

        Method om = FileOutputFormat.class.getDeclaredMethod("initDefaultsFromConfiguration");
        om.setAccessible(true);
        om.invoke(null);
    } catch (Exception e) {
        LOG.error(
                "Cannot (re) initialize the globally loaded defaults. Some classes might mot follow the specified default behavior.");
    }
}