本文研究的主要是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() => falsethis's method name is main
以上就是本文关于Java StackTraceElement实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。