2013-12-22 42 views
0

如何才能获取方法的名称,就像我可以获取类的名称一样? (RandomClass.class.getName())如何在不使用class.getDeclaredMethod的情况下获取方法的名称

因为混淆器会破坏它,所以硬编码将不起作用。

原因: 我注射的方法是这样的:

​​

声明的方法不会在这里工作,因为我不知道的方法名称和有像4个瓦尔相同类型的。

这个工作还是会是一团糟?

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

您可以抛出一个异常,抓住它并查看堆栈跟踪。只是在开玩笑,不要... – 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

您需要每一个你想引用反思这样做的方法:

static String baseXName; 

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

detectMyName()会,作为@eran在你的问题第一意见建议,实例化Exception并从其堆栈跟踪中检索调用方法的实际运行时名称。如果您拨打Thread.currentThread().getStackTrace(),则可以通过获取堆栈跟踪而不实例化异常来避免一小部分开销。

0

你可以试试这个:

Thread.currentThread().getStackTrace(); 

它告诉你在当前线程调用的方法的堆栈。
祝您好运:)

相关问题