1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.objects; 27 28 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid; 29 30 import jdk.internal.dynalink.CallSiteDescriptor; 31 import jdk.internal.dynalink.beans.StaticClass; 32 import jdk.internal.dynalink.linker.GuardedInvocation; 33 import jdk.internal.dynalink.linker.LinkRequest; 34 import jdk.nashorn.internal.objects.annotations.Attribute; 35 import jdk.nashorn.internal.objects.annotations.Constructor; 36 import jdk.nashorn.internal.objects.annotations.Function; 37 import jdk.nashorn.internal.objects.annotations.ScriptClass; 38 import jdk.nashorn.internal.runtime.Context; 39 import jdk.nashorn.internal.runtime.NativeJavaPackage; 40 import jdk.nashorn.internal.runtime.PropertyMap; 41 import jdk.nashorn.internal.runtime.ScriptObject; 42 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; 43 44 /** 45 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names. 46 * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript 47 * strict mode. Example: 48 * <pre> 49 * var imports = new JavaImporter(java.util, java.io); 50 * with (imports) { 51 * var m = new HashMap(); // java.util.HashMap 52 * var f = new File("."); // java.io.File 53 * ... 54 * } 55 * </pre> 56 * Note however that the preferred way for accessing Java types in Nashorn is through the use of 57 * {@link NativeJava#type(Object, Object) Java.type()} method. 58 */ 59 @ScriptClass("JavaImporter") 60 public final class NativeJavaImporter extends ScriptObject { 61 private final Object[] args; 62 63 // initialized by nasgen 64 private static PropertyMap $nasgenmap$; 65 66 private NativeJavaImporter(final Object[] args, final ScriptObject proto, final PropertyMap map) { 67 super(proto, map); 68 this.args = args; 69 } 70 71 private NativeJavaImporter(final Object[] args, final Global global) { 72 this(args, global.getJavaImporterPrototype(), $nasgenmap$); 73 } 74 75 private NativeJavaImporter(final Object[] args) { 76 this(args, Global.instance()); 77 } 78 79 @Override 80 public String getClassName() { 81 return "JavaImporter"; 82 } 83 84 /** 85 * Constructor 86 * @param isNew is the new operator used for instantiating this NativeJavaImporter 87 * @param self self reference 88 * @param args arguments 89 * @return NativeJavaImporter instance 90 */ 91 @Constructor(arity = 1) 92 public static NativeJavaImporter constructor(final boolean isNew, final Object self, final Object... args) { 93 return new NativeJavaImporter(args); 94 } 95 96 /** 97 * "No such property" call placeholder. 98 * 99 * This can never be called as we override {@link ScriptObject#noSuchProperty}. We do declare it here as it's a signal 100 * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a {@code noSuchProperty} on this object. 101 * 102 * @param self self reference 103 * @param name property name 104 * @return never returns 105 */ 106 @Function(attributes = Attribute.NOT_ENUMERABLE) 107 public static Object __noSuchProperty__(final Object self, final Object name) { 108 throw new AssertionError("__noSuchProperty__ placeholder called"); 109 } 110 111 /** 112 * "No such method call" placeholder 113 * 114 * This can never be called as we override {@link ScriptObject#noSuchMethod}. We do declare it here as it's a signal 115 * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a noSuchProperty on this object. 116 * 117 * @param self self reference 118 * @param args arguments to method 119 * @return never returns 120 */ 121 @Function(attributes = Attribute.NOT_ENUMERABLE) 122 public static Object __noSuchMethod__(final Object self, final Object... args) { 123 throw new AssertionError("__noSuchMethod__ placeholder called"); 124 } 125 126 @Override 127 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { 128 return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchProperty(desc, request); 129 } 130 131 @Override 132 public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) { 133 return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request); 134 } 135 136 @Override 137 protected Object invokeNoSuchProperty(final String name, final int programPoint) { 138 final Object retval = createProperty(name); 139 if (isValid(programPoint)) { 140 throw new UnwarrantedOptimismException(retval, programPoint); 141 } 142 return retval; 143 } 144 145 private boolean createAndSetProperty(final CallSiteDescriptor desc) { 146 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 147 final Object value = createProperty(name); 148 if(value != null) { 149 set(name, value, 0); 150 return true; 151 } 152 return false; 153 } 154 155 private Object createProperty(final String name) { 156 final int len = args.length; 157 158 for (int i = len - 1; i > -1; i--) { 159 final Object obj = args[i]; 160 161 if (obj instanceof StaticClass) { 162 if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) { 163 return obj; 164 } 165 } else if (obj instanceof NativeJavaPackage) { 166 final String pkgName = ((NativeJavaPackage)obj).getName(); 167 final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name); 168 final Context context = Global.instance().getContext(); 169 try { 170 return StaticClass.forClass(context.findClass(fullName)); 171 } catch (final ClassNotFoundException e) { 172 // IGNORE 173 } 174 } 175 } 176 return null; 177 } 178 }