Defines JavaScript objects from a Java class that implements Scriptable.
If the given class has a method
static void init(Context cx, Scriptable scope, boolean sealed);
or its compatibility form
static void init(Scriptable scope);
then it is invoked and no further initialization is done.
However, if no such a method is found, then the class's constructors and
methods are used to initialize a class in the following manner.
First, the zero-parameter constructor of the class is called to
create the prototype. If no such constructor exists,
a
EvaluatorException is thrown.
Next, all methods are scanned for special prefixes that indicate that they
have special meaning for defining JavaScript objects.
These special prefixes are
jsFunction_
for a JavaScript function
jsStaticFunction_
for a JavaScript function that
is a property of the constructor
jsGet_
for a getter of a JavaScript property
jsSet_
for a setter of a JavaScript property
jsConstructor
for a JavaScript function that
is the constructor
If the method's name begins with "jsFunction_", a JavaScript function
is created with a name formed from the rest of the Java method name
following "jsFunction_". So a Java method named "jsFunction_foo" will
define a JavaScript method "foo". Calling this JavaScript function
will cause the Java method to be called. The parameters of the method
must be of number and types as defined by the FunctionObject class.
The JavaScript function is then added as a property
of the prototype.
If the method's name begins with "jsStaticFunction_", it is handled
similarly except that the resulting JavaScript function is added as a
property of the constructor object. The Java method must be static.
If the method's name begins with "jsGet_" or "jsSet_", the method is
considered to define a property. Accesses to the defined property
will result in calls to these getter and setter methods. If no
setter is defined, the property is defined as READONLY.
If the method's name is "jsConstructor", the method is
considered to define the body of the constructor. Only one
method of this name may be defined. You may use the varargs forms
for constructors documented in
FunctionObject#FunctionObject(String,Member,Scriptable)If no method is found that can serve as constructor, a Java
constructor will be selected to serve as the JavaScript
constructor in the following manner. If the class has only one
Java constructor, that constructor is used to define
the JavaScript constructor. If the the class has two constructors,
one must be the zero-argument constructor (otherwise an
EvaluatorException would have already been thrown
when the prototype was to be created). In this case
the Java constructor with one or more parameters will be used
to define the JavaScript constructor. If the class has three
or more constructors, an
EvaluatorExceptionwill be thrown.
Finally, if there is a method
static void finishInit(Scriptable scope, FunctionObject constructor,
Scriptable prototype)
it will be called to finish any initialization. The
scope
argument will be passed, along with the newly created constructor and
the newly created prototype.