BAEL-608 - Introduction to Java9 StackWalking AP (#1329)
* yasin.bhojawala@gmail.com Evaluation article on Different Types of Bean Injection in Spring * Revert "yasin.bhojawala@gmail.com" This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032. * Fixing compilation error and removing unused import * Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com Code examples for the article "Introduction to Java9 StackWalking API" * BAEL-608 Introduction to Java9 StackWalking API * BAEL-608 Introduction to Java9 StackWalking API changing the test names to BDD style * BAEL-608 Introduction to Java9 StackWalking API correcting the typo * BAEL-608 Introduction to Java9 StackWalking API improving method names * BAEL-608 Introduction to Java9 StackWalking API test method names improvements
This commit is contained in:
parent
6ea120d67f
commit
00d42129ef
|
@ -16,48 +16,68 @@ public class StackWalkerDemo {
|
|||
}
|
||||
|
||||
public void methodThree() {
|
||||
List<StackFrame> stackTrace = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
|
||||
.walk(StackWalkerDemo::walkExample);
|
||||
List<StackFrame> stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
|
||||
.walk(StackWalkerDemo::walkExample2);
|
||||
stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample2);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
String line = StackWalker.getInstance()
|
||||
.walk(StackWalkerDemo::walkExample3);
|
||||
String line = StackWalker.getInstance().walk(this::walkExample3);
|
||||
System.out.println(line);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance(StackWalker.Option.SHOW_REFLECT_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
Runnable r = () -> {
|
||||
List<StackFrame> stackTrace2 = StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
printStackTrace(stackTrace2);
|
||||
};
|
||||
r.run();
|
||||
}
|
||||
|
||||
private static List<StackFrame> walkExample(Stream<StackFrame> stackFrameStream) {
|
||||
public List<StackFrame> walkExample(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<StackFrame> walkExample2(Stream<StackFrame> stackFrameStream) {
|
||||
public List<StackFrame> walkExample2(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static String walkExample3(Stream<StackFrame> stackFrameStream) {
|
||||
public String walkExample3(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung")
|
||||
&& frame.getClassName()
|
||||
.endsWith("Test"))
|
||||
.endsWith("Test"))
|
||||
.findFirst()
|
||||
.map(frame -> frame.getClassName() + "#" + frame.getMethodName() + ", Line " + frame.getLineNumber())
|
||||
.orElse("Unknown caller");
|
||||
}
|
||||
|
||||
public void findCaller() {
|
||||
Class<?> caller = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
|
||||
System.out.println(caller.getCanonicalName());
|
||||
}
|
||||
|
||||
private void printStackTrace(List<StackFrame> stackTrace) {
|
||||
public void printStackTrace(List<StackFrame> stackTrace) {
|
||||
for (StackFrame stackFrame : stackTrace) {
|
||||
System.out.println(stackFrame.getDeclaringClass()
|
||||
System.out.println(stackFrame.getClassName()
|
||||
.toString() + "#" + stackFrame.getMethodName() + ", Line " + stackFrame.getLineNumber());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,12 @@ import org.junit.Test;
|
|||
public class StackWalkerDemoTest {
|
||||
|
||||
@Test
|
||||
public void walkTheStack() {
|
||||
public void giveStalkWalker_whenWalkingTheStack_thenShowStackFrames() {
|
||||
new StackWalkerDemo().methodOne();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giveStalkWalker_whenInvokingFindCaller_thenFindCallingClass() {
|
||||
new StackWalkerDemo().findCaller();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue