Thu, 06 Nov 2014 13:15:52 +0100
8062624: java.lang.String methods not available on concatenated strings
Reviewed-by: lagergren, attila
1.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java Wed Nov 05 17:07:26 2014 +0100 1.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java Thu Nov 06 13:15:52 2014 +0100 1.3 @@ -53,15 +53,34 @@ 1.4 // Object type arguments of Java method calls, field set and array set. 1.5 private static final boolean MIRROR_ALWAYS = Options.getBooleanProperty("nashorn.mirror.always", true); 1.6 1.7 - private static final MethodHandle EXPORT_ARGUMENT = new Lookup(MethodHandles.lookup()).findOwnStatic("exportArgument", Object.class, Object.class); 1.8 - private static final MethodHandle EXPORT_NATIVE_ARRAY = new Lookup(MethodHandles.lookup()).findOwnStatic("exportNativeArray", Object.class, NativeArray.class); 1.9 - private static final MethodHandle EXPORT_SCRIPT_OBJECT = new Lookup(MethodHandles.lookup()).findOwnStatic("exportScriptObject", Object.class, ScriptObject.class); 1.10 - private static final MethodHandle IMPORT_RESULT = new Lookup(MethodHandles.lookup()).findOwnStatic("importResult", Object.class, Object.class); 1.11 + private static final MethodHandle EXPORT_ARGUMENT; 1.12 + private static final MethodHandle EXPORT_NATIVE_ARRAY; 1.13 + private static final MethodHandle EXPORT_SCRIPT_OBJECT; 1.14 + private static final MethodHandle IMPORT_RESULT; 1.15 + private static final MethodHandle FILTER_CONSSTRING; 1.16 + 1.17 + static { 1.18 + final Lookup lookup = new Lookup(MethodHandles.lookup()); 1.19 + EXPORT_ARGUMENT = lookup.findOwnStatic("exportArgument", Object.class, Object.class); 1.20 + EXPORT_NATIVE_ARRAY = lookup.findOwnStatic("exportNativeArray", Object.class, NativeArray.class); 1.21 + EXPORT_SCRIPT_OBJECT = lookup.findOwnStatic("exportScriptObject", Object.class, ScriptObject.class); 1.22 + IMPORT_RESULT = lookup.findOwnStatic("importResult", Object.class, Object.class); 1.23 + FILTER_CONSSTRING = lookup.findOwnStatic("consStringFilter", Object.class, Object.class); 1.24 + } 1.25 1.26 private final BeansLinker beansLinker = new BeansLinker(); 1.27 1.28 @Override 1.29 public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { 1.30 + if (linkRequest.getReceiver() instanceof ConsString) { 1.31 + // In order to treat ConsString like a java.lang.String we need a link request with a string receiver. 1.32 + final Object[] arguments = linkRequest.getArguments(); 1.33 + arguments[0] = ""; 1.34 + final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(linkRequest.getCallSiteDescriptor(), arguments); 1.35 + final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices); 1.36 + // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings. 1.37 + return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING); 1.38 + } 1.39 return getGuardedInvocation(beansLinker, linkRequest, linkerServices); 1.40 } 1.41 1.42 @@ -113,6 +132,11 @@ 1.43 return ScriptUtils.unwrap(arg); 1.44 } 1.45 1.46 + @SuppressWarnings("unused") 1.47 + private static Object consStringFilter(final Object arg) { 1.48 + return arg instanceof ConsString ? arg.toString() : arg; 1.49 + } 1.50 + 1.51 private static class NashornBeansLinkerServices implements LinkerServices { 1.52 private final LinkerServices linkerServices; 1.53
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/script/basic/JDK-8062624.js Thu Nov 06 13:15:52 2014 +0100 2.3 @@ -0,0 +1,45 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/** 2.28 + * JDK-8062624: java.lang.String methods not available on concatenated strings 2.29 + * 2.30 + * @test 2.31 + * @run 2.32 + */ 2.33 + 2.34 +function testStringMethods(s) { 2.35 + print(s.startsWith("f")); 2.36 + print(s.endsWith("r")); 2.37 + print(Java.from(s.getBytes())); 2.38 + print(Java.from(s.bytes)); 2.39 +} 2.40 + 2.41 +var s = "f"; 2.42 +testStringMethods(s); 2.43 +s = s + "oo"; 2.44 +testStringMethods(s); 2.45 +testStringMethods("abc"); 2.46 +s += "bar"; 2.47 +s = "baz" + s; 2.48 +testStringMethods(s);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8062624.js.EXPECTED Thu Nov 06 13:15:52 2014 +0100 3.3 @@ -0,0 +1,16 @@ 3.4 +true 3.5 +false 3.6 +102 3.7 +102 3.8 +true 3.9 +false 3.10 +102,111,111 3.11 +102,111,111 3.12 +false 3.13 +false 3.14 +97,98,99 3.15 +97,98,99 3.16 +false 3.17 +true 3.18 +98,97,122,102,111,111,98,97,114 3.19 +98,97,122,102,111,111,98,97,114