是不是这样? package pack;public class ThreadTest{ public static void main(String[] args) { b b1 = new b(); b1.method(); System.out.println("*******************"); runMethod(); }
public static void runMethod() { new b().method(); } }class b{ public void method() { try { Thread thr = Thread.currentThread(); StackTraceElement[] ele = thr.getStackTrace();
package pack;public class ThreadTest{
public static void main(String[] args) {
b b1 = new b();
b1.method();
System.out.println("*******************");
runMethod();
}
public static void runMethod() {
new b().method();
}
}class b{
public void method() {
try {
Thread thr = Thread.currentThread();
StackTraceElement[] ele = thr.getStackTrace();
for (StackTraceElement e: ele) {
System.out.print("类:"+e.getClassName());
System.out.println("调用"+e.getMethodName()+"方法");
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
不知JAVA里有没有这类函数直接可以找到调用的类?
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.go();
} public void go() {
new MethodClass().getMethodName();
}
}class MethodClass {
public void getMethodName() {
try {
Thread thr = Thread.currentThread();
StackTraceElement[] ele = thr.getStackTrace();
System.out.println(ele[2].getClassName() + "." + ele[2].getMethodName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}