Skip to content

Parses a java stack trace to access package, class, method, file, and line information of every stack trace element.

License

Notifications You must be signed in to change notification settings

alexscheitlin/java-stack-trace-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Stack Trace Parser

Parses a java stack trace to access package, class, method, file, and line information of every stack trace element.

Build Status JitPack Version License: MIT

How to Use

Add Dependency

Maven

  • Add the JitPack repository to your pom.xml file:
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>
  • Add the dependency:
	<dependency>
	    <groupId>com.github.alexscheitlin</groupId>
	    <artifactId>java-stack-trace-parser</artifactId>
	    <version>v1.0.1</version>
	</dependency>

Gradle

  • Add the JitPack repository to your build.gradle file:
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
  • Add the dependency:
	dependencies {
	        implementation 'com.github.alexscheitlin:java-stack-trace-parser:v1.0.1'
	}

Code Example

The StackTraceParser class parses a java stack trace (e.g. below) and maps it to a StackTrace object with java.lang.StackTraceElements.

The example below can be found here and the dummy stack trace here.

java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:86)
	at org.junit.Assert.assertTrue(Assert.java:41)
	at org.junit.Assert.assertTrue(Assert.java:52)
	at com.example.bank.BankAccountTest.getNumber(BankAccountTest.java:21)
	...

Parse Stack Trace

First read some dummy stack trace:

String stackTraceString = null;
try {
    stackTraceString = ResourceReader.readResourceFile("dummyStackTrace.txt");
} catch (FileNotFoundException e) {
    System.out.println("Could not read test resource file!");
    return;
}

Then parse the string to map it to a StackTrace object.

StackTrace stackTrace = null;
try {
    stackTrace = StackTraceParser.parse(stackTraceString);
} catch (Exception e) {
    System.out.println("Could not read test resource file!");
}

Access Error

To access the first stack trace line (the line indicating the caused error) use the following command:

String firstLine = stackTrace.getFirstLine();

Access Stack Trace Elements

To get detailed information about some stack trace line use the following command:

StackTraceElement stackLine = stackTrace.getStackTraceLines().get(0);

System.out.println("First line:\t" + firstLine);
System.out.println("Stack Line:\t" + stackLine.toString());
System.out.println("\tDeclaring class:\t" + stackLine.getClassName());
System.out.println("\tMethod name:\t\t" + stackLine.getMethodName());
System.out.println("\tFile name:\t\t\t" + stackLine.getFileName());
System.out.println("\tLine number:\t\t" + stackLine.getLineNumber());
System.out.println("\tIs Native Method:\t" + stackLine.isNativeMethod());

Expected output:

Stack Line:	org.junit.Assert.fail(Assert.java:86)
	Declaring class:	org.junit.Assert
	Method name:		fail
	File name:		Assert.java
	Line number:		86
	Is Native Method:	false

Original stack trace line: at org.junit.Assert.fail(Assert.java:86)

Get Stack Trace Lines of Package

To get stack trace lines of a specific package use the following command:

String packageName = "com.example.bank";
List<StackTraceElement> linesOfPackage = stackTrace.getLinesOfPackage(packageName);

for (StackTraceElement line : linesOfPackage) {
    System.out.println(line.toString());
}

Expected output:

com.example.bank.BankAccountTest.getNumber(BankAccountTest.java:21)

Reconstruct Stack Trace

To reconstruct the original stack trace use the following command:

System.out.println(stackTrace.getOriginalStackTrace());

Authors

License

This project is licensed under the MIT License.

About

Parses a java stack trace to access package, class, method, file, and line information of every stack trace element.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages