8080501: javaarrayconversion.js test is flawed
authorsundar
Tue, 15 Sep 2015 19:31:24 +0530
changeset 32693 7da64fc12993
parent 32692 8f60bd284bf4
child 32694 da2f35ab2ea6
8080501: javaarrayconversion.js test is flawed Reviewed-by: attila, hannesw
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJava.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Messages.properties
nashorn/test/script/basic/javaarrayconversion.js
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJava.java	Mon Sep 14 16:13:10 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJava.java	Tue Sep 15 19:31:24 2015 +0530
@@ -345,7 +345,8 @@
     /**
      * Given a script object and a Java type, converts the script object into the desired Java type. Currently it
      * performs shallow creation of Java arrays, as well as wrapping of objects in Lists, Dequeues, Queues,
-     * and Collections. Example:
+     * and Collections. If conversion is not possible or fails for some reason, TypeError is thrown.
+     * Example:
      * <pre>
      * var anArray = [1, "13", false]
      * var javaIntArray = Java.to(anArray, "int[]")
@@ -389,7 +390,11 @@
         }
 
         if(targetClass.isArray()) {
-            return JSType.toJavaArray(obj, targetClass.getComponentType());
+            try {
+                return JSType.toJavaArray(obj, targetClass.getComponentType());
+            } catch (final Exception exp) {
+                throw typeError(exp, "java.array.conversion.failed", targetClass.getName());
+            }
         }
 
         if (targetClass == List.class || targetClass == Deque.class || targetClass == Queue.class || targetClass == Collection.class) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Sep 14 16:13:10 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Messages.properties	Tue Sep 15 19:31:24 2015 +0530
@@ -150,6 +150,7 @@
 type.error.method.not.constructor=Java method {0} cannot be used as a constructor.
 type.error.env.not.object=$ENV must be an Object.
 type.error.unsupported.java.to.type=Unsupported Java.to target type {0}.
+type.error.java.array.conversion.failed=Java.to conversion to array type {0} failed
 type.error.constructor.requires.new=Constructor {0} requires "new".
 type.error.new.on.nonpublic.javatype=new cannot be used with non-public java type {0}.
 
--- a/nashorn/test/script/basic/javaarrayconversion.js	Mon Sep 14 16:13:10 2015 +0530
+++ b/nashorn/test/script/basic/javaarrayconversion.js	Tue Sep 15 19:31:24 2015 +0530
@@ -128,24 +128,32 @@
 // Converting to string, toString takes precedence over valueOf
 test({ valueOf: function() { return "42"; },  toString: function() { return "43"; } }, "java.lang.String", "43")
 
+function assertCanConvert(sourceType, targetType) {
+  Java.to([new (Java.type(sourceType))()], targetType + "[]")
+  ++testCount;
+}
+
 function assertCantConvert(sourceType, targetType) {
   try {
-    Java.to([new Java.type(sourceType)()], targetType + "[]")
+    Java.to([new (Java.type(sourceType))()], targetType + "[]")
     throw "no TypeError encountered"
   } catch(e) {
-      if(!(e instanceof TypeError)) {
+      if(!(e instanceof TypeError) ||
+          !e.message.startsWith("Java.to conversion to array type")) {
         throw e;
       }
       ++testCount;
   }
 }
 
+// Arbitrary POJOs to JS Primitive type should work
+assertCanConvert("java.util.BitSet", "int")
+assertCanConvert("java.util.BitSet", "double")
+assertCanConvert("java.util.BitSet", "long")
+assertCanConvert("java.util.BitSet", "boolean")
+assertCanConvert("java.util.BitSet", "java.lang.String")
+
 // Arbitrary POJOs can't be converted to Java values
-assertCantConvert("java.util.BitSet", "int")
-assertCantConvert("java.util.BitSet", "double")
-assertCantConvert("java.util.BitSet", "long")
-assertCantConvert("java.util.BitSet", "boolean")
-assertCantConvert("java.util.BitSet", "java.lang.String")
 assertCantConvert("java.util.BitSet", "java.lang.Double")
 assertCantConvert("java.util.BitSet", "java.lang.Long")