8011362: Overloaded method resolution foiled by nulls
Reviewed-by: hannesw, sundar
--- a/nashorn/src/jdk/internal/dynalink/beans/ClassString.java Wed Apr 03 11:41:42 2013 +0530
+++ b/nashorn/src/jdk/internal/dynalink/beans/ClassString.java Wed Apr 03 11:13:08 2013 +0200
@@ -96,6 +96,11 @@
* @author Attila Szegedi
*/
final class ClassString {
+ /**
+ * An anonymous inner class used solely to represent the "type" of null values for method applicability checking.
+ */
+ static final Class<?> NULL_CLASS = (new Object() { /* Intentionally empty */ }).getClass();
+
private final Class<?>[] classes;
private int hashCode;
@@ -203,6 +208,9 @@
}
private static boolean canConvert(LinkerServices ls, Class<?> from, Class<?> to) {
+ if(from == NULL_CLASS) {
+ return !to.isPrimitive();
+ }
return ls == null ? TypeUtilities.isMethodInvocationConvertible(from, to) : ls.canConvert(from, to);
}
}
--- a/nashorn/src/jdk/internal/dynalink/beans/OverloadedMethod.java Wed Apr 03 11:41:42 2013 +0530
+++ b/nashorn/src/jdk/internal/dynalink/beans/OverloadedMethod.java Wed Apr 03 11:13:08 2013 +0200
@@ -152,7 +152,7 @@
final Class<?>[] argTypes = new Class[args.length];
for(int i = 0; i < argTypes.length; ++i) {
final Object arg = args[i];
- argTypes[i] = arg == null ? callSiteType.parameterType(i) : arg.getClass();
+ argTypes[i] = arg == null ? ClassString.NULL_CLASS : arg.getClass();
}
final ClassString classString = new ClassString(argTypes);
MethodHandle method = argTypesToMethods.get(classString);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8011362.js Wed Apr 03 11:13:08 2013 +0200
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010, 2013, 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-8011362: Overloaded method resolution foiled by nulls
+ *
+ * @test
+ * @run
+ */
+
+var subject = new (Java.type("jdk.nashorn.test.models.Jdk8011362TestSubject"))
+
+print(subject.overloaded("", null))
+print(subject.overloaded(0, null))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8011362.js.EXPECTED Wed Apr 03 11:13:08 2013 +0200
@@ -0,0 +1,2 @@
+overloaded(Double, Double)
+overloaded(String, String)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/src/jdk/nashorn/test/models/Jdk8011362TestSubject.java Wed Apr 03 11:13:08 2013 +0200
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2010, 2013, 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. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+package jdk.nashorn.test.models;
+
+/**
+ * Test class used by JDK-8011362.js.
+ */
+public class Jdk8011362TestSubject {
+ // This is selected for overloaded("", null)
+ public String overloaded(String a, String b) {
+ return "overloaded(String, String)";
+ }
+
+ // This is selected for overloaded(0, null)
+ public String overloaded(Double a, Double b) {
+ return "overloaded(Double, Double)";
+ }
+
+ // This method is added to test that null will not match a primitive type, that is overloaded(0, null) will always
+ // select the (Double, Double) over (Double, double).
+ public String overloaded(Double a, double b) {
+ return "overloaded(Double, double)";
+ }
+}