src/jdk/nashorn/internal/objects/NativeJava.java

Print this page




  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")




  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.ScriptFunction;
  49 import jdk.nashorn.internal.runtime.ScriptRuntime;
  50 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  51 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
  52 
  53 /**
  54  * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This
  55  * object acts as the API entry point to Java platform specific functionality, dealing with creating new instances of
  56  * Java classes, subclassing Java classes, implementing Java interfaces, converting between Java arrays and ECMAScript
  57  * arrays, and so forth.
  58  */
  59 @ScriptClass("Java")
  60 public final class NativeJava {
  61 
  62     // initialized by nasgen
  63     @SuppressWarnings("unused")
  64     private static PropertyMap $nasgenmap$;
  65 
  66     private NativeJava() {
  67         // don't create me
  68         throw new UnsupportedOperationException();
  69     }
  70 
  71     /**
  72      * Returns true if the specified object is a Java type object, that is an instance of {@link StaticClass}.
  73      * @param self not used
  74      * @param type the object that is checked if it is a type object or not
  75      * @return tells whether given object is a Java type object or not.
  76      * @see #type(Object, Object)
  77      */
  78     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
  79     public static boolean isType(final Object self, final Object type) {
  80         return type instanceof StaticClass;
  81     }
  82 
  83     /**
  84      * Returns synchronized wrapper version of the given ECMAScript function.
  85      * @param self not used
  86      * @param func the ECMAScript function whose synchronized version is returned.
  87      * @param obj the object (i.e, lock) on which the function synchronizes.
  88      * @return synchronized wrapper version of the given ECMAScript function.
  89      */
  90     @Function(name="synchronized", attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
  91     public static Object synchronizedFunc(final Object self, final Object func, final Object obj) {
  92         return (func instanceof ScriptFunction)?
  93             ((ScriptFunction)func).makeSynchronizedFunction(obj) : UNDEFINED;
  94     }
  95 
  96     /**
  97      * Returns true if the specified object is a Java method, that is an instance of {@link DynamicMethod}.
  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 instanceof Object) && !(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")