2013-12-22 3 views
0

클래스의 이름을 얻을 수있는 것처럼 메소드의 이름을 얻는 방법은 무엇입니까? (RandomClass.class.getName())class.getDeclaredMethod를 사용하지 않고 메소드의 이름을 얻는 방법

난독 화가가 손상시켜 하드 코딩이 작동하지 않습니다.

이유 :이 같은 내가 주입하고있어 방법 : 나는 메소드 이름을 알고 같은 종류의 사 바르 같이가 없기 때문에

 MethodNode getLocalPlayer = GetterAdapter.insert(false, true, "getLocalPlayer", "Lvanquish/accessors/Player;", "client", "yD", "LQZ;"); 
     classNode.methods.add(getLocalPlayer); 

     //class client implements my interface which contains the method getLocalPlayer 

     public interface Client { 

     public int[] getPlayerIndices(); 

     public Player getLocalPlayer(); 

     public Player[] getPlayers(); 

     public int getBaseX(); 

     public int getBaseY(); 

     public int getCameraX(); 

     public int getCameraY(); 

     } 

     //when I obfuscate my files getLocalPlayer get's a name like a2 
     //when you look above you see that the method name was hard code and so 
     // will it create an error 

선언 방법은 늘 여기에 작동합니다.

이 것이 작동할까요, 아니면 엉망이 될까요?

@DataMap.varDetails(name = "getPlayerIndices") 
public int[] getPlayerIndices(); 
+3

예외를 throw하고 catch하여 스택 추적을 확인할 수 있습니다. 농담하는 것만으로도 ... – eran

+0

http://stackoverflow.com/questions/3864175/how-to-get-the-name-of-method-in-current-class?rq=1 – MGorgon

+1

왜 필요한가요? 에? (이것은 [XY 문제] (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)와 비슷합니다.) –

답변

0

당신은 반사적으로 같은 것을 할 참조 할 각 방법을 필요 : @eran이 질문에 대한 첫번째 코멘트에서 알 수 있듯이

static String baseXName; 

public int getBaseX() { 
    if (baseXName == null) 
    baseXName = detectMyName(); 
    ... the actual code of the method ... 
} 

detectMyName(),를 인스턴스화 것 Exception을 호출하고 스택 추적에서 호출하는 메소드의 실제 런타임 이름을 검색합니다. Thread.currentThread().getStackTrace()에 전화하면 예외를 인스턴스화하지 않고 스택 추적을 얻음으로써 약간의 오버 헤드를 피할 수 있습니다.

0

이 시도 할 수 있습니다 :

Thread.currentThread().getStackTrace(); 

그것이 현재의 thread로 당신에게 당신의 전화 방법의 스택을 보여줍니다.
행운을 빕니다.

관련 문제