8166400: JShell: friendlier representation of array values
authorrfield
Wed, 21 Sep 2016 23:46:06 -0700
changeset 41158 957f4fa38bd6
parent 41157 b235a429089a
child 41159 06ac98da3f82
8166400: JShell: friendlier representation of array values Reviewed-by: sundar, jlahoda
langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java
langtools/test/jdk/jshell/SimpleRegressionTest.java
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java	Wed Sep 21 18:18:50 2016 -0700
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java	Wed Sep 21 23:46:06 2016 -0700
@@ -24,6 +24,7 @@
  */
 package jdk.jshell.execution;
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -200,6 +201,21 @@
             return "\"" + (String) value + "\"";
         } else if (value instanceof Character) {
             return "'" + value + "'";
+        } else if (value.getClass().isArray()) {
+            String tn = value.getClass().getTypeName();
+            int len = Array.getLength(value);
+            StringBuilder sb = new StringBuilder();
+            sb.append(tn.substring(tn.lastIndexOf('.') + 1, tn.length() - 1));
+            sb.append(len);
+            sb.append("] { ");
+            for (int i = 0; i < len; ++i) {
+                sb.append(valueString(Array.get(value, i)));
+                if (i < len - 1) {
+                    sb.append(", ");
+                }
+            }
+            sb.append(" }");
+            return sb.toString();
         } else {
             return value.toString();
         }
--- a/langtools/test/jdk/jshell/SimpleRegressionTest.java	Wed Sep 21 18:18:50 2016 -0700
+++ b/langtools/test/jdk/jshell/SimpleRegressionTest.java	Wed Sep 21 23:46:06 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /*
- * @test 8130450 8158906 8154374
+ * @test 8130450 8158906 8154374 8166400
  * @summary simple regression test
  * @build KullaTesting TestingInputStream
  * @run testng SimpleRegressionTest
@@ -149,4 +149,21 @@
         assertEval("class C {}");
         assertEval("C.class.getClassLoader() == Thread.currentThread().getContextClassLoader()", "true");
     }
+
+    public void testArayRepresentation() {
+        assertEval("new int[4]", "int[4] { 0, 0, 0, 0 }");
+        assertEval("new int[0]", "int[0] {  }");
+        assertEval("new byte[2]", "byte[2] { 0, 0 }");
+        assertEval("new short[] { 1234, 4321 }", "short[2] { 1234, 4321 }");
+        assertEval("new long[] { 123456789 }", "long[1] { 123456789 }");
+        assertEval("new float[] { -23.5645f, 1.0101f }", "float[2] { -23.5645, 1.0101 }");
+        assertEval("new double[] { 1/8, Math.PI }", "double[2] { 0.0, 3.141592653589793 }");
+        assertEval("new String[] { \"hi\", \"low\", null }", "String[3] { \"hi\", \"low\", null }");
+        assertEval("new char[] { 'a', 34, 77 }", "char[3] { 'a', '\"', 'M' }");
+        assertEval("new boolean[] { false, true }", "boolean[2] { false, true }");
+        assertEval("new int[][] { new int[] {44, 55}, new int[] {88,99}}",
+                "int[][2] { int[2] { 44, 55 }, int[2] { 88, 99 } }");
+        assertEval("new Object[] { \"howdy\", new int[] { 33, 44, 55 }, new String[] { \"up\", \"down\" }}",
+                "Object[3] { \"howdy\", int[3] { 33, 44, 55 }, String[2] { \"up\", \"down\" } }");
+    }
 }