Maroun
Maroun

Reputation: 95958

Convert variable to its original form in JShell

I'm playing with the JShell and trying to convert a variable to its original form. To do that, I reset everything in the REPL using /r, then I imported java.util.* and created a list:

-> import java.util.*;

-> List<String> l = new ArrayList<>(Arrays.asList("a", "b"));
|  Modified variable l of type List<String> with initial value [a, b]

Now I'm trying to convert the lists values to upper case, so I'm doing:

-> l.replaceAll(String::toUpperCase)

-> l
|  Variable l of type List<String> has value [A, B]

Listing the executed source I've typed using /list (or /l) shows:

-> /l

   1 : List<String> l = new ArrayList<>(Arrays.asList("a", "b"));
   2 : l.replaceAll(String::toUpperCase)
   3 : l

Now when I try to reset the list to stage 1 (before changing its values), I'm getting the import statement:

-> /1
import java.util.*;

Does anyone know why this happens? I tried the same without the import statement, but I'm getting the same result (I assume this is because it's getting imported explicitly).


I've just notice that if I write /l all I get:

-> /l all

s1 : import java.util.*;
s2 : import java.io.*;
s3 : import java.math.*;
s4 : import java.net.*;
s5 : import java.util.concurrent.*;
s6 : import java.util.prefs.*;
s7 : import java.util.regex.*;
s8 : void printf(String format, Object... args) { System.out.printf(format, args); }
 1 : String a = "a";
 2 : a = "b"

I don't know why /1 executes the first import statement and not first the string assignment. Also it's really weird that even if import java.util.*; is there, s5 is import java.util.concurrent.*; (which is clearly redundant).

Upvotes: 4

Views: 172

Answers (1)

Maroun
Maroun

Reputation: 95958

Edit

Looks like the problem was solved in JDK 9 EA build 107 on 03-01-2016 (#4560).


After hours of digging, I found an explanation. The command:

/list all

lists all executions, including the start-up entries that were silently and automatically executed before the REPL started:

s1 : import java.util.*;
s2 : import java.io.*;
s3 : import java.math.*;
s4 : import java.net.*;
s5 : import java.util.concurrent.*;
s6 : import java.util.prefs.*;
s7 : import java.util.regex.*;
s8 : void printf(String format, Object... args) { System.out.printf(format, args); }
 1 : String a = "a";
 2 : a = "b"

One solution is using the /setstart command and pass it a file that includes the entries you specify. This way you will not have the default imports and the handy print method available for you.

The other solution would be writing /9 (the state that's right after the last silent entry).

To be honest, I'm not satisfied with neither of the solutions, I hope there will be better one soon.

Upvotes: 2

Related Questions