关于栈方法和反射的理解

 

import java.lang.reflect.InvocationTargetException;

public class LegacyStackWalk {
    public static void main(String[] args) {
        m1();
    }
    public static void m1() {
        m2();
    }
    public static void m2() {
        // Call m3() directly
        System.out.println("\nWithout using reflection: ");
       // m3("3","4");
        m3("3","4");
        // Call m3() using reflection        
        try {
            System.out.println("\nUsing reflection: ");
            LegacyStackWalk.class
                         .getMethod("m3",String.class,String.class)
                         .invoke(/*new LegacyStackWalk()*/null,new Object[]{"1","2"} );
        } catch (NoSuchMethodException |  
                 InvocationTargetException |
                 IllegalAccessException |
                 SecurityException e) {
            e.printStackTrace();
        }        
    }
    public static void m3(String a,String b) {
        System.out.println("a+b="+a+b);
        // Prints the call stack details
        StackTraceElement[] frames = new Throwable().getStackTrace();
        for(StackTraceElement frame : frames) {
            System.out.println(frame.toString());
        }
    }
}
 

你可能感兴趣的:(Test)