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; 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 } | 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.ECMAErrors.typeError; 29 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid; 30 31 import jdk.internal.dynalink.CallSiteDescriptor; 32 import jdk.internal.dynalink.beans.StaticClass; 33 import jdk.internal.dynalink.linker.GuardedInvocation; 34 import jdk.internal.dynalink.linker.LinkRequest; 35 import jdk.nashorn.internal.objects.annotations.Attribute; 36 import jdk.nashorn.internal.objects.annotations.Constructor; 37 import jdk.nashorn.internal.objects.annotations.Function; 38 import jdk.nashorn.internal.objects.annotations.ScriptClass; 39 import jdk.nashorn.internal.runtime.Context; 40 import jdk.nashorn.internal.runtime.JSType; 41 import jdk.nashorn.internal.runtime.NativeJavaPackage; 42 import jdk.nashorn.internal.runtime.PropertyMap; 43 import jdk.nashorn.internal.runtime.ScriptObject; 44 import jdk.nashorn.internal.runtime.ScriptRuntime; 45 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; 46 47 /** 48 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names. 49 * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript 50 * strict mode. Example: 51 * <pre> 52 * var imports = new JavaImporter(java.util, java.io); 53 * with (imports) { 54 * var m = new HashMap(); // java.util.HashMap 55 * var f = new File("."); // java.io.File 56 * ... 57 * } 58 * </pre> 59 * Note however that the preferred way for accessing Java types in Nashorn is through the use of 60 * {@link NativeJava#type(Object, Object) Java.type()} method. 61 */ 62 @ScriptClass("JavaImporter") 63 public final class NativeJavaImporter extends ScriptObject { 64 private final Object[] args; 80 } 81 82 @Override 83 public String getClassName() { 84 return "JavaImporter"; 85 } 86 87 /** 88 * Constructor 89 * @param isNew is the new operator used for instantiating this NativeJavaImporter 90 * @param self self reference 91 * @param args arguments 92 * @return NativeJavaImporter instance 93 */ 94 @Constructor(arity = 1) 95 public static NativeJavaImporter constructor(final boolean isNew, final Object self, final Object... args) { 96 return new NativeJavaImporter(args); 97 } 98 99 /** 100 * "No such property" handler. 101 * 102 * @param self self reference 103 * @param name property name 104 * @return value of the missing property 105 */ 106 @Function(attributes = Attribute.NOT_ENUMERABLE) 107 public static Object __noSuchProperty__(final Object self, final Object name) { 108 if (! (self instanceof NativeJavaImporter)) { 109 throw typeError("not.a.java.importer", ScriptRuntime.safeToString(self)); 110 } 111 return ((NativeJavaImporter)self).createProperty(JSType.toString(name)); 112 } 113 114 /** 115 * "No such method call" handler 116 * 117 * @param self self reference 118 * @param args arguments to method 119 * @return never returns always throw TypeError 120 */ 121 @Function(attributes = Attribute.NOT_ENUMERABLE) 122 public static Object __noSuchMethod__(final Object self, final Object... args) { 123 throw typeError("not.a.function", ScriptRuntime.safeToString(args[0])); 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 } |