https://github.com/mtumilowicz/java8-stack-stackwalking
How to traverse stack in java 8 environtment.
https://github.com/mtumilowicz/java8-stack-stackwalking
java8 stack stack-walking
Last synced: 4 months ago
JSON representation
How to traverse stack in java 8 environtment.
- Host: GitHub
- URL: https://github.com/mtumilowicz/java8-stack-stackwalking
- Owner: mtumilowicz
- Created: 2019-02-18T11:53:16.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-18T18:41:34.000Z (almost 7 years ago)
- Last Synced: 2025-06-04T16:05:47.906Z (7 months ago)
- Topics: java8, stack, stack-walking
- Language: Java
- Homepage:
- Size: 58.6 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://travis-ci.com/mtumilowicz/java8-stack-stackwalking)
# java8-stack-stackwalking
# manual
* theory: https://github.com/mtumilowicz/java-stack
* `StackTraceElement` class represents a stack frame
* useful methods:
* `int getLineNumber()`
* `String getClassName()`
* `String getMethodName()`
* to get all frames of the current thread's stack
* first element of the array is the top frame in the stack (last method invocation)
* `StackTraceElement[] frames = new Throwable().getStackTrace();`
* `StackTraceElement[] frames = Thread.currentThread().getStackTrace();`
* drawbacks:
* `getStrackTrace()` returns entire stack - we may need only top 5 elements for example
* no easy way to filter
* `StackTraceElement` contains method names and class names, but we often need class references
# project description
1. for test purposes
```
class StackWalking {
void m1() {
m2();
}
private void m2() {
m3();
}
private void m3() {
System.out.println("---");
System.out.println("stack from m3():");
Arrays.stream(new Throwable().getStackTrace()) // getStackTrace on Throwable
.forEach(System.out::println);
System.out.println("---");
m4();
}
private void m4() {
System.out.println("---");
System.out.println("stack from m4():");
Arrays.stream(Thread.currentThread().getStackTrace()) // getStackTrace on Thread
.forEach(System.out::println);
System.out.println("---");
}
}
```
1. simulation
```
@Test
public void simulation() {
new StackWalking().m1();
}
```
produces:
```
---
stack from m3():
StackWalking.m3(StackWalking.java:18)
StackWalking.m2(StackWalking.java:12)
StackWalking.m1(StackWalking.java:8)
StackWalkingTest.simulation(StackWalkingTest.java:10)
... // other
---
---
stack from m4():
java.base/java.lang.Thread.getStackTrace(Thread.java:1606)
StackWalking.m4(StackWalking.java:28)
StackWalking.m3(StackWalking.java:22)
StackWalking.m2(StackWalking.java:12)
StackWalking.m1(StackWalking.java:8)
StackWalkingTest.simulation(StackWalkingTest.java:10)
.. // other
---
```