|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.mozilla.javascript.ScriptableObject | +--org.mozilla.javascript.IdScriptable | +--org.mozilla.javascript.BaseFunction | +--org.mozilla.javascript.FunctionObject
Fields inherited from class org.mozilla.javascript.ScriptableObject |
DONTENUM, EMPTY, PERMANENT, READONLY |
Fields inherited from interface org.mozilla.javascript.Scriptable |
NOT_FOUND |
Constructor Summary | |
FunctionObject(java.lang.String name,
java.lang.reflect.Member methodOrConstructor,
Scriptable scope)
Create a JavaScript function object from a Java method. |
Method Summary | |
void |
addAsConstructor(Scriptable scope,
Scriptable prototype)
Define this function as a JavaScript constructor. |
java.lang.Object |
call(Context cx,
Scriptable scope,
Scriptable thisObj,
java.lang.Object[] args)
Performs conversions on argument types if needed and invokes the underlying Java method or constructor. |
Scriptable |
construct(Context cx,
Scriptable scope,
java.lang.Object[] args)
Performs conversions on argument types if needed and invokes the underlying Java method or constructor to create a new Scriptable object. |
static java.lang.Object |
convertArg(Context cx,
Scriptable scope,
java.lang.Object arg,
java.lang.Class desired)
|
static java.lang.reflect.Method[] |
findMethods(java.lang.Class clazz,
java.lang.String name)
Finds methods of a given name in a given class. |
int |
getArity()
Return the value defined by the method used to construct the object (number of parameters of the method, or 1 if the method is a "varargs" form). |
int |
getLength()
Return the same value as getArity() . |
Methods inherited from class org.mozilla.javascript.BaseFunction |
createObject, decompile, execMethod, getClassName, getFunctionName, hasInstance, methodArity, setImmunePrototypeProperty |
Methods inherited from class org.mozilla.javascript.IdScriptable |
addAsPrototype, defineProperty, delete, get, getAttributes, has, put, setAttributes |
Methods inherited from class org.mozilla.javascript.ScriptableObject |
callMethod, defineClass, defineClass, defineFunctionProperties, defineProperty, defineProperty, defineProperty, delete, deleteProperty, deleteProperty, get, getAllIds, getAttributes, getClassPrototype, getDefaultValue, getFunctionPrototype, getIds, getObjectPrototype, getParentScope, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, has, hasProperty, hasProperty, isSealed, put, putProperty, putProperty, sealObject, setAttributes, setParentScope, setPrototype |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.mozilla.javascript.Scriptable |
delete, delete, get, get, getDefaultValue, getIds, getParentScope, getPrototype, has, has, put, put, setParentScope, setPrototype |
Constructor Detail |
public FunctionObject(java.lang.String name, java.lang.reflect.Member methodOrConstructor, Scriptable scope)
The member
argument must be either a java.lang.reflect.Method
or a java.lang.reflect.Constructor and must match one of two forms.
The first form is a member with zero or more parameters of the following types: Object, String, boolean, Scriptable, byte, short, int, float, or double. The Long type is not supported because the double representation of a long (which is the EMCA-mandated storage type for Numbers) may lose precision. If the member is a Method, the return value must be void or one of the types allowed for parameters.
The runtime will perform appropriate conversions based upon the type of the parameter. A parameter type of Object specifies that no conversions are to be done. A parameter of type String will use Context.toString to convert arguments. Similarly, parameters of type double, boolean, and Scriptable will cause Context.toNumber, Context.toBoolean, and Context.toObject, respectively, to be called.
If the method is not static, the Java 'this' value will correspond to the JavaScript 'this' value. Any attempt to call the function with a 'this' value that is not of the right Java type will result in an error.
The second form is the variable arguments (or "varargs") form. If the FunctionObject will be used as a constructor, the member must have the following parameters
(Context cx, Object[] args, Function ctorObj, boolean inNewExpr)and if it is a Method, be static and return an Object result.
Otherwise, if the FunctionObject will not be used to define a constructor, the member must be a static Method with parameters (Context cx, Scriptable thisObj, Object[] args, Function funObj)
and an Object result.When the function varargs form is called as part of a function call, the
args
parameter contains the arguments, withthisObj
set to the JavaScript 'this' value.funObj
is the function object for the invoked function.When the constructor varargs form is called or invoked while evaluating a
new
expression,args
contains the arguments,ctorObj
refers to this FunctionObject, andinNewExpr
is true if and only if anew
expression caused the call. This supports defining a function that has different behavior when called as a constructor than when invoked as a normal function call. (For example, the Boolean constructor, when called as a function, will convert to boolean rather than creating a new object.)
name
- the name of the functionmethodOrConstructor
- a java.lang.reflect.Method or a java.lang.reflect.Constructor
that defines the objectscope
- enclosing scope of functionScriptable
Method Detail |
public int getArity()
getArity
in class org.mozilla.javascript.BaseFunction
public int getLength()
getArity()
.getLength
in class org.mozilla.javascript.BaseFunction
public static java.lang.reflect.Method[] findMethods(java.lang.Class clazz, java.lang.String name)
Searches clazz
for methods with name
name
. Maintains a cache so that multiple
lookups on the same class are cheap.
clazz
- the class to searchname
- the name of the methods to findClass.getMethods()
public void addAsConstructor(Scriptable scope, Scriptable prototype)
Sets up the "prototype" and "constructor" properties. Also
calls setParent and setPrototype with appropriate values.
Then adds the function object as a property of the given scope, using
prototype.getClassName()
as the name of the property.
scope
- the scope in which to define the constructor (typically
the global object)prototype
- the prototype objectScriptable.setParentScope(org.mozilla.javascript.Scriptable)
,
Scriptable.setPrototype(org.mozilla.javascript.Scriptable)
,
Scriptable.getClassName()
public static java.lang.Object convertArg(Context cx, Scriptable scope, java.lang.Object arg, java.lang.Class desired)
public java.lang.Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws JavaScriptException
Implements Function.call.
call
in class org.mozilla.javascript.BaseFunction
JavaScriptException
- if the underlying Java method or
constructor threw an exceptionFunction.call(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[])
public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) throws JavaScriptException
Implements Function.construct.
construct
in class org.mozilla.javascript.BaseFunction
cx
- the current Context for this threadscope
- the scope to execute the function relative to. This
set to the value returned by getParentScope() except
when the function is called from a closure.args
- arguments to the constructorJavaScriptException
- if the underlying Java method or constructor
threw an exceptionFunction.construct(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object[])
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |