changeset 25258:325380d7c38c

8049242: Explicit constructor overload selection should work with StaticClass as well Reviewed-by: jlaskey, attila
author sundar
date Thu, 03 Jul 2014 23:03:37 +0530
parents c1c2eb19a90e
children aa64bc1a97d9 18a7c7d3c1ca
files nashorn/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java nashorn/src/jdk/internal/dynalink/beans/StaticClassLinker.java nashorn/test/script/basic/JDK-8043232.js nashorn/test/script/basic/JDK-8043232.js.EXPECTED nashorn/test/script/basic/JDK-8049242.js nashorn/test/script/basic/JDK-8049242.js.EXPECTED
diffstat 6 files changed, 128 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/nashorn/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java	Thu Jul 03 17:14:38 2014 +0200
+++ b/nashorn/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java	Thu Jul 03 23:03:37 2014 +0530
@@ -390,6 +390,10 @@
         return new GuardedInvocationComponent(invocation, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
     }
 
+    SingleDynamicMethod getConstructorMethod(final String signature) {
+        return null;
+    }
+
     private MethodHandle getAssignableGuard(final MethodType type) {
         return Guards.asType(assignableGuard, type);
     }
@@ -412,18 +416,18 @@
         return inv == null ? null : new GuardedInvocation(inv, getClassGuard(callSiteDescriptor.getMethodType()));
     }
 
-    private static MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
+    private MethodHandle getDynamicMethodInvocation(final CallSiteDescriptor callSiteDescriptor,
             final LinkerServices linkerServices, final String methodName, final Map<String, DynamicMethod> methodMap) {
         final DynamicMethod dynaMethod = getDynamicMethod(methodName, methodMap);
         return dynaMethod != null ? dynaMethod.getInvocation(callSiteDescriptor, linkerServices) : null;
     }
 
-    private static DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) {
+    private DynamicMethod getDynamicMethod(final String methodName, final Map<String, DynamicMethod> methodMap) {
         final DynamicMethod dynaMethod = methodMap.get(methodName);
         return dynaMethod != null ? dynaMethod : getExplicitSignatureDynamicMethod(methodName, methodMap);
     }
 
-    private static SingleDynamicMethod getExplicitSignatureDynamicMethod(final String methodName,
+    private SingleDynamicMethod getExplicitSignatureDynamicMethod(final String fullName,
             final Map<String, DynamicMethod> methodsMap) {
         // What's below is meant to support the "name(type, type, ...)" syntax that programmers can use in a method name
         // to manually pin down an exact overloaded variant. This is not usually required, as the overloaded method
@@ -433,23 +437,33 @@
         // for performance reasons.
 
         // Is the method name lexically of the form "name(types)"?
-        final int lastChar = methodName.length() - 1;
-        if(methodName.charAt(lastChar) != ')') {
+        final int lastChar = fullName.length() - 1;
+        if(fullName.charAt(lastChar) != ')') {
             return null;
         }
-        final int openBrace = methodName.indexOf('(');
+        final int openBrace = fullName.indexOf('(');
         if(openBrace == -1) {
             return null;
         }
 
+        final String name = fullName.substring(0, openBrace);
+        final String signature = fullName.substring(openBrace + 1, lastChar);
+
         // Find an existing method for the "name" part
-        final DynamicMethod simpleNamedMethod = methodsMap.get(methodName.substring(0, openBrace));
+        final DynamicMethod simpleNamedMethod = methodsMap.get(name);
         if(simpleNamedMethod == null) {
+            // explicit signature constructor access
+            // Java.type("java.awt.Color")["(int,int,int)"]
+            // will get Color(int,int,int) constructor of Color class.
+            if (name.isEmpty()) {
+                return getConstructorMethod(signature);
+            }
+
             return null;
         }
 
         // Try to get a narrowed dynamic method for the explicit parameter types.
-        return simpleNamedMethod.getMethodForExactParamTypes(methodName.substring(openBrace + 1, lastChar));
+        return simpleNamedMethod.getMethodForExactParamTypes(signature);
     }
 
     private static final MethodHandle IS_METHOD_HANDLE_NOT_NULL = Guards.isNotNull().asType(MethodType.methodType(
--- a/nashorn/src/jdk/internal/dynalink/beans/StaticClassLinker.java	Thu Jul 03 17:14:38 2014 +0200
+++ b/nashorn/src/jdk/internal/dynalink/beans/StaticClassLinker.java	Thu Jul 03 23:03:37 2014 +0530
@@ -161,8 +161,9 @@
             return null;
         }
 
-        DynamicMethod getConstructorMethod(final String signature) {
-            return constructor.getMethodForExactParamTypes(signature);
+        @Override
+        SingleDynamicMethod getConstructorMethod(final String signature) {
+            return constructor != null? constructor.getMethodForExactParamTypes(signature) : null;
         }
     }
 
--- a/nashorn/test/script/basic/JDK-8043232.js	Thu Jul 03 17:14:38 2014 +0200
+++ b/nashorn/test/script/basic/JDK-8043232.js	Thu Jul 03 23:03:37 2014 +0530
@@ -78,3 +78,15 @@
 
 // call constructor as normal method (without 'new')
 checkIt(function() Color());
+
+// try constructor on interface
+checkIt(function() new java.lang["Runnable()"]);
+checkIt(function() new java.lang["Runnable(int)"]);
+
+// try constructor on abstrace class
+try {
+    new java.io["InputStream()"];
+    throw new Error("should have thrown exception!");
+} catch (e) {
+    print(e);
+}
--- a/nashorn/test/script/basic/JDK-8043232.js.EXPECTED	Thu Jul 03 17:14:38 2014 +0200
+++ b/nashorn/test/script/basic/JDK-8043232.js.EXPECTED	Thu Jul 03 23:03:37 2014 +0530
@@ -9,3 +9,6 @@
 TypeError: Java method [jdk.internal.dynalink.beans.OverloadedDynamicMethod java.lang.System.getProperty] cant be used as a constructor.
 TypeError: Java method [jdk.internal.dynalink.beans.OverloadedDynamicMethod java.io.PrintStream.println] cant be used as a constructor.
 TypeError: Constructor [jdk.internal.dynalink.beans.SimpleDynamicMethod Color java.awt.Color.java.awt.Color(int,int,int)] requires new.
+TypeError: No such Java constructor: Runnable()
+TypeError: No such Java constructor: Runnable(int)
+java.lang.InstantiationException: java.io.InputStream
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8049242.js	Thu Jul 03 23:03:37 2014 +0530
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2014, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8049242: Explicit constructor overload selection should work with StaticClass as well
+ *
+ * @test
+ * @run
+ */
+
+// call explicit constructor
+print(new (Java.type("java.awt.Color")["(int,int,int)"])(255,0,255));
+// print the constructor itself
+print(Java.type("java.awt.Color")["(int,int,int)"]);
+
+// store constructor to call later
+var Color = Java.type("java.awt.Color")["(int,int,int)"];
+// call stored constructor
+print(new Color(33, 233, 2))
+
+// check if default constructor works
+var obj = new (Java.type("java.lang.Object")["()"])();
+if (obj.class != Java.type("java.lang.Object").class) {
+    fail("obj is a java.lang.Object");
+}
+
+// expected failure cases.
+function checkIt(func) {
+    try {
+        func();
+        throw new Error("should have thrown TypeError");
+    } catch(e) {
+        if (! (e instanceof TypeError)) {
+            fail("Expected TypeError, got " + e);
+        }
+        print(e);
+    }
+}
+
+// garbage signature string
+checkIt(function() new (Java.type("java.lang.Object")["()xxxxx"])());
+checkIt(function() new (Java.type("java.lang.Object")["("])());
+checkIt(function() new (Java.type("java.lang.Object")[")"])());
+
+// call constructor as normal method (without 'new')
+checkIt(function() Color());
+
+// try constructor on interface
+checkIt(function() new (Java.type("java.lang.Runnable"))["()"]);
+checkIt(function() new (Java.type("java.lang.Runnable"))["(int)"]);
+
+// try constructor on abstrace class
+try {
+    new (Java.type("java.io.InputStream"))["()"];
+    throw new Error("should have thrown exception!");
+} catch (e) {
+    print(e);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8049242.js.EXPECTED	Thu Jul 03 23:03:37 2014 +0530
@@ -0,0 +1,10 @@
+java.awt.Color[r=255,g=0,b=255]
+[jdk.internal.dynalink.beans.SimpleDynamicMethod Color java.awt.Color.java.awt.Color(int,int,int)]
+java.awt.Color[r=33,g=233,b=2]
+TypeError: null is not a function
+TypeError: null is not a function
+TypeError: null is not a function
+TypeError: Constructor [jdk.internal.dynalink.beans.SimpleDynamicMethod Color java.awt.Color.java.awt.Color(int,int,int)] requires new.
+TypeError: null is not a function
+TypeError: null is not a function
+java.lang.InstantiationException: java.io.InputStream