如何从该方法中获取方法名称?

发布于 2024-10-30 17:46:47 字数 343 浏览 0 评论 0原文

我正在尝试创建一个从该方法中返回方法名称的函数:

  public static String getMethodName(final int depth)
  {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    return ste[ste.length - 1 - depth].getMethodName();
  }

但是,当我从 Activity.onCreate() 调用此方法时,它返回“main”而不是“onCreate”。

如何从该方法中获取实际的方法名称?

I am trying to create a function that returns the method name from within that method:

  public static String getMethodName(final int depth)
  {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    return ste[ste.length - 1 - depth].getMethodName();
  }

However, when I call this method from Activity.onCreate(), it returns "main" instead of "onCreate".

How do I get the actual method name from within that method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森罗 2024-11-06 17:46:47
return ste[1+depth].getMethodName();

如果您如上所述更改 return 语句,您将立即调用方法名称,当然深度应该为零。

return ste[1+depth].getMethodName();

If you change return statement as above, you would get immediate calling method name , of cource depth shoould be zero..

放肆 2024-11-06 17:46:47

尽管事实上启动异常是更昂贵的方式,但无论如何我都会这样做。

Log.d("CurrentMethod", new Exception().getStackTrace()[0].getMethodName());

如果在 onCreate 中调用则有效。

Despite the fact initiating an Exception is more expensive way, I would do it anyway.

Log.d("CurrentMethod", new Exception().getStackTrace()[0].getMethodName());

Works if called in onCreate.

撩起发的微风 2024-11-06 17:46:47

管理日志

public class ActiveLog {
public static final String TAG = "TRACE LOG";
private static ActiveLog instance;
private static boolean actif;

public static ActiveLog getInstance() {
    if (null == instance) 
        instance = new ActiveLog();
    return instance;
}

private ActiveLog() {
    ActiveLog.setActif(true);
}

public void log() {
    if(isActif())
        Log.d(TAG, "" + (new Exception().getStackTrace()[1].getClassName())
                      + ": "
                      + (new Exception().getStackTrace()[1].getMethodName()));
}

public static boolean isActif() {
    return actif;
}

public static void setActif(boolean actif) {
    ActiveLog.actif = actif;
}}

的单例: 使用示例:

public class MyTest {
  public void test() {
    ActiveLog.getInstance().log();
  }
}

结果:

09-05 14:37:09.822: D/TRACE LOG(XXXX): com.TestProject.MyTest: test

A singleton to manage logs:

public class ActiveLog {
public static final String TAG = "TRACE LOG";
private static ActiveLog instance;
private static boolean actif;

public static ActiveLog getInstance() {
    if (null == instance) 
        instance = new ActiveLog();
    return instance;
}

private ActiveLog() {
    ActiveLog.setActif(true);
}

public void log() {
    if(isActif())
        Log.d(TAG, "" + (new Exception().getStackTrace()[1].getClassName())
                      + ": "
                      + (new Exception().getStackTrace()[1].getMethodName()));
}

public static boolean isActif() {
    return actif;
}

public static void setActif(boolean actif) {
    ActiveLog.actif = actif;
}}

An example of use:

public class MyTest {
  public void test() {
    ActiveLog.getInstance().log();
  }
}

The result:

09-05 14:37:09.822: D/TRACE LOG(XXXX): com.TestProject.MyTest: test
顾冷 2024-11-06 17:46:47

我认为你的问题可能是你颠倒了访问堆栈。在返回值中,元素 0 是最近的调用(即 getStackTrace())。我认为您打算做的是:

public static String getMethodName(final int depth) {
  final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
  return ste[1 + depth].getMethodName();
}

这将访问堆栈中最近的调用(在调用 getStackTrace() 之外)。例如,如果您有一个方法:

public void foo() {
  System.out.println(getMethodName(0));
}

这将使用上述函数的实现打印“foo”。当然,您可能还想向函数添加一些边界检查,因为它可以轻松地超出数组。

I think your problem maybe you are accessing the stack upside down. In the returned value element 0 is the most recent call (which would be getStackTrace()). I think what you are intending to do is:

public static String getMethodName(final int depth) {
  final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
  return ste[1 + depth].getMethodName();
}

This will access the most recent call in the stack (outside of the call to getStackTrace()). For example if you have a method:

public void foo() {
  System.out.println(getMethodName(0));
}

This will print "foo" with the above implementation of the function. Of course you may also want to add some bounds checking to the function since it could easily go outside the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文