changeset 9394:251198e174cd

8141386: Unable to pass values to java functions which takes wrapper objects as arguments Reviewed-by: ghb, kcr, azvegint Contributed-by: Arunprasad Rajkumar <arunprasad.rajkumar@oracle.com>
author kcr
date Fri, 20 Nov 2015 14:50:42 -0800
parents 4c14b9f7e350
children 172c329b1a0c
files modules/web/src/main/native/Source/WebCore/bridge/jni/jsc/JNIUtilityPrivate.cpp modules/web/src/test/java/javafx/scene/web/JavaScriptBridgeTest.java
diffstat 2 files changed, 67 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/modules/web/src/main/native/Source/WebCore/bridge/jni/jsc/JNIUtilityPrivate.cpp	Thu Nov 19 09:52:56 2015 -0800
+++ b/modules/web/src/main/native/Source/WebCore/bridge/jni/jsc/JNIUtilityPrivate.cpp	Fri Nov 20 14:50:42 2015 -0800
@@ -248,27 +248,27 @@
                 }
             }
 
-            // Create an appropriate Java object if target type is java.lang.Object.
-            if (!result.l && !strcmp(javaClassName, "java.lang.Object")) {
-                if (value.isString()) {
+            // Create an appropriate Java object if target type is java.lang.Object or other wrapper Objects {Integer, Double, Boolean}.
+            if (!result.l) {
+                if (value.isString() && !strcmp(javaClassName, "java.lang.Object")) {
                     String stringValue = asString(value)->value(exec);
                     JNIEnv* env = getJNIEnv();
                     jobject javaString = env->functions->NewString(env, (const jchar*)stringValue.deprecatedCharacters(), stringValue.length());
                     result.l = javaString;
                 } else if (value.isNumber()) {
                     JNIEnv* env = getJNIEnv();
-                    if (value.isInt32()) {
+                    if (value.isInt32() && (!strcmp(javaClassName, "java.lang.Number") || !strcmp(javaClassName, "java.lang.Integer") || !strcmp(javaClassName, "java.lang.Object"))) {
                         static JGClass clazz(env->FindClass("java/lang/Integer"));
                         jmethodID meth = env->GetStaticMethodID(clazz, "valueOf", "(I)Ljava/lang/Integer;");
                         result.l = env->CallStaticObjectMethod(clazz, meth, (jint) value.asInt32());
-                    } else {
+                    } else if (!strcmp(javaClassName, "java.lang.Number") || !strcmp(javaClassName, "java.lang.Double") || !strcmp(javaClassName, "java.lang.Object")) {
                         jdouble doubleValue = (jdouble) value.asNumber();
                         static JGClass clazz = env->FindClass("java/lang/Double");
                         jmethodID meth = env->GetStaticMethodID(clazz, "valueOf", "(D)Ljava/lang/Double;");
                         jobject javaDouble = env->CallStaticObjectMethod(clazz, meth, doubleValue);
                         result.l = javaDouble;
                     }
-                } else if (value.isBoolean()) {
+                } else if (value.isBoolean() && (!strcmp(javaClassName, "java.lang.Boolean") || !strcmp(javaClassName, "java.lang.Object"))) {
                     bool boolValue = value.asBoolean();
                     JNIEnv* env = getJNIEnv();
                     static JGClass clazz(env->FindClass("java/lang/Boolean"));
--- a/modules/web/src/test/java/javafx/scene/web/JavaScriptBridgeTest.java	Thu Nov 19 09:52:56 2015 -0800
+++ b/modules/web/src/test/java/javafx/scene/web/JavaScriptBridgeTest.java	Fri Nov 20 14:50:42 2015 -0800
@@ -315,6 +315,67 @@
         });
     }
 
+    // JDK-8141386
+    public static class WrapperObjects {
+        public Number n0; // using setter
+        public Number n1; // direct access
+        public Double d0; // using setter
+        public Double d1; // direct access
+        public Integer i0; // using setter
+        public Integer i1; // direct access
+        public Boolean b0; // using setter
+        public Boolean b1; // direct access
+
+        public void setNumberVal(Number n) {
+            n0 = n;
+        }
+
+        public void setDoubleVal(Double d) {
+            d0 = d;
+        }
+
+        public void setIntegerVal(Integer i) {
+            i0 = i;
+        }
+
+        public void setBooleanVal(Boolean b) {
+            b0 = b;
+        }
+    }
+
+    public @Test void testMethodCallWithWrapperObjects() {
+        final WebEngine web = getEngine();
+
+        submit(() -> {
+            WrapperObjects obj = new WrapperObjects();
+            bind("obj", obj);
+            // Test java.lang.Number
+            web.executeScript("obj.setNumberVal(1.23)");
+            assertEquals(1.23, obj.n0.doubleValue(), 0.1);
+            web.executeScript("obj.n1 = 1.23");
+            assertEquals(1.23, obj.n1.doubleValue(), 0.1);
+            // Test java.lang.Double
+            web.executeScript("obj.setDoubleVal(1.23)");
+            assertEquals(1.23, obj.d0, 0.1);
+            web.executeScript("obj.d1 = 1.23");
+            assertEquals(1.23, obj.d1, 0.1);
+            // Test java.lang.Integer
+            web.executeScript("obj.setIntegerVal(123)");
+            assertEquals(123, obj.i0.intValue());
+            web.executeScript("obj.i1 = 123");
+            assertEquals(123, obj.i1.intValue());
+            // Test java.lang.Boolean
+            web.executeScript("obj.setBooleanVal(true)");
+            assertEquals(true, obj.b0.booleanValue());
+            web.executeScript("obj.setBooleanVal(false)");
+            assertEquals(false, obj.b0.booleanValue());
+            web.executeScript("obj.b1 = true");
+            assertEquals(true, obj.b1.booleanValue());
+            web.executeScript("obj.b1 = false");
+            assertEquals(false, obj.b1.booleanValue());
+        });
+    }
+
     public @Test void testBridgeExplicitOverloading() throws InterruptedException {
         final WebEngine web = getEngine();