獲取呼叫當前方法的類名

2022-01-30 09:27:39 字數 871 閱讀 7174

第一種常規方式:

stacktraceelement stack = (new

throwable()).getstacktrace();

for (int i = 0; i < stack.length; i++)

view code

stacktraceelement stack = (new

throwable()).getstacktrace();

stacktraceelement s = stack[0];

system.out.println("呼叫當前當前方法的類名:"+s.getclassname());

system.out.println("呼叫當前當前方法的方法名:"+s.getmethodname());

system.out.println("呼叫當前當前方法的檔名:"+s.getfilename());

system.out.println("呼叫當前當前方法的行數:"+s.getlinenumber());

system.out.println("呼叫當前當前方法的類:"+s.getclass());

view code

第二種懶人方式:

string name = thread.currentthread().getstacktrace()[1].getclassname();

system.out.println("呼叫當前方法的類名"+name);

view code

第三種方式:

this.getclass()
使用方法:

直接新增在當前方法中即可。

獲取呼叫當前方法的方法

thread.currentthread getstacktrace 1 是你當前方法執行堆疊 thread.currentthread getstacktrace 2 就是上一級的方法堆疊 以此類推 stacktraceelement temp thread.currentthread getst...

java獲取當前方法的名稱

string clazz this.getclass getname 獲取當前類的 類名 string method thread.currentthread getstacktrace 1 getmethodname 獲取當前方法 返回乙個陣列 thread.currentthread getst...

PHP獲取當前類名 方法名

class 獲取當前類名 function 當前函式名 confirm method 當前方法名 bankcard confirm function 函式名稱 php 4.3.0 新加 自 php 5 起本常量返回該函式被定義時的名字 區分大小寫 在 php 4 中該值總是小寫字母的。class 類...