mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 18:44:38 +02:00
8157819: TypeError when a java.util.Comparator object is invoked as a function
Reviewed-by: mhaupt, forax, hannesw
This commit is contained in:
parent
883dc41e42
commit
1f0c44c1c4
2 changed files with 64 additions and 1 deletions
|
@ -186,7 +186,7 @@ public class NashornBeansLinker implements GuardingDynamicLinker {
|
|||
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 @@ public class NashornBeansLinker implements GuardingDynamicLinker {
|
|||
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) {
|
||||
|
|
46
nashorn/test/script/basic/JDK-8157819.js
Normal file
46
nashorn/test/script/basic/JDK-8157819.js
Normal file
|
@ -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")
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue