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.ScriptRuntime.UNDEFINED; 30 31 import java.lang.invoke.MethodHandles; 32 import java.lang.reflect.Array; 33 import java.util.Collection; 34 import java.util.Deque; 35 import java.util.List; 36 import jdk.internal.dynalink.beans.StaticClass; 37 import jdk.internal.dynalink.support.TypeUtilities; 38 import jdk.nashorn.api.scripting.JSObject; 39 import jdk.nashorn.internal.objects.annotations.Attribute; 40 import jdk.nashorn.internal.objects.annotations.Function; 41 import jdk.nashorn.internal.objects.annotations.ScriptClass; 42 import jdk.nashorn.internal.objects.annotations.Where; 43 import jdk.nashorn.internal.runtime.Context; 44 import jdk.nashorn.internal.runtime.JSType; 45 import jdk.nashorn.internal.runtime.ListAdapter; 46 import jdk.nashorn.internal.runtime.PropertyMap; 47 import jdk.nashorn.internal.runtime.ScriptObject; 48 import jdk.nashorn.internal.runtime.ScriptRuntime; 49 import jdk.nashorn.internal.runtime.linker.Bootstrap; 50 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 51 52 /** 53 * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This 54 * object acts as the API entry point to Java platform specific functionality, dealing with creating new instances of 55 * Java classes, subclassing Java classes, implementing Java interfaces, converting between Java arrays and ECMAScript 56 * arrays, and so forth. 57 */ 58 @ScriptClass("Java") 59 public final class NativeJava { 60 61 // initialized by nasgen 62 @SuppressWarnings("unused") 63 private static PropertyMap $nasgenmap$; 64 65 private NativeJava() { 66 // don't create me 67 throw new UnsupportedOperationException(); 68 } 69 70 /** 71 * Returns true if the specified object is a Java type object, that is an instance of {@link StaticClass}. 72 * @param self not used 73 * @param type the object that is checked if it is a type object or not 74 * @return tells whether given object is a Java type object or not. 75 * @see #type(Object, Object) 76 */ 77 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 78 public static boolean isType(final Object self, final Object type) { 79 return type instanceof StaticClass; 80 } 81 82 /** 83 * <p> 84 * Given a name of a Java type, returns an object representing that type in Nashorn. The Java class of the objects 85 * used to represent Java types in Nashorn is not {@link java.lang.Class} but rather {@link StaticClass}. They are 86 * the objects that you can use with the {@code new} operator to create new instances of the class as well as to 87 * access static members of the class. In Nashorn, {@code Class} objects are just regular Java objects that aren't 88 * treated specially. Instead of them, {@link StaticClass} instances - which we sometimes refer to as "Java type 89 * objects" are used as constructors with the {@code new} operator, and they expose static fields, properties, and 90 * methods. While this might seem confusing at first, it actually closely matches the Java language: you use a 91 * different expression (e.g. {@code java.io.File}) as an argument in "new" and to address statics, and it is 92 * distinct from the {@code Class} object (e.g. {@code java.io.File.class}). Below we cover in details the 93 * properties of the type objects. 94 * </p> 95 * <p><b>Constructing Java objects</b></p> 96 * Examples: 97 * <pre> 98 * var arrayListType = Java.type("java.util.ArrayList") 99 * var intType = Java.type("int") | 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.ScriptRuntime.UNDEFINED; 30 31 import java.lang.invoke.MethodHandles; 32 import java.lang.reflect.Array; 33 import java.util.Collection; 34 import java.util.Deque; 35 import java.util.List; 36 import jdk.internal.dynalink.beans.StaticClass; 37 import jdk.internal.dynalink.support.TypeUtilities; 38 import jdk.nashorn.api.scripting.JSObject; 39 import jdk.nashorn.api.scripting.ScriptUtils; 40 import jdk.nashorn.internal.objects.annotations.Attribute; 41 import jdk.nashorn.internal.objects.annotations.Function; 42 import jdk.nashorn.internal.objects.annotations.ScriptClass; 43 import jdk.nashorn.internal.objects.annotations.Where; 44 import jdk.nashorn.internal.runtime.Context; 45 import jdk.nashorn.internal.runtime.JSType; 46 import jdk.nashorn.internal.runtime.ListAdapter; 47 import jdk.nashorn.internal.runtime.PropertyMap; 48 import jdk.nashorn.internal.runtime.ScriptObject; 49 import jdk.nashorn.internal.runtime.ScriptFunction; 50 import jdk.nashorn.internal.runtime.ScriptRuntime; 51 import jdk.nashorn.internal.runtime.linker.Bootstrap; 52 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 53 54 /** 55 * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This 56 * object acts as the API entry point to Java platform specific functionality, dealing with creating new instances of 57 * Java classes, subclassing Java classes, implementing Java interfaces, converting between Java arrays and ECMAScript 58 * arrays, and so forth. 59 */ 60 @ScriptClass("Java") 61 public final class NativeJava { 62 63 // initialized by nasgen 64 @SuppressWarnings("unused") 65 private static PropertyMap $nasgenmap$; 66 67 private NativeJava() { 68 // don't create me 69 throw new UnsupportedOperationException(); 70 } 71 72 /** 73 * Returns true if the specified object is a Java type object, that is an instance of {@link StaticClass}. 74 * @param self not used 75 * @param type the object that is checked if it is a type object or not 76 * @return tells whether given object is a Java type object or not. 77 * @see #type(Object, Object) 78 */ 79 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 80 public static boolean isType(final Object self, final Object type) { 81 return type instanceof StaticClass; 82 } 83 84 /** 85 * Returns synchronized wrapper version of the given ECMAScript function. 86 * @param self not used 87 * @param func the ECMAScript function whose synchronized version is returned. 88 * @param obj the object (i.e, lock) on which the function synchronizes. 89 * @return synchronized wrapper version of the given ECMAScript function. 90 */ 91 @Function(name="synchronized", attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 92 public static Object synchronizedFunc(final Object self, final Object func, final Object obj) { 93 return ScriptUtils.makeSynchronizedFunction(func, obj); 94 } 95 96 /** 97 * Returns true if the specified object is a Java method. 98 * @param self not used 99 * @param obj the object that is checked if it is a Java method object or not 100 * @return tells whether given object is a Java method object or not. 101 */ 102 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 103 public static boolean isJavaMethod(final Object self, final Object obj) { 104 return Bootstrap.isDynamicMethod(obj); 105 } 106 107 /** 108 * Returns true if the specified object is a java function (but not script function) 109 * @param self not used 110 * @param obj the object that is checked if it is a Java function or not 111 * @return tells whether given object is a Java function or not 112 */ 113 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 114 public static boolean isJavaFunction(final Object self, final Object obj) { 115 return Bootstrap.isCallable(obj) && !(obj instanceof ScriptFunction); 116 } 117 118 /** 119 * Returns true if the specified object is a Java object but not a script object 120 * @param self not used 121 * @param obj the object that is checked 122 * @return tells whether given object is a Java object but not a script object 123 */ 124 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 125 public static boolean isJavaObject(final Object self, final Object obj) { 126 return obj != null && !(obj instanceof ScriptObject); 127 } 128 129 /** 130 * Returns true if the specified object is a ECMAScript object, that is an instance of {@link ScriptObject}. 131 * @param self not used 132 * @param obj the object that is checked if it is a ECMAScript object or not 133 * @return tells whether given object is a ECMAScript object or not. 134 */ 135 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 136 public static boolean isScriptObject(final Object self, final Object obj) { 137 return obj instanceof ScriptObject; 138 } 139 140 /** 141 * Returns true if the specified object is a ECMAScript function, that is an instance of {@link ScriptFunction}. 142 * @param self not used 143 * @param obj the object that is checked if it is a ECMAScript function or not 144 * @return tells whether given object is a ECMAScript function or not. 145 */ 146 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 147 public static boolean isScriptFunction(final Object self, final Object obj) { 148 return obj instanceof ScriptFunction; 149 } 150 151 /** 152 * <p> 153 * Given a name of a Java type, returns an object representing that type in Nashorn. The Java class of the objects 154 * used to represent Java types in Nashorn is not {@link java.lang.Class} but rather {@link StaticClass}. They are 155 * the objects that you can use with the {@code new} operator to create new instances of the class as well as to 156 * access static members of the class. In Nashorn, {@code Class} objects are just regular Java objects that aren't 157 * treated specially. Instead of them, {@link StaticClass} instances - which we sometimes refer to as "Java type 158 * objects" are used as constructors with the {@code new} operator, and they expose static fields, properties, and 159 * methods. While this might seem confusing at first, it actually closely matches the Java language: you use a 160 * different expression (e.g. {@code java.io.File}) as an argument in "new" and to address statics, and it is 161 * distinct from the {@code Class} object (e.g. {@code java.io.File.class}). Below we cover in details the 162 * properties of the type objects. 163 * </p> 164 * <p><b>Constructing Java objects</b></p> 165 * Examples: 166 * <pre> 167 * var arrayListType = Java.type("java.util.ArrayList") 168 * var intType = Java.type("int") |