티스토리 뷰

Language/Java

[Java]현재 Method 확인

out of coding 2015. 9. 7. 17:27

현재 메소드명 알아내기


Java 1.4

new Exception().getStackTrace()[0].getMethodName();


Java 1.5

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

for(StackTraceElement element : elements) {

boolean doNext = element.getMethodName().equals([Method명]);

if(doNext) {

System.out.println(element.getMethodName());

return;

}

}


* 현재 Class와 MethodName 출력

StackTraceElement[] stacks = new Throwable().getStackTrace();

StackTraceElement currentStack = stacks[0];

System.out.println("Class : " + currentStack.getClassName());

System.out.println("Method : " + currentStack.getMethodName());  

 

* 바로전의 Class와 MethodName 출력

StackTraceElement[] stacks = new Throwable().getStackTrace();

StackTraceElement beforeStack = stacks[1];

System.out.println( "before Class : " + beforeStack.getClassName() );

System.out.println( "before Method : " + beforeStack.getMethodName() );  

 

* 호출된 StackTrace 갯수

StackTraceElement[] stacks = new Throwable().getStackTrace();

System.out.println("stacks size : " + stacks.length);

'Language > Java' 카테고리의 다른 글

Java 코딩 규칙  (0) 2018.01.08
[Java] Java 버전 분류  (0) 2016.12.20
[Java]Exception 발생시키기  (0) 2015.04.02
[Java]System.arraycopy  (0) 2015.01.23
[Java]정규표현식  (0) 2015.01.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함