Search code examples
javajshell

why does jshell return empty String using StackWalker


Is it an Error? I would at least expect an Exception instead of empty String for:

jshell> ((Supplier<String>)(()->StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName())).get()+"invisible";
$1 ==> ""

using jshell 13.0.1


Solution

  • I have no explanation, but it seems to be due to some weird behavior of the shell feedback.

    jshell> String a = ((Supplier<String>)(() -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass().getName())).get();
    a ==> ""
    
    jshell> System.out.println(a);
    REPL.$JShell$11
    
    jshell> "$JShell$foobar"
    $3 ==> ""
    

    JShell seems to interprete the String $Jshell$... somehow, but the String itself is just fine.