Java 如何获取方法的调用方类

Java 如何获取方法的调用方类,java,Java,我如何知道哪个类称为方法 class A { B b = new B(); public void methodA() { Class callerClass = b.getCallerCalss(); // it should be 'A' class } } class B { public Class getCallerCalss() { //... ??? return clazz; } } 请尝试Throwable.getStackTr

我如何知道哪个类称为方法

class A {
   B b = new B();

   public void methodA() {
    Class callerClass = b.getCallerCalss(); // it should be 'A' class
   } 
}

class B {
 public Class getCallerCalss() {
   //... ???
   return clazz;
 }
}

请尝试
Throwable.getStackTrace()

创建一个新的可丢弃的
。。你不必扔它:)

未经测试:

Throwable t = new Throwable();
StackTraceElement[] es = t.getStackTrace();
// Not sure if es[0] would contain the caller, or es[1]. My guess is es[1].
System.out.println( es[0].getClass() + " or " + es[1].getClass() + " called me.");

显然,如果要创建某个函数(
getCaller()
),则必须在堆栈跟踪中再上一层。

尝试
Throwable.getStackTrace()

创建一个新的可丢弃的
。。你不必扔它:)

未经测试:

Throwable t = new Throwable();
StackTraceElement[] es = t.getStackTrace();
// Not sure if es[0] would contain the caller, or es[1]. My guess is es[1].
System.out.println( es[0].getClass() + " or " + es[1].getClass() + " called me.");

显然,如果要创建某个函数(
getCaller()
),则必须在堆栈跟踪中再上一层。

有一种观察堆栈跟踪的方法

StackTraceElement[] elements = Thread.currentThread().getStackTrace()

数组的最后一个元素表示堆栈的底部,它是序列中最近的方法调用


有一种观察stacktrace的方法

StackTraceElement[] elements = Thread.currentThread().getStackTrace()

数组的最后一个元素表示堆栈的底部,它是序列中最近的方法调用


您可以通过获取堆栈跟踪的第二个元素来获取调用方类的类名:

final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
System.out.println(stackTrace[1].getClassName());

类的方法返回一个
字符串
,因此很遗憾,您不会得到
对象。

您可以通过获取堆栈跟踪的第二个元素来获取调用方类的类名:

final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
System.out.println(stackTrace[1].getClassName());

类的方法返回一个
字符串
,因此不幸的是,您不会得到
对象。

这可以通过
线程.currentThread().getStackTrace()
轻松完成

publicstaticvoidmain(字符串[]args){
doSomething();
}
专用静态无效剂量仪(){
System.out.println(getCallerClass());
}
私有静态类getCallerClass(){
final StackTraceElement[]stackTrace=Thread.currentThread().getStackTrace();
字符串clazzName=stackTrace[3]。getClassName();
试一试{
返回Class.forName(clazzName);
}catch(classnotfounde异常){
e、 printStackTrace();
返回null;
}
}

之所以使用
[3]
,是因为
[0]
线程的元素。currentThread()
[1]
getCallerClass
[2]
doSomething
的元素,最后,
[3]
main
。如果将
doSomething
放在另一个类中,您将看到它返回正确的类。

这可以通过
Thread.currentThread().getStackTrace()轻松完成

publicstaticvoidmain(字符串[]args){
doSomething();
}
专用静态无效剂量仪(){
System.out.println(getCallerClass());
}
私有静态类getCallerClass(){
final StackTraceElement[]stackTrace=Thread.currentThread().getStackTrace();
字符串clazzName=stackTrace[3]。getClassName();
试一试{
返回Class.forName(clazzName);
}catch(classnotfounde异常){
e、 printStackTrace();
返回null;
}
}

之所以使用
[3]
,是因为
[0]
线程的元素。currentThread()
[1]
getCallerClass
[2]
doSomething
的元素,最后,
[3]
main
。如果将
doSomething
放入另一个类中,您将看到它返回正确的类。

是否可以将类作为参数传递给getCallerClass()?关于编辑。-是 啊对不起。随需应变。。比如重构。。TDD类。将类作为参数传递-这是可能的,但我想不是很好。也许一些有反射的魔法可以帮助“如何做谁”“计算”!=“班级”!=“clazz”这个问题可能会有帮助:是否可以将类作为参数传递给getCallerClass()?关于编辑。-是 啊对不起。随需应变。。比如重构。。TDD类。将类作为参数传递-这是可能的,但我想不是很好。也许一些有反射的魔法可以帮助“如何做谁”“计算”!=“班级”!=“clazz”这个问题可能会有帮助:为什么它“不是很好”?基于检查堆栈跟踪来执行逻辑是一种可疑的设计,但如果你能接受性能的影响,这种方法似乎很好。@millimoose,我的意思是,这种场景对我来说更像是一个
设计问题。为什么它“不是很好”?基于检查堆栈跟踪来执行逻辑是一种可疑的设计,但如果您能接受性能的影响,这种方法似乎很好。@millimoose,我的意思是该场景对我来说更像是一个
设计
问题。是的,您是对的-但是您需要
堆栈跟踪[1]
堆栈跟踪[0]。getClassName()
返回
java.lang.Thread
。很好!谢谢,这是明智的方式!:)是的,你是对的-但是你需要
stackTrace[1]
stackTrace[0]。getClassName()
返回
java.lang.Thread
。很好!谢谢,这是明智的方式!:)