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 main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];

    field.setAccessible(true);// w  w  w .  j a v a2s .  c  om
    constructor.setAccessible(true);
    method.setAccessible(true);

}

From source file:javarestart.JavaRestartLauncher.java

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Usage: <URL> {<MainClass>}");
        return;// www. j av  a 2  s.  c om
    }

    if (args[0].equals("fork")) {
        String[] args2 = new String[args.length - 1];
        for (int i = 0; i < args.length - 1; i++) {
            args2[i] = args[i + 1];
        }
        fork(args2);
        return;
    }

    AppClassloader loader = new AppClassloader(args[0]);
    Thread.currentThread().setContextClassLoader(loader);
    String main;
    JSONObject obj = getJSON(args[0]);
    if (args.length < 2) {
        main = (String) obj.get("main");
    } else {
        main = args[1];
    }

    String splash = (String) obj.get("splash");
    if (splash != null) {
        SplashScreen scr = SplashScreen.getSplashScreen();
        if (scr != null) {
            URL url = loader.getResource(splash);
            scr.setImageURL(url);
        }
    }

    //auto close splash after 45 seconds
    Thread splashClose = new Thread() {
        @Override
        public void run() {
            try {
                sleep(45000);
            } catch (InterruptedException e) {
            }
            SplashScreen scr = SplashScreen.getSplashScreen();
            if ((scr != null) && (scr.isVisible())) {
                scr.close();
            }
        }
    };
    splashClose.setDaemon(true);
    splashClose.start();

    Class mainClass = loader.loadClass(main);
    Method mainMethod = mainClass.getMethod("main", String[].class);
    mainMethod.setAccessible(true);
    mainMethod.invoke(null, new Object[] { new String[0] });
}

From source file:Deet.java

public static void main(String... args) {
    if (args.length != 4) {
        err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n");
        return;/*from  www .  j a  v a  2 s .  c om*/
    }

    try {
        Class<?> c = Class.forName(args[0]);
        Object t = c.newInstance();

        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            String mname = m.getName();
            if (!mname.startsWith("test") || (m.getGenericReturnType() != boolean.class)) {
                continue;
            }
            Type[] pType = m.getGenericParameterTypes();
            if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) {
                continue;
            }

            out.format("invoking %s()%n", mname);
            try {
                m.setAccessible(true);
                Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
                out.format("%s() returned %b%n", mname, (Boolean) o);

                // Handle any exceptions thrown by method to be invoked.
            } catch (InvocationTargetException x) {
                Throwable cause = x.getCause();
                err.format("invocation of %s failed: %s%n", mname, cause.getMessage());
            }
        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

private static Object invokeGetter(Method getter, Object object) {
    try {//from   ww  w. j ava 2  s  . co  m
        getter.setAccessible(true);
        return getter.invoke(object, new Object[0]);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static void init(MediaPlayer mp) {
    Method method = getMediadataMethod();
    method.setAccessible(true);
    try {/*from ww  w  .j  a va  2 s  .c o m*/
        data = method.invoke(mp, METADATA_ALL, BYPASS_METADATA_FILTER);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        data = null;
    } catch (Exception e) {
        data = null;
    }
}

From source file:Main.java

public static Method getPrivateMethod(Object target, String methodName, Class<?>... parameterTypes)
        throws NoSuchMethodException {
    Class targetClass = target.getClass();
    Method method = targetClass.getDeclaredMethod(methodName, parameterTypes);
    method.setAccessible(true);
    return method;
}

From source file:Main.java

public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... argTypes) {
    try {/*from   w  ww.  j a  v a  2s .  c  o m*/
        final Method method = clazz.getDeclaredMethod(name, argTypes);
        method.setAccessible(true);
        return method;
    } catch (final SecurityException e) {
        throw new RuntimeException(e);
    } catch (final NoSuchMethodException e) {
        return null;
    }
}

From source file:Main.java

private static Method loadMethod(String className, String methodName) {
    try {/*w  w w .ja v  a  2  s  .  co m*/
        Method method = Class.forName(className).getMethod(methodName);
        method.setAccessible(true);
        return method;
    } catch (NoSuchMethodException ex) {
        return null; // the method was not found
    } catch (SecurityException ex) {
        return null; // setAccessible not allowed by security policy
    } catch (ClassNotFoundException ex) {
        return null; // the direct buffer implementation was not found
    }
}

From source file:Main.java

public static void forceConvertActivityFromTranslucent(Activity activity) {
    try {//from  www  .j  av  a  2  s  .  c  o  m
        Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
        method.setAccessible(true);
        method.invoke(activity);
    } catch (Throwable ignored) {
    }
}

From source file:Main.java

private static void addtoClassLoader(URL url) throws IOException, NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    Method method = sysclass.getDeclaredMethod("addURL", parameters);
    method.setAccessible(true);
    method.invoke(sysloader, new Object[] { url });
}