Wed, 15 Jan 2014 14:51:49 +0530
8031715: Indexed access to java package not working
Reviewed-by: lagergren, hannesw
1.1 --- a/src/jdk/nashorn/internal/objects/NativeJavaImporter.java Mon Jan 13 18:13:55 2014 +0530 1.2 +++ b/src/jdk/nashorn/internal/objects/NativeJavaImporter.java Wed Jan 15 14:51:49 2014 +0530 1.3 @@ -134,6 +134,11 @@ 1.4 return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request); 1.5 } 1.6 1.7 + @Override 1.8 + protected Object invokeNoSuchProperty(final String name) { 1.9 + return createProperty(name); 1.10 + } 1.11 + 1.12 private boolean createAndSetProperty(final CallSiteDescriptor desc) { 1.13 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 1.14 final Object value = createProperty(name);
2.1 --- a/src/jdk/nashorn/internal/runtime/NativeJavaPackage.java Mon Jan 13 18:13:55 2014 +0530 2.2 +++ b/src/jdk/nashorn/internal/runtime/NativeJavaPackage.java Wed Jan 15 14:51:49 2014 +0530 2.3 @@ -198,24 +198,13 @@ 2.4 @Override 2.5 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { 2.6 final String propertyName = desc.getNameToken(2); 2.7 - final String fullName = name.isEmpty() ? propertyName : name + "." + propertyName; 2.8 + createProperty(propertyName); 2.9 + return super.lookup(desc, request); 2.10 + } 2.11 2.12 - final Context context = Context.getContextTrusted(); 2.13 - 2.14 - Class<?> javaClass = null; 2.15 - try { 2.16 - javaClass = context.findClass(fullName); 2.17 - } catch (final NoClassDefFoundError | ClassNotFoundException e) { 2.18 - //ignored 2.19 - } 2.20 - 2.21 - if (javaClass == null) { 2.22 - set(propertyName, new NativeJavaPackage(fullName, getProto()), false); 2.23 - } else { 2.24 - set(propertyName, StaticClass.forClass(javaClass), false); 2.25 - } 2.26 - 2.27 - return super.lookup(desc, request); 2.28 + @Override 2.29 + protected Object invokeNoSuchProperty(final String name) { 2.30 + return createProperty(name); 2.31 } 2.32 2.33 @Override 2.34 @@ -226,4 +215,26 @@ 2.35 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 2.36 return MH.findStatic(MethodHandles.lookup(), NativeJavaPackage.class, name, MH.type(rtype, types)); 2.37 } 2.38 + 2.39 + private Object createProperty(final String propertyName) { 2.40 + final String fullName = name.isEmpty() ? propertyName : name + "." + propertyName; 2.41 + final Context context = Context.getContextTrusted(); 2.42 + 2.43 + Class<?> javaClass = null; 2.44 + try { 2.45 + javaClass = context.findClass(fullName); 2.46 + } catch (final NoClassDefFoundError | ClassNotFoundException e) { 2.47 + //ignored 2.48 + } 2.49 + 2.50 + final Object propertyValue; 2.51 + if (javaClass == null) { 2.52 + propertyValue = new NativeJavaPackage(fullName, getProto()); 2.53 + } else { 2.54 + propertyValue = StaticClass.forClass(javaClass); 2.55 + } 2.56 + 2.57 + set(propertyName, propertyValue, false); 2.58 + return propertyValue; 2.59 + } 2.60 }
3.1 --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java Mon Jan 13 18:13:55 2014 +0530 3.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java Wed Jan 15 14:51:49 2014 +0530 3.3 @@ -2093,12 +2093,13 @@ 3.4 3.5 return createEmptyGetter(desc, name); 3.6 } 3.7 + 3.8 /** 3.9 * Invoke fall back if a property is not found. 3.10 * @param name Name of property. 3.11 * @return Result from call. 3.12 */ 3.13 - private Object invokeNoSuchProperty(final String name) { 3.14 + protected Object invokeNoSuchProperty(final String name) { 3.15 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 3.16 3.17 if (find != null) {
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8031715.js Wed Jan 15 14:51:49 2014 +0530 4.3 @@ -0,0 +1,49 @@ 4.4 +/* 4.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/** 4.28 + * JDK-8031715: Indexed access to java package not working 4.29 + * @test 4.30 + * @run 4.31 + */ 4.32 + 4.33 +print(java["net"]); 4.34 +print(java["net"]["URL"]); 4.35 +print(java["net"].URL); 4.36 +print(java.net["URL"]); 4.37 + 4.38 +var is = "InputStream"; 4.39 +var io = "io"; 4.40 + 4.41 +print(java.io[is]); 4.42 +print(java[io]); 4.43 +print(java[io][is]); 4.44 + 4.45 +var ji = new JavaImporter(java.util, java.io); 4.46 +print(ji["InputStream"]); 4.47 +print(ji['Vector']); 4.48 + 4.49 +var hash = "Hashtable"; 4.50 +var printStream = "PrintStream"; 4.51 +print(ji[hash]); 4.52 +print(ji[printStream]);
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/script/basic/JDK-8031715.js.EXPECTED Wed Jan 15 14:51:49 2014 +0530 5.3 @@ -0,0 +1,11 @@ 5.4 +[JavaPackage java.net] 5.5 +[JavaClass java.net.URL] 5.6 +[JavaClass java.net.URL] 5.7 +[JavaClass java.net.URL] 5.8 +[JavaClass java.io.InputStream] 5.9 +[JavaPackage java.io] 5.10 +[JavaClass java.io.InputStream] 5.11 +[JavaClass java.io.InputStream] 5.12 +[JavaClass java.util.Vector] 5.13 +[JavaClass java.util.Hashtable] 5.14 +[JavaClass java.io.PrintStream]