Get caller information from stack trace (java)

Get caller information from stack trace

When programming in java, there are times when you want to know where a method was called. In such a case, you can get the stack trace information and get the caller information.

Use Throwable # getStackTrace () to get the stack trace information.

StackTraceElement[] ste = new Throwable().getStackTrace();
for (int i = 0; i < ste.length; i++) {
	System.out.println(ste[i].getClassName()); //Get class name
	System.out.println(ste[i].getMethodName()); //Get method name
	System.out.println(ste[i].getFileName()); //Get the file name
	System.out.println(ste[i].getLineNumber()); //Get line number (cannot get in case of native method)
	System.out.println(ste[i].isNativeMethod()); //Determine if it is a native method.
	System.out.println(ste[i]); //Format and display stack trace information

You can also use Thread # getStackTrace () instead of Throwable # getStackTrace ().

StackTraceElement[] ste = Thread.currentThread().getStackTrace();

However, there are some differences in the stack trace information obtained by the two methods. Let's run the sample program and check it.


package sample;

public class Main1 {
	public static void main(String[] args) {
		SampleClass1 sc = new SampleClass1();

class SampleClass1 {
	public void method1() {
	public void method2() {
		System.out.println("Throwable#getStackTrace()Get stack trace information using");
		StackTraceElement[] ste = new Throwable().getStackTrace();
		for (int i = 0; i < ste.length; i++) {

		System.out.println("\nThread#getStackTrace()Get stack trace information using");
		ste = Thread.currentThread().getStackTrace();
		for (int i = 0; i < ste.length; i++) {

The result of executing the sample program is as follows.

Throwable#getStackTrace()Get stack trace information using

Thread#getStackTrace()Get stack trace information using

If you use Thread # getStackTrace (), the stack trace information will be increased by one line. It's not a big difference, so you can use either one.

Precautions when acquiring stack trace information

There is an upper limit to the stack trace information that can be acquired. In other words, the obtained StackTraceElement [] does not always contain all the stack trace information.

Let's check by running the following sample program.


package sample;

public class Main2 {
	public static void main(String[] args) {
		SampleClass2 sc = new SampleClass2();

class SampleClass2 {
	public void printStackTrace(int stackCount) {
		if (stackCount > 2) {
			printStackTrace(stackCount - 1);
		} else {
			StackTraceElement[] ste = new Throwable().getStackTrace();
			System.out.println("ste.length=" + ste.length);

The execution result depends on the JVM, but in my environment, the output is as follows.


You can see that the maximum number of stack trace information that can be acquired is 1024. In other words, it is not always possible to trace the caller back to the origin.

Recommended Posts

Get caller information from stack trace (java)
[Java] Get tag information from music files
Get EXIF information in Java
Log Java NullPointerException stack trace
Get country from IP address (Java)
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
Get history from Zabbix server in Java
Get block information with Hyperledger Iroha's Java SDK
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Library "OSHI" to get system information in Java
[Java] Get multiple values from one return value
[Java] Get a random value from an array
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Get a list of MBean information for Java applications
[Swift5] Get array (individual) information from JSON using library'SwiftyJSON'
Call Java from JRuby
Get location information quickly
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Get miscellaneous dates
Eval Java source from Java
Access API.AI from Java
[Java] Initialize, add, get
From Java to Ruby !!
HttpUrlConnection mysterious stack trace
Get Android location information
Get weather forecasts from Watson Weather Company Data in simple Java
[Java] Get Charset with Apathce Tika / Initialize String from Charset [Kotlin]
Get video information from Nikorepo and throw it to Slack
How to get the longest information from Twitter as of 12/12/2016
[Kotlin] Get Java Constructor / Method from KFunction and call it
Easily get an integer from a system property in Java
Get attributes and values from an XML file in Java
CData Software Hands-on (Get Twitter data from Java console application)
Get a non-empty collection from an Optional stream in java