获取运行时的堆栈信息

public class Hi {
	public static void main(String[] args) {
		Throwable ex = new Throwable();

		StackTraceElement[] stackElements = ex.getStackTrace();

		if (stackElements != null) {
			for (int i = 0; i < stackElements.length; i++) {
				System.out.println(stackElements[i].getClassName());
				System.out.println(stackElements[i].getFileName());
				System.out.println(stackElements[i].getLineNumber());
				System.out.println(stackElements[i].getMethodName());
				System.out.println("-----------------------------------");
			}
		}
	}

}

下面是对上面的封装
Class caller = Reflection.getCallerClass(5);
String path = caller.getProtectionDomain().getCodeSource()
				.getLocation().getFile();

你可能感兴趣的:(堆栈)