运行中获取当前执行位置所在类、所在函数等信息
2013-09-14 10:53
246 查看
目的:为方便Debug,需要在运行时确定当前执行位置的上下文信息,如确定当前执行代码所在类、所在文件,所在函数、所在行等。
思路:
1、使用Java的栈跟踪API。
2、使用类反射机制。
具体实现:
思路1:
代码示例:
思路2:
实例代码:
参考资料:
1、Getting the name of the current executing method
思路:
1、使用Java的栈跟踪API。
2、使用类反射机制。
具体实现:
思路1:
代码示例:
/* Utility class: Getting the name of the current executing method * http://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. http://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 */ 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 + ")"; } }
思路2:
实例代码:
String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); String packageName = new Object(){}.getClass().getPackage().getName();
参考资料:
1、Getting the name of the current executing method
相关文章推荐
- JAVA 获取当前执行的函数名、当前运行的类名等等信息
- JAVA 获取当前执行的函数名、当前运行的类名等等信息
- JAVA 获取当前执行的函数名、当前运行的类名等等信息
- 在Linux下获取当前运行程序可执行文件全路径(不使用real_path()函数)
- python 获取当前位置所在的函数名和行号
- C++ 获取当前正在执行的函数的相关信息
- C++ 获取当前正在执行的函数的相关信息
- 运行时如何获取运行当前代码所在的函数,一个神奇的宏
- python 获取当前位置所在的函数名和行号
- python中获取当前位置所在的行号和函数名
- python中获取当前位置所在的行号和函数名(转)
- 在python中获取当前位置所在的行号和函数名
- 在python中获取当前位置所在的行号和函数名
- python中获取当前位置所在的行号和函数名(转)
- 【python系列】python 获取当前位置所在的文件名、函数名和行号
- 在python中获取当前位置所在的行号和函数名
- C++ 获取当前正在执行的函数的相关信息
- FW:在python中获取当前位置所在的行号和函数名
- C++ 获取当前正在执行的函数的相关信息
- C++ 获取当前正在执行的函数的相关信息