import java.util.*;
import java.io.*;
public class mytest
{
public void inferCaller() {
// Get the stack trace.
StackTraceElement stack[] = (new Throwable()).getStackTrace();
// First, search back to a method in the Logger class.
int ix = 0;
while (ix < stack.length) {
StackTraceElement frame = stack[ix];
String cname = frame.getClassName();
if (cname.equals(getClass().getName())) //当前的类正在被call的method
{
System.out.println("ix="+ix);
break;
}
System.out.println(cname+"类:"+frame.getMethodName());
ix++;
}
//ix=0;
// Now search for the first frame before the "Logger" class.
while (ix < stack.length) {
StackTraceElement frame = stack[ix];
String cname = frame.getClassName();
if (!cname.equals(getClass().getName()))
{
// We've found the relevant frame.
//在当前类之前的类是调用自己的类.
System.out.println(cname+":"+frame.getMethodName());
}
ix++;
}
}
}