U E D R , A S I H C RSS

java/reflection (rev. 1.1)

java/reflection

public class Test {

    public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
        ClassLoader classLoader = Test.class.getClassLoader();
        System.out.println(classLoader.getClass().getName());

        URLClassLoader urlClassLoader = new URLClassLoader(
            new URL[]{
                new URL("file:/Users/fharenheit/Projects/openflamingo/helloworld.jar")
            }, classLoader
        );
        System.out.println(Thread.currentThread().getContextClassLoader().getClass().getName());
        Thread.currentThread().setContextClassLoader(urlClassLoader);
        System.out.println(Thread.currentThread().getContextClassLoader().getClass().getName());

        Class<?> helloWorld = ClassUtils.getClass("HelloWorld");
        Object o = helloWorld.newInstance();

        Method[] declaredMethods = helloWorld.getDeclaredMethods();
        for (int i = 0; i < declaredMethods.length; i++) {
            Method declaredMethod = declaredMethods[i];
            System.out.println(declaredMethod.getName());
        }

        Method sayHello = o.getClass().getMethod("sayHello", null);
        sayHello.invoke(o, null);

    }
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:42
Processing time 0.0117 sec