This class provides methods named create
that will
create a new instance of a class using the class or class name as
create
's first argument. The first argument for these
methods should be either the class of the object that is to be
created or a string containing the fully-qualified class name of
that class. The remaining arguments are those that a constructor
can use. Up to 11 arguments are supported. The original motivation
for these methods was a work-around for a bug, but the methods may
also be useful when a number of classes have constructors that take
the same arguments and the choice of which class to use is made at
run time. The create
methods specify Object
as the type of the object returned, which works for scripting
languages as these typically are not strongly typed.
There are two methods named createArray
that will
create an array of a specified size, and whose first
argument is the class of an array's components or the
fully-qualified name of the class of the array's components.
This can be used by a scripting language to create a Java array
conveniently.
There are a also series of methods named
createAndInitArray
. The first argument of these
methods are also either the class of an array's components or the
fully-qualified name of the class of the array's components.
The remaining arguments are the values used to initialize the
array.
Several methods aid in the use of Java classes:
importClass(String)
, importClass(String,String)
,
importClasses(String,Object)
, and finishImport()
.
These methods allow Java classes to be imported and bound to
variables in the script's name space. The method
importClasses(String,Object)
will import multiple classes
using the scripting language to represent the class names in a
succinct form. After a sequence of calls to importClass and importClasses,
the method finishImport()
must be called.
Finally, the scrunner
command will run scripts in an
environment where a variable names scripting
is
predefined. This variable is an instance of this class.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.scripting.ScriptingContext
ScriptingContext.BindingSwapper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateAndInitArray
(Class<?> clazz) Create a zero-length array given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0) Create and initialize an array of length one given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1) Create and initialize an array of length two given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2) Create and initialize an array of length three given the class of its components.Create and initialize an array of length four given the class of its components.Create and initialize an array of length five given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) Create and initialize an array of length six given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) Create and initialize an array of length seven given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) Create and initialize an array of length eight given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) Create and initialize an array of length nine given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) Create and initialize an array of length ten given the class of its components.createAndInitArray
(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) Create and initialize an array of length eleven given the class of its components.createAndInitArray
(String className) Create a zero-length array given the name of the class of its components.createAndInitArray
(String className, Object arg0) Create and initialize an array of length one given the name of the class of its componentscreateAndInitArray
(String className, Object arg0, Object arg1) Create and initialize an array of length two given the name of the class of its componentscreateAndInitArray
(String className, Object arg0, Object arg1, Object arg2) Create an array of length three given the name of the class of its components.Create an array of length four given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) Create an array of length five given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) Create an array of length six given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) Create an array of length seven given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) Create an array of length eight given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) Create an array of length nine given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) Create an array of length ten given the name of the class of its components.createAndInitArray
(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) Create an array of length eleven given the name of the class of its components.createArray
(Class<?> clazz, int n) Create an array given its class.createArray
(String className, int n) Create an array given the class name of its element type.void
Complete a series of imports.void
importClass
(String className) Import a class given its fully-qualified name and bind it to a scripting-language variable.void
importClass
(String packageName, String className) Import a class given its package and bind it to a scripting-language variable.void
importClasses
(String packageName, Object scriptObject) Import classes, specifying the classes to import in a script.Methods inherited from class org.bzdev.scripting.ScriptingContext
callScriptFunction, callScriptFunction, callScriptMethod, callScriptMethod, containsScriptObject, createBindings, createBindingSwapper, createScriptEngine, doGetDefaultBindings, doGetScriptEngine, doGetScriptLanguage, doScriptPrivileged, evalScript, evalScript, evalScript, evalScript, getErrorWriter, getNames, getReader, getScriptLanguage, getScriptObject, getWriter, hasScriptEngine, invokePrivateFunction, invokePrivateFunction, putScriptObject, setErrorWriter, setReader, setWriter, usingGraalVM
-
Constructor Details
-
ExtendedScriptingContext
Constructor. Unless methods are overridden, the parent scripting context provides the scripting language, script engine, and bindings. This scripting context will be trusted if and only if the parent's scripting context is trusted.- Parameters:
parent
- the parent scripting context; null if there is none.
-
-
Method Details
-
createArray
Create an array given its class. This is a convenience method so that a script does not have to call java.lang.reflect.Array.newInstance(...). The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the name of a classn
- the number of elements in the array- Returns:
- an array of size n
- Throws:
NegativeArraySizeException
- the array size was negative
-
createArray
public Object createArray(String className, int n) throws NegativeArraySizeException, ClassNotFoundException Create an array given the class name of its element type. This is a convenience method so that a script does not have to call java.lang.reflect.Array.newInstance(...). For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".- Parameters:
className
- the name of a classn
- the number of elements in the array- Returns:
- an array of size n
- Throws:
NegativeArraySizeException
- the array size was negativeClassNotFoundException
- the class could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz) throws IllegalArgumentException, NullPointerException Create a zero-length array given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array components- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create a zero-length array given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's components- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0) throws IllegalArgumentException, NullPointerException Create and initialize an array of length one given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create and initialize an array of length one given the name of the class of its componentsFor convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1) throws IllegalArgumentException, NullPointerException Create and initialize an array of length two given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create and initialize an array of length two given the name of the class of its componentsFor convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2) throws IllegalArgumentException, NullPointerException Create and initialize an array of length three given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length three given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3) throws IllegalArgumentException, NullPointerException Create and initialize an array of length four given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length four given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) throws IllegalArgumentException, NullPointerException Create and initialize an array of length five given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length five given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws IllegalArgumentException, NullPointerException Create and initialize an array of length six given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length six given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) throws IllegalArgumentException, NullPointerException Create and initialize an array of length seven given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length seven given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) throws IllegalArgumentException, NullPointerException Create and initialize an array of length eight given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length eight given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) throws IllegalArgumentException, NullPointerException Create and initialize an array of length nine given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length nine given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) throws IllegalArgumentException, NullPointerException Create and initialize an array of length ten given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the arrayarg9
- the 9th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length ten given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the arrayarg9
- the 9th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
createAndInitArray
public Object createAndInitArray(Class<?> clazz, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) throws IllegalArgumentException, NullPointerException Create and initialize an array of length eleven given the class of its components.The classes Byte, Character, Double, Integer, Long, Short, have fields named "TYPE" that provide the class names for the primitive types "byte", "char", "double", "int", "long", and "short" respectively.
Note: for Java 1.8 and 1.7, the default scripting engine (ECMAScript) differ in how objects that represent a Java class are treated. For Java 1.7, which uses the Rhino script engine, the variable used to represent a class is automatically converted to a Java class when this method is called. For Java 1.8, which uses the Nashorn script engine, one must use the "class" property of the object to get the Java class or an error will occur. Due to this incompatibility, one should use the variant of this method that accepts a string rather than a class name if the script will be used with both versions of Java.
- Parameters:
clazz
- the class of the array componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the arrayarg9
- the 9th component of the arrayarg10
- the 10th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was null
-
createAndInitArray
public Object createAndInitArray(String className, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) throws IllegalArgumentException, NullPointerException, ClassNotFoundException Create an array of length eleven given the name of the class of its components.For convenience, the names of primitive types can be given as "byte", "char", "double", "int", "long", or "short".
- Parameters:
className
- the fully-qualified name of the class of the array's componentsarg0
- the 0th component of the arrayarg1
- the 1st component of the arrayarg2
- the 2nd component of the arrayarg3
- the 3rd component of the arrayarg4
- the 4th component of the arrayarg5
- the 5th component of the arrayarg6
- the 6th component of the arrayarg7
- the 7th component of the arrayarg8
- the 8th component of the arrayarg9
- the 9th component of the arrayarg10
- the 10th component of the array- Returns:
- the array
- Throws:
IllegalArgumentException
- an array component had the wrong typeNullPointerException
- the first argument was nullClassNotFoundException
- the class corresponding to the class name could not be found
-
importClass
public void importClass(String className) throws ClassNotFoundException, IllegalArgumentException, NullPointerException, UnsupportedOperationException, ScriptException Import a class given its fully-qualified name and bind it to a scripting-language variable. The name of the scripting language variable will be the last component of the fully qualified name.Inner classes of the class imported are accessible using the normal dot (".") notation. For example,
will create an ECMAScript object stored in a variable named Path2D, and the ECMAScript expression Path2D.Double will refer to the class java.awt.geom.Path2D.Double. In addition, the ECMASCriptscripting.importClass("java.awt.geom.Path2D"); scripting.finishImport();
new
operator can be used to create new instances:scripting.importClass("java.awt.geom.Path2D"); scripting.finishImport(); path = new Path2D.Double();
As a second example,
will create an ECMAScript variable named Category whose value is the enumeration class java.util.Locale.Category.scripting.importClass("java.util.Locale.Category") scripting.finishImport();
Note: even for the same scripting language, the type of object created will depended on the specific implementation in use.
- Parameters:
className
- the fully-qualified name of the class- Throws:
ClassNotFoundException
- if the class could not be foundIllegalArgumentException
- if the method detected a syntax error in the package name or class nameNullPointerException
- if the class name was nullUnsupportedOperationException
- this operation is not supported (could not add a class to the scripting environment)ScriptException
- an error occurred executing a script- See Also:
-
importClass
public void importClass(String packageName, String className) throws ClassNotFoundException, IllegalArgumentException, NullPointerException, UnsupportedOperationException, ScriptException Import a class given its package and bind it to a scripting-language variable. The name of the scripting language variable will be the last component of the class name.Inner classes of the class imported are accessible using the normal dot (".") notation. For example,
will create an ECMAScript object stored in a variable named Path2D, and the ECMAScript expression Path2D.Double will refer to the class java.awt.geom.Path2D.Double. In addition, the ECMASCriptscripting.importClass("java.awt.geom", "Path2D") scripting.finishImport();
new
operator can be used to create new instances:
As a second example,scripting.importClass("java.awt.geom.Path2D"); scripting.finishImport(); path = new Path2D.Double();
will create an ECMAScript variable named Category whose value is the enumeration class java.util.Locale.Category.scripting.importClass("java.util", "Locale.Category") scripting.finishImport();
- Parameters:
packageName
- the name of the class' package.className
- the class's name, excluding its package name- Throws:
ClassNotFoundException
- if the class could not be foundIllegalArgumentException
- if the method detected a syntax error in the package name or class nameNullPointerException
- if the class name was nullUnsupportedOperationException
- this operation is not supported (could not add a class to the scripting environment)ScriptException
- an error occurred executing a script- See Also:
-
importClasses
Import classes, specifying the classes to import in a script. This method is provided because the Java scripting interface does not provide a standard way of importing Java classes into a scripting environment. Java-7 used the Rhino ECMAScript implementation that defined a global variable named Packages and the EMCAScript functions importPackage and importClass. Java-8 uses the Nashhorn ECMAScript implementation, which does not contain these two functions, allows them to be defined by calling
but this function call will fail when the Rhino script engine is used as the specified ECMAScript file will not be found.load("nashorn:mozilla_compat.js");
The importClasses method and importClass methods are provided to to allow a script to import various classes regardless of the script engine in use and without having to add error-handling to the script. To use importClasses, the first argument is a package name. The second argument is scripting-language dependent. For ECMA script, the second argument is an ECMA-script array of class names, excluding the package name. For inner classes, the names are a sequence of names separated by a period (".") separating a class from the name of an inner class that class contains. The first component of the name must be the name of a class in the specified package. In all cases, the inner classes must be declared to be public and static.
If the variable
scripting
is an instance of ExtendedScriptingContext (it is initialized as an instance of this class when scrunner is used), and if it uses ECMAScript, the following script fragment will import several classes from the anim2d package:
The classes imported will be the classesscripting.inputClasses("org.bzdev.anim2d", ["Animation2D", "AnimationLayer2D", "GraphView]); scripting.finishImport();
Animation2D
,AnimationLayer2D
, andGraphView
, stored using variables with the same names respectively. The ECMAScript expressions that refer to the inner classes are AnimationLayer2D.Type and GraphView.ZoomMode (both are enumerations). Similarly, the statement
will define ECMAScript variables named Type and ZoomMode and their values represent the enumeration classes org.bzdev.anim2d.AnimationLayer2D.Type and org.bzdev.anim2d.GraphView.ZoomMode respectively.scripting.inputClasses("org.bzdev.anim2d", ["AnimationLayer2D.Type", "GraphView.ZoomMode]); scripting.finishImport();
This method does not add any functionality beyond that provided by the importClass methods: it's purpose is to shorten the amount of text that must be placed in scripts to import the desired classes.
As an aside, the ECMA scripting engine that comes with Java 7 and Java 8 allow one to construct an object named JavaImporter, with packages provided as arguments. For example,
classes = new JavaImporter(java.util, java.io); ... with (classes) { var list = new LinkedList(); ... }
There is a recommendation to use JavaImporter to minimize the size of the scripting engine's global name space. This recommendation makes sense for long-running applications (servers, actually) that may load large numbers of classes in response to user-generated requests. The result is that unneeded classes will not be garbage-collected. When using programs such as scrunner, this is typically not an issue: scrunner is intended to perform a single task, after which it exits. Unfortunately, there is no guarantee as to what other implementations of ECMAScript will do to import java classes and specifically whether they will provide the JavaImporter class. For maximal portability, it is possibly better to use the methods
importClasses(String,Object)
,importClass(String,String)
orimportClass(String)
Sequences of calls these methods should be followed by a call tofinishImport()
. For some scripting languages the imports will not occur untilfinishImport()
is called.- Parameters:
packageName
- the name of the package from which classes are to be importedscriptObject
- an object in current scripting context's scripting language that specifies the classes to import (typically an array or a list of names for the classes in the specified package)- See Also:
-
finishImport
public void finishImport()Complete a series of imports. This method should be called after a sequence of calls toimportClass(String)
,importClass(String,String)
, and/orimportClasses(String,Object)
. It is optional for some scripting languages but mandatory for others. For the ESP scripting language, a line containing '###' should be placed after this call so that the import occurs before additional parsing.
-