8157819: TypeError when a java.util.Comparator object is invoked as a function
authorsundar
Wed, 25 May 2016 19:25:57 +0530
changeset 38596 9151a113789d
parent 38595 6eceed1919cf
child 38597 3e6c168b4edb
8157819: TypeError when a java.util.Comparator object is invoked as a function Reviewed-by: mhaupt, forax, hannesw
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java
nashorn/test/script/basic/JDK-8157819.js
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java	Wed May 25 15:14:37 2016 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java	Wed May 25 19:25:57 2016 +0530
@@ -186,7 +186,7 @@
             if (iface.isAnnotationPresent(FunctionalInterface.class)) {
                 // return the first abstract method
                 for (final Method m : iface.getMethods()) {
-                    if (Modifier.isAbstract(m.getModifiers())) {
+                    if (Modifier.isAbstract(m.getModifiers()) && !isOverridableObjectMethod(m)) {
                         return m.getName();
                     }
                 }
@@ -197,6 +197,23 @@
         return findFunctionalInterfaceMethodName(clazz.getSuperclass());
     }
 
+    // is this an overridable java.lang.Object method?
+    private static boolean isOverridableObjectMethod(final Method m) {
+        switch (m.getName()) {
+            case "equals":
+                if (m.getReturnType() == boolean.class) {
+                    final Class<?>[] params = m.getParameterTypes();
+                    return params.length == 1 && params[0] == Object.class;
+                }
+                return false;
+            case "hashCode":
+                return m.getReturnType() == int.class && m.getParameterCount() == 0;
+            case "toString":
+                return m.getReturnType() == String.class && m.getParameterCount() == 0;
+        }
+        return false;
+    }
+
     // Returns @FunctionalInterface annotated interface's single abstract
     // method name. If not found, returns null.
     static String getFunctionalInterfaceMethodName(final Class<?> clazz) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8157819.js	Wed May 25 19:25:57 2016 +0530
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 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
+ * 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-8157819: TypeError when a java.util.Comparator object is invoked as a function
+ *
+ * @test
+ * @run
+ */
+
+var compare = java.util.Comparator.naturalOrder()
+Assert.assertTrue(compare("nashorn", "ecmascript") > 0)
+Assert.assertTrue(compare("abc", "xyz") < 0)
+Assert.assertTrue(compare("hello", "hello") == 0)
+
+var rcompare = java.util.Comparator.reverseOrder()
+Assert.assertTrue(rcompare("nashorn", "ecmascript") < 0)
+Assert.assertTrue(rcompare("abc", "xyz") > 0)
+Assert.assertTrue(rcompare("hello", "hello") == 0)
+
+var arr = [ "nashorn", "JavaScript", "ECMAScript", "ecmascript", "js" ]
+Assert.assertEquals(arr.sort(compare).join(),
+    "ECMAScript,JavaScript,ecmascript,js,nashorn")
+Assert.assertEquals(arr.sort(rcompare).join(),
+    "nashorn,js,ecmascript,JavaScript,ECMAScript")
+