Skip to content

Commit

Permalink
BAEL-608 - Introduction to Java9 StackWalking AP (#1329)
Browse files Browse the repository at this point in the history
* yasin.bhojawala@gmail.com

Evaluation article on Different Types of Bean Injection in Spring

* Revert "yasin.bhojawala@gmail.com"

This reverts commit 963cc51.

* 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
  • Loading branch information
yasin3061 authored and maibin committed Mar 9, 2017
1 parent 6ea120d commit 00d4212
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,68 @@ public void methodTwo() {
}

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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
public class StackWalkerDemoTest {

@Test
public void walkTheStack() {
public void giveStalkWalker_whenWalkingTheStack_thenShowStackFrames() {
new StackWalkerDemo().methodOne();
}

@Test
public void giveStalkWalker_whenInvokingFindCaller_thenFindCallingClass() {
new StackWalkerDemo().findCaller();
}
}

0 comments on commit 00d4212

Please sign in to comment.