src/jdk/nashorn/internal/runtime/NativeJavaPackage.java

Print this page




  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.runtime;
  27 
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  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.internal.dynalink.support.Guards;
  36 import jdk.nashorn.internal.lookup.MethodHandleFactory;
  37 import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
  38 import jdk.nashorn.internal.objects.NativeJava;
  39 import jdk.nashorn.internal.objects.annotations.Attribute;
  40 import jdk.nashorn.internal.objects.annotations.Function;
  41 
  42 /**
  43  * An object that exposes Java packages and classes as its properties. Packages are exposed as objects that have further
  44  * sub-packages and classes as their properties. Normally, three instances of this class are exposed as built-in objects
  45  * in Nashorn: {@code "Packages"}, {@code "java"}, and {@code "javax"}. Typical usages are:
  46  * <pre>
  47  * var list = new java.util.ArrayList()
  48  * var sprocket = new Packages.com.acme.Sprocket()
  49  * </pre>
  50  * or you can store the type objects in a variable for later reuse:
  51  * <pre>
  52  * var ArrayList = java.util.ArrayList
  53  * var list = new ArrayList
  54  * </pre>
  55  * You can also use {@link NativeJava#type(Object, Object)} to access Java classes. These two statements are mostly
  56  * equivalent:
  57  * <pre>
  58  * var listType1 = java.util.ArrayList
  59  * var listType2 = Java.type("java.util.ArrayList")
  60  * </pre>
  61  * The difference is that {@code Java.type()} will throw an error if the class does not exist, while the first
  62  * expression will return an empty object, as it must treat all non-existent classes as potentially being further
  63  * subpackages. As such, {@code Java.type()} has the potential to catch typos earlier. A further difference is that
  64  * {@code Java.type()} doesn't recognize {@code .} (dot) as the separator between outer class name and inner class name,
  65  * it only recognizes the dollar sign. These are equivalent:
  66  * <pre>
  67  * var ftype1 = java.awt.geom.Arc2D$Float
  68  * var ftype2 = java.awt.geom.Arc2D.Float
  69  * var ftype3 = Java.asType("java.awt.geom.Arc2D$Float")
  70  * var ftype4 = Java.asType("java.awt.geom.Arc2D").Float
  71  * </pre>
  72  */
  73 public final class NativeJavaPackage extends ScriptObject {
  74     private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
  75     private static final MethodHandle CLASS_NOT_FOUND = findOwnMH("classNotFound", Void.TYPE, NativeJavaPackage.class);




  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.runtime;
  27 
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  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.internal.dynalink.support.Guards;
  36 import jdk.nashorn.internal.lookup.MethodHandleFactory;
  37 import jdk.nashorn.internal.lookup.MethodHandleFunctionality;

  38 import jdk.nashorn.internal.objects.annotations.Attribute;
  39 import jdk.nashorn.internal.objects.annotations.Function;
  40 
  41 /**
  42  * An object that exposes Java packages and classes as its properties. Packages are exposed as objects that have further
  43  * sub-packages and classes as their properties. Normally, three instances of this class are exposed as built-in objects
  44  * in Nashorn: {@code "Packages"}, {@code "java"}, and {@code "javax"}. Typical usages are:
  45  * <pre>
  46  * var list = new java.util.ArrayList()
  47  * var sprocket = new Packages.com.acme.Sprocket()
  48  * </pre>
  49  * or you can store the type objects in a variable for later reuse:
  50  * <pre>
  51  * var ArrayList = java.util.ArrayList
  52  * var list = new ArrayList
  53  * </pre>
  54  * You can also use {@link jdk.nashorn.internal.objects.NativeJava#type(Object, Object)} to access Java classes. These two statements are mostly
  55  * equivalent:
  56  * <pre>
  57  * var listType1 = java.util.ArrayList
  58  * var listType2 = Java.type("java.util.ArrayList")
  59  * </pre>
  60  * The difference is that {@code Java.type()} will throw an error if the class does not exist, while the first
  61  * expression will return an empty object, as it must treat all non-existent classes as potentially being further
  62  * subpackages. As such, {@code Java.type()} has the potential to catch typos earlier. A further difference is that
  63  * {@code Java.type()} doesn't recognize {@code .} (dot) as the separator between outer class name and inner class name,
  64  * it only recognizes the dollar sign. These are equivalent:
  65  * <pre>
  66  * var ftype1 = java.awt.geom.Arc2D$Float
  67  * var ftype2 = java.awt.geom.Arc2D.Float
  68  * var ftype3 = Java.asType("java.awt.geom.Arc2D$Float")
  69  * var ftype4 = Java.asType("java.awt.geom.Arc2D").Float
  70  * </pre>
  71  */
  72 public final class NativeJavaPackage extends ScriptObject {
  73     private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
  74     private static final MethodHandle CLASS_NOT_FOUND = findOwnMH("classNotFound", Void.TYPE, NativeJavaPackage.class);