我想獲取Scala應用程序中的函數內的調用方類和方法名稱。我目前正在使用堆棧跟蹤,但性能下降。我做這樣的事情獲取來電者類和方法名稱
stackTrace(CodeDepth).getClassName
stackTrace(CodeDepth).getMethodName
我已經找到了Java反射多很多更快,但我只能用
sun.reflect.Reflection.getCallerClass(CodeDepth).getName()
獲取類名是有沒有辦法獲得方法名(和可選的行號)通過反射?也許使用Scala的反射?
感謝
我認爲這是你正在尋找的東西:http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html – nafas
可能的重複[Get callers method(java.lang .reflect.Method)](http://stackoverflow.com/questions/4024587/get-callers-method-java-lang-reflect-method) –
@nafas不,使用Throwable也很貴。請參閱http://stackoverflow.com/questions/421280/how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection以供參考。 – sebi