博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取当前正在执行的方法的名称
阅读量:2290 次
发布时间:2019-05-09

本文共 6266 字,大约阅读时间需要 20 分钟。

本文翻译自:

有没有办法在Java中获取当前正在执行的方法的名称?


#1楼

参考:


#2楼

Use the following Code : 使用以下代码:

StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();    StackTraceElement e = stacktrace[1];//coz 0th will be getStackTrace so 1st    String methodName = e.getMethodName();    System.out.println(methodName);

#3楼

The fastest way I found is that: 我找到的最快的方法是:

import java.lang.reflect.Method;public class TraceHelper {    // save it static to have it available on every call    private static Method m;    static {        try {            m = Throwable.class.getDeclaredMethod("getStackTraceElement",                    int.class);            m.setAccessible(true);        } catch (Exception e) {            e.printStackTrace();        }    }    public static String getMethodName(final int depth) {        try {            StackTraceElement element = (StackTraceElement) m.invoke(                    new Throwable(), depth + 1);            return element.getMethodName();        } catch (Exception e) {            e.printStackTrace();            return null;        }    }}

It accesses the native method getStackTraceElement(int depth) directly. 它直接访问本机方法getStackTraceElement(int depth)。 And stores the accessible Method in a static variable. 并将可访问的Method存储在静态变量中。


#4楼

This is an expansion on (above). 这是对的扩展(上图)。

It provides some static methods to get the current and invoking class / method names. 它提供了一些静态方法来获取当前和调用类/方法名称。

/* Utility class: Getting the name of the current executing method  * https://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method *  * Provides:  *  *      getCurrentClassName() *      getCurrentMethodName() *      getCurrentFileName() *  *      getInvokingClassName() *      getInvokingMethodName() *      getInvokingFileName() * * Nb. Using StackTrace's to get this info is expensive. There are more optimised ways to obtain * method names. See other stackoverflow posts eg. https://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection/2924426#2924426 * * 29/09/2012 (lem) - added methods to return (1) fully qualified names and (2) invoking class/method names */package com.stackoverflow.util;public class StackTraceInfo{    /* (Lifted from virgo47's stackoverflow answer) */    private static final int CLIENT_CODE_STACK_INDEX;    static {        // Finds out the index of "this code" in the returned stack trace - funny but it differs in JDK 1.5 and 1.6        int i = 0;        for (StackTraceElement ste: Thread.currentThread().getStackTrace())        {            i++;            if (ste.getClassName().equals(StackTraceInfo.class.getName()))            {                break;            }        }        CLIENT_CODE_STACK_INDEX = i;    }    public static String getCurrentMethodName()    {        return getCurrentMethodName(1);     // making additional overloaded method call requires +1 offset    }    private static String getCurrentMethodName(int offset)    {        return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getMethodName();    }    public static String getCurrentClassName()    {        return getCurrentClassName(1);      // making additional overloaded method call requires +1 offset    }    private static String getCurrentClassName(int offset)    {    return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getClassName();    }    public static String getCurrentFileName()    {        return getCurrentFileName(1);     // making additional overloaded method call requires +1 offset    }    private static String getCurrentFileName(int offset)    {        String filename = Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getFileName();        int lineNumber = Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getLineNumber();        return filename + ":" + lineNumber;    }    public static String getInvokingMethodName()    {        return getInvokingMethodName(2);     }    private static String getInvokingMethodName(int offset)    {        return getCurrentMethodName(offset + 1);    // re-uses getCurrentMethodName() with desired index    }    public static String getInvokingClassName()    {        return getInvokingClassName(2);     }    private static String getInvokingClassName(int offset)    {        return getCurrentClassName(offset + 1);     // re-uses getCurrentClassName() with desired index    }    public static String getInvokingFileName()    {        return getInvokingFileName(2);     }    private static String getInvokingFileName(int offset)    {        return getCurrentFileName(offset + 1);     // re-uses getCurrentFileName() with desired index    }    public static String getCurrentMethodNameFqn()    {        return getCurrentMethodNameFqn(1);    }    private static String getCurrentMethodNameFqn(int offset)    {        String currentClassName = getCurrentClassName(offset + 1);        String currentMethodName = getCurrentMethodName(offset + 1);        return currentClassName + "." + currentMethodName ;    }    public static String getCurrentFileNameFqn()    {        String CurrentMethodNameFqn = getCurrentMethodNameFqn(1);        String currentFileName = getCurrentFileName(1);        return CurrentMethodNameFqn + "(" + currentFileName + ")";    }    public static String getInvokingMethodNameFqn()    {        return getInvokingMethodNameFqn(2);    }    private static String getInvokingMethodNameFqn(int offset)    {        String invokingClassName = getInvokingClassName(offset + 1);        String invokingMethodName = getInvokingMethodName(offset + 1);        return invokingClassName + "." + invokingMethodName;    }    public static String getInvokingFileNameFqn()    {        String invokingMethodNameFqn = getInvokingMethodNameFqn(2);        String invokingFileName = getInvokingFileName(2);        return invokingMethodNameFqn + "(" + invokingFileName + ")";    }}

#5楼

public class SomeClass {   public void foo(){      class Local {};      String name = Local.class.getEnclosingMethod().getName();   } }

name will have value foo. 名称将具有值foo。


#6楼

String methodName =Thread.currentThread().getStackTrace()[1].getMethodName();System.out.println("methodName = " + methodName);

转载地址:http://cvcnb.baihongyu.com/

你可能感兴趣的文章
MySQL主从同步
查看>>
MySQL半同步复制
查看>>
MySQL主库宕机从库提权
查看>>
MySQL主主模式
查看>>
MySQL错误代码
查看>>
MySQL binlog的三种模式
查看>>
MySQL利用binlog增量恢复数据库
查看>>
Tomcat多实例多应用
查看>>
Tomcat启动慢解决方法
查看>>
Tomca主配置文件详解
查看>>
Tomcat创建虚拟主机
查看>>
Tomcat集群
查看>>
Tomcat DeltaManager集群共享session
查看>>
Tomcat连接Apache之mod_proxy模块
查看>>
sersync+rsync数据同步
查看>>
使用com.aspose.words将word模板转为PDF文件时乱码解决方法
查看>>
Linux发送邮件
查看>>
YUM安装PHP5.6
查看>>
YUM源安装MySQL5.7
查看>>
Tomcat日志切割cronolog
查看>>