亲宝软件园·资讯

展开

Java StackTraceElement Java StackTraceElement实例代码

司马懿字仲达 人气:0
想了解Java StackTraceElement实例代码的相关内容吗,司马懿字仲达在本文为您仔细讲解Java StackTraceElement的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,stack,trace,java,trace,element,java,获取stacktrace,java,stacktrace,下面大家一起来学习吧。

本文研究的主要是Java StackTraceElement的相关内容,具体介绍如下。

实例:

package com.demo.test;
public class StackTraceDemo {
	public static void testA() {
		testB();
	}
	public static void testB() {
		StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
		System.out.println("-------> StackTraceElement[]");
		for (int i = 0; i < stackTraceElements.length; i++) {
			System.out.println("-------> " + i);
			System.out.println(stackTraceElements[i]);
			System.out.println("StackTraceElement.getClassName() => " + stackTraceElements[i].getClassName());
			System.out.println("StackTraceElement.getFileName() => " + stackTraceElements[i].getFileName());
			System.out.println("StackTraceElement.getLineNumber() => " + stackTraceElements[i].getLineNumber());
			System.out.println("StackTraceElement.getMethodName() => " + stackTraceElements[i].getMethodName());
			System.out.println("StackTraceElement.isNativeMethod() => " + stackTraceElements[i].isNativeMethod());
		}
	}
	public static String getMethodName() {
		StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
		if(stackTraceElements.length > 2) {
			return stackTraceElements[2].getMethodName();
		}
		return null;
	}
	public static void main(String[] args) {
		testA();
		System.out.println("\nthis's method name is " + getMethodName());
	}
}

输出:

-------> StackTraceElement[]
-------> 0
java.lang.Thread.getStackTrace(Unknown Source)
StackTraceElement.getClassName() => java.lang.Thread
StackTraceElement.getFileName() => null
StackTraceElement.getLineNumber() => -1
StackTraceElement.getMethodName() => getStackTrace
StackTraceElement.isNativeMethod() => false
-------> 1
com.demo.test.StackTraceDemo.testB(StackTraceDemo.java:10)
StackTraceElement.getClassName() => com.demo.test.StackTraceDemo
StackTraceElement.getFileName() => StackTraceDemo.java
StackTraceElement.getLineNumber() => 10
StackTraceElement.getMethodName() => testB
StackTraceElement.isNativeMethod() => false
-------> 2
com.demo.test.StackTraceDemo.testA(StackTraceDemo.java:6)
StackTraceElement.getClassName() => com.demo.test.StackTraceDemo
StackTraceElement.getFileName() => StackTraceDemo.java
StackTraceElement.getLineNumber() => 6
StackTraceElement.getMethodName() => testA
StackTraceElement.isNativeMethod() => false
-------> 3
com.demo.test.StackTraceDemo.main(StackTraceDemo.java:32)
StackTraceElement.getClassName() => com.demo.test.StackTraceDemo
StackTraceElement.getFileName() => StackTraceDemo.java
StackTraceElement.getLineNumber() => 32
StackTraceElement.getMethodName() => main
StackTraceElement.isNativeMethod() => false

this's method name is main

总结

以上就是本文关于Java StackTraceElement实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论