Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAEL-608 - Introduction to Java9 StackWalking AP #1329

Merged
merged 11 commits into from
Mar 9, 2017
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();
}
}