8161969: jshell tool: /var value is not truncated per feedback setting
authorrfield
Thu, 03 Nov 2016 09:12:02 -0700
changeset 41937 1313399705e9
parent 41936 94139f291be4
child 41938 8e66bf10fcec
8161969: jshell tool: /var value is not truncated per feedback setting 8166637: jshell tool: confusing truncation of long result values 8154513: JShell tool: welcome message should match feedback mode 8167552: jshell tool: Typo in jshell command '/? /reload' description Reviewed-by: jlahoda
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Feedback.java
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties
langtools/test/jdk/jshell/ToolFormatTest.java
langtools/test/jdk/jshell/ToolRetainTest.java
langtools/test/jdk/jshell/ToolSimpleTest.java
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Feedback.java	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/Feedback.java	Thu Nov 03 09:12:02 2016 -0700
@@ -116,6 +116,10 @@
                 name, type, value, unresolved, errorLines);
     }
 
+    public String truncateVarValue(String value) {
+        return mode.truncateVarValue(value);
+    }
+
     public String getPrompt(String nextId) {
         return mode.getPrompt(nextId);
     }
@@ -416,6 +420,45 @@
             return sb.toString();
         }
 
+        String truncateVarValue(String value) {
+            return truncateValue(value,
+                    bits(FormatCase.VARVALUE, FormatAction.ADDED,
+                            FormatWhen.PRIMARY, FormatResolve.OK,
+                            FormatUnresolved.UNRESOLVED0, FormatErrors.ERROR0));
+        }
+
+        String truncateValue(String value, long bits) {
+            if (value==null) {
+                return "";
+            } else {
+                // Retrieve the truncation length
+                String truncField = format(TRUNCATION_FIELD, bits);
+                if (truncField.isEmpty()) {
+                    // No truncation set, use whole value
+                    return value;
+                } else {
+                    // Convert truncation length to int
+                    // this is safe since it has been tested before it is set
+                    int trunc = Integer.parseUnsignedInt(truncField);
+                    int len = value.length();
+                    if (len > trunc) {
+                        if (trunc <= 13) {
+                            // Very short truncations have no room for "..."
+                            return value.substring(0, trunc);
+                        } else {
+                            // Normal truncation, make total length equal truncation length
+                            int endLen = trunc / 3;
+                            int startLen = trunc - 5 - endLen;
+                            return value.substring(0, startLen) + " ... " + value.substring(len -endLen);
+                        }
+                    } else {
+                        // Within truncation length, use whole value
+                        return value;
+                    }
+                }
+            }
+        }
+
         // Compute the display output given full context and values
         String format(FormatCase fc, FormatAction fa, FormatWhen fw,
                     FormatResolve fr, FormatUnresolved fu, FormatErrors fe,
@@ -425,33 +468,7 @@
             String fname = name==null? "" : name;
             String ftype = type==null? "" : type;
             // Compute the representation of value
-            String fvalue;
-            if (value==null) {
-                fvalue = "";
-            } else {
-                // Retrieve the truncation length
-                String truncField = format(TRUNCATION_FIELD, bits);
-                if (truncField.isEmpty()) {
-                    // No truncation set, use whole value
-                    fvalue = value;
-                } else {
-                    // Convert truncation length to int
-                    // this is safe since it has been tested before it is set
-                    int trunc = Integer.parseUnsignedInt(truncField);
-                    if (value.length() > trunc) {
-                        if (trunc <= 5) {
-                            // Very short truncations have no room for "..."
-                            fvalue = value.substring(0, trunc);
-                        } else {
-                            // Normal truncation, make total length equal truncation length
-                            fvalue = value.substring(0, trunc - 4) + " ...";
-                        }
-                    } else {
-                        // Within truncation length, use whole value
-                        fvalue = value;
-                    }
-                }
-            }
+            String fvalue = truncateValue(value, bits);
             String funresolved = unresolved==null? "" : unresolved;
             String errors = errorLines.stream()
                     .map(el -> String.format(
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Thu Nov 03 09:12:02 2016 -0700
@@ -526,7 +526,7 @@
             runFile(loadFile, "jshell");
         }
 
-        if (regenerateOnDeath) {
+        if (regenerateOnDeath && feedback.shouldDisplayCommandFluff()) {
             hardmsg("jshell.msg.welcome", version());
         }
 
@@ -2240,7 +2240,7 @@
         stream.forEachOrdered(vk ->
         {
             String val = state.status(vk) == Status.VALID
-                    ? state.varValue(vk)
+                    ? feedback.truncateVarValue(state.varValue(vk))
                     : getResourceString("jshell.msg.vars.not.active");
             hard("  %s %s = %s", vk.typeName(), vk.name(), val);
         });
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/resources/l10n.properties	Thu Nov 03 09:12:02 2016 -0700
@@ -338,7 +338,7 @@
      Reset and replay the valid history between the previous and most\n\t\
      recent time that jshell was entered, or a /reset, or /reload\n\t\
      command was executed. This can thus be used to restore a previous\n\t\
-     jshell tool sesson.\n\n\
+     jshell tool session.\n\n\
 /reload [-restore] -quiet\n\t\
      With the '-quiet' argument the replay is not shown.  Errors will display.
 
--- a/langtools/test/jdk/jshell/ToolFormatTest.java	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/test/jdk/jshell/ToolFormatTest.java	Thu Nov 03 09:12:02 2016 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8148316 8148317 8151755 8152246 8153551 8154812 8157261 8163840
+ * @bug 8148316 8148317 8151755 8152246 8153551 8154812 8157261 8163840 8166637 8161969
  * @summary Tests for output customization
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -220,8 +220,8 @@
             test(
                     (a) -> assertCommandOutputStartsWith(a, "/set feedback normal", ""),
                     (a) -> assertCommand(a, "String s = java.util.stream.IntStream.range(65, 74)"+
-                            ".mapToObj(i -> \"\"+(char)i).reduce((a,b) -> a + b + a).get()",
-                            "s ==> \"ABACABADABACABAEABACABADABACABAFABACABADABACABAEABACABADABACABAGABACABADABA ..."),
+                            ".mapToObj(i -> \"\"+(char)i).reduce((a,b) -> a + b + a).get() + \"XYZ\"",
+                            "s ==> \"ABACABADABACABAEABACABADABACABAFABACABADABACABAE ... BACABAEABACABADABACABAXYZ\""),
                     (a) -> assertCommandOutputStartsWith(a, "/set mode test -quiet", ""),
                     (a) -> assertCommandOutputStartsWith(a, "/set feedback test", ""),
                     (a) -> assertCommand(a, "/set format test display '{type}:{value}' primary", ""),
@@ -234,8 +234,9 @@
                             "/set truncation test 10 varvalue"),
                     (a) -> assertCommandOutputContains(a, "/set truncation test",
                             "/set truncation test 10 varvalue"),
-                    (a) -> assertCommand(a, "String r = s", "String:\"ABACABADABACABA ..."),
-                    (a) -> assertCommand(a, "r", "String:\"ABACA ..."),
+                    (a) -> assertCommand(a, "/var", "|    String s = \"ABACABADA"),
+                    (a) -> assertCommand(a, "String r = s", "String:\"ABACABAD ... BAXYZ\""),
+                    (a) -> assertCommand(a, "r", "String:\"ABACABADA"),
                     (a) -> assertCommand(a, "r=s", "String:\"AB")
             );
         } finally {
--- a/langtools/test/jdk/jshell/ToolRetainTest.java	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/test/jdk/jshell/ToolRetainTest.java	Thu Nov 03 09:12:02 2016 -0700
@@ -23,13 +23,14 @@
 
 /*
  * @test
- * @bug 8157200 8163840
+ * @bug 8157200 8163840 8154513
  * @summary Tests of what information is retained across jshell tool runs
  * @modules jdk.jshell/jdk.internal.jshell.tool
  * @build ToolRetainTest ReplToolTesting
  * @run testng ToolRetainTest
  */
 
+import java.util.Locale;
 import org.testng.annotations.Test;
 
 @Test
@@ -62,14 +63,14 @@
                 (a) -> assertCommand(a, "/set mode -retain trm1", ""),
                 (a) -> assertCommand(a, "/exit", "")
         );
-        test(
+        test(Locale.ROOT, true, new String[0], "",
                 (a) -> assertCommand(a, "/set mode trm2 -quiet", ""),
                 (a) -> assertCommand(a, "/set format trm2 display '{name}={value}'", ""),
                 (a) -> assertCommand(a, "int x = 45", "x:45"),
                 (a) -> assertCommand(a, "/set mode -retain trm2", ""),
                 (a) -> assertCommand(a, "/exit", "")
         );
-        test(
+        test(Locale.ROOT, true, new String[0], "",
                 (a) -> assertCommandOutputContains(a, "/set mode trm1",
                         "/set format trm1 display \"{name}:{value}\""),
                 (a) -> assertCommand(a, "/set format trm2 display",
--- a/langtools/test/jdk/jshell/ToolSimpleTest.java	Wed Nov 02 15:42:20 2016 -0700
+++ b/langtools/test/jdk/jshell/ToolSimpleTest.java	Thu Nov 03 09:12:02 2016 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128
+ * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513
  * @summary Simple jshell tool tests
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -35,6 +35,7 @@
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -465,14 +466,14 @@
     }
 
     public void testOptionQ() {
-        test(new String[]{"-q", "--no-startup"},
+        test(Locale.ROOT, false, new String[]{"-q", "--no-startup"}, "",
                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
                 (a) -> assertCommand(a, "int x = 5", "")
         );
     }
 
     public void testOptionS() {
-        test(new String[]{"-s", "--no-startup"},
+        test(Locale.ROOT, false, new String[]{"-s", "--no-startup"}, "",
                 (a) -> assertCommand(a, "1+1", "")
         );
     }
@@ -486,7 +487,7 @@
     }
 
     public void testOptionFeedback() {
-        test(new String[]{"--feedback", "concise", "--no-startup"},
+        test(Locale.ROOT, false, new String[]{"--feedback", "concise", "--no-startup"}, "",
                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
                 (a) -> assertCommand(a, "int x = 5", "")
         );
@@ -498,17 +499,17 @@
                             .filter(l -> !l.isEmpty())
                             .count(), "Expected no lines: " + s);
                 };
-        test(new String[]{"-nq"},
+        test(Locale.ROOT, false, new String[]{"-nq"}, "",
                 (a) -> assertCommandCheckOutput(a, "/list -all", confirmNoStartup),
                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
                 (a) -> assertCommand(a, "int x = 5", "")
         );
-        test(new String[]{"-qn"},
+        test(Locale.ROOT, false, new String[]{"-qn"}, "",
                 (a) -> assertCommandCheckOutput(a, "/list -all", confirmNoStartup),
                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
                 (a) -> assertCommand(a, "int x = 5", "")
         );
-        test(new String[]{"-ns"},
+        test(Locale.ROOT, false, new String[]{"-ns"}, "",
                 (a) -> assertCommandCheckOutput(a, "/list -all", confirmNoStartup),
                 (a) -> assertCommand(a, "1+1", "")
         );