Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Java/reflection: Difference between revisions

From ZeroWiki
imported>rabierre
No edit summary
imported>rabierre
No edit summary
Line 2: Line 2:


= 코드 =
= 코드 =
~java
/**
*/
  public class Test {
  public class Test {
   
   

Revision as of 14:33, 18 January 2012

설명

코드

~java
/**
*/
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);

    }
}