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 Method executeMethod(Object obj, Method executeMethod) {
    try {//from www.  ja  v a2  s  .  c om
        if (executeMethod != null) {
            if (!executeMethod.isAccessible())
                executeMethod.setAccessible(true);
            executeMethod.invoke(obj, new Object[] {});
        }
    } catch (Exception e) {
        executeMethod = null;
        e.printStackTrace();
    } finally {
        return executeMethod;
    }

}

From source file:Main.java

/**
 * Calling the convertToTranslucent method on platforms before Android 5.0
 */// w  ww .  ja  v a  2 s. com
public static void convertActivityToTranslucentBeforeL(Activity activity) {
    try {
        Class<?>[] classes = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        for (Class clazz : classes) {
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
            }
        }
        Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
                translucentConversionListenerClazz);
        method.setAccessible(true);
        method.invoke(activity, new Object[] { null });
    } catch (Throwable t) {
    }
}

From source file:net.fabricmc.loom.task.ProcessModsTask.java

public static void addFile(File file, Object object) {
    try {/*from ww  w .  j a  va  2s  .co  m*/
        URLClassLoader classLoader = (URLClassLoader) object.getClass().getClassLoader();
        Class urlClassLoaderClass = URLClassLoader.class;
        Method method = urlClassLoaderClass.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, file.toURI().toURL());
    } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:net.minecrell.serverlistplus.canary.SnakeYAML.java

@SneakyThrows
private static void loadJAR(Path path) {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(SnakeYAML.class.getClassLoader(), path.toUri().toURL());
}

From source file:com.clustercontrol.plugin.ClassUtils.java

/**
 * ??URLCLASSPATH??<br/>/*from www .  j  a  va  2 s .  c  om*/
 * @param ?URL
 * @throws IOException
 */
public static void addURL(URL u) throws IOException {
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    for (URL url : sysLoader.getURLs()) {
        if (url.toString().equals(u.toString())) {
            log.info("URL " + u + " is already in CLASSPATH");
            return;
        }
    }

    Class<URLClassLoader> sysClass = URLClassLoader.class;
    try {
        Method method = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Throwable t) {
        log.warn(t.getMessage(), t);
        throw new IOException("could not add URL " + u + " to CLASSPATH");
    }
}

From source file:Main.java

/**
 * Calling the convertToTranslucent method on platforms after Android 5.0
 *///from  w w w  .j a v a2  s.  c om
private static void convertActivityToTranslucentAfterL(Activity activity) {
    try {
        Method getActivityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
        getActivityOptions.setAccessible(true);
        Object options = getActivityOptions.invoke(activity);

        Class<?>[] classes = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        for (Class clazz : classes) {
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
            }
        }
        Method convertToTranslucent = Activity.class.getDeclaredMethod("convertToTranslucent",
                translucentConversionListenerClazz, ActivityOptions.class);
        convertToTranslucent.setAccessible(true);
        convertToTranslucent.invoke(activity, null, options);
    } catch (Throwable t) {
    }
}

From source file:Main.java

public static void setRefreshing(SwipeRefreshLayout refreshLayout, boolean refreshing, boolean notify) {
    Class<? extends SwipeRefreshLayout> refreshLayoutClass = refreshLayout.getClass();
    if (refreshLayoutClass != null) {
        try {/*  w  w  w  . j  av  a 2 s  . c o m*/
            Method setRefreshing = refreshLayoutClass.getDeclaredMethod("setRefreshing", boolean.class,
                    boolean.class);
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(refreshLayout, refreshing, notify);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.networknt.info.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(classLoader, url);// w  ww. ja  va2  s.com
}

From source file:Main.java

public static String readRegistryValue(boolean user, String path, String key) throws Exception {
    final Class clz = Class.forName("java.util.prefs.WindowsPreferences");
    final Field f = clz.getDeclaredField(user ? "userRoot" : "systemRoot");

    f.setAccessible(true);//w  w  w  .j a va  2  s . co  m

    final Object root = f.get(null);
    final Method openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);

    openKey.setAccessible(true);

    final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);

    closeKey.setAccessible(true);

    final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);

    winRegQueryValue.setAccessible(true);

    byte[] valb = null;
    String vals = null;
    Integer handle = -1;

    handle = (Integer) openKey.invoke(root, toCSTR(path), 0x20019, 0x20019);
    valb = (byte[]) winRegQueryValue.invoke(root, handle, toCSTR(key));
    vals = ((valb != null) ? new String(valb).trim() : null);
    closeKey.invoke(root, handle);

    return vals;
}

From source file:Main.java

/**
 * Called by the {@link android.preference.PreferenceManager} to dispatch a subactivity result.
 *///  w  ww.  ja v a  2s. c  o  m
public static void dispatchActivityResult(@NonNull PreferenceManager manager, int requestCode, int resultCode,
        Intent data) {
    try {
        Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class,
                Intent.class);
        m.setAccessible(true);
        m.invoke(manager, requestCode, resultCode, data);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}