Class FunctionObject
- All Implemented Interfaces:
Serializable,Callable,ConstProperties,Constructable,DebuggableObject,Function,IdFunctionCall,Scriptable,SymbolScriptable
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.mozilla.javascript.ScriptableObject
ScriptableObject.KeyComparator -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate booleanprivate booleanstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int(package private) MemberBoxprivate intprivate intprivate static booleanprivate static final longprivate byte[]private static final shortprivate static final shortFields inherited from class org.mozilla.javascript.BaseFunction
GENERATOR_FUNCTION_CLASSFields inherited from class org.mozilla.javascript.ScriptableObject
CONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONSTFields inherited from interface org.mozilla.javascript.Scriptable
NOT_FOUND -
Constructor Summary
ConstructorsConstructorDescriptionFunctionObject(String name, Member methodOrConstructor, Scriptable scope) Create a JavaScript function object from a Java method. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAsConstructor(Scriptable scope, Scriptable prototype) Define this function as a JavaScript constructor.call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) Performs conversions on argument types if needed and invokes the underlying Java method or constructor.static ObjectconvertArg(Context cx, Scriptable scope, Object arg, int typeTag) static ObjectconvertArg(Context cx, Scriptable scope, Object arg, Class<?> desired) Deprecated.createObject(Context cx, Scriptable scope) Return newScriptableinstance using the default constructor for the class of the underlying Java method.(package private) static MethodfindSingleMethod(Method[] methods, String name) intgetArity()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).intReturn the same value asgetArity().(package private) static Method[]getMethodList(Class<?> clazz) Returns all public methods declared by the specified class.Get Java method or constructor this function represent.static intgetTypeTag(Class<?> type) (package private) voidinitAsConstructor(Scriptable scope, Scriptable prototype) (package private) boolean(package private) booleanprivate voidMethods inherited from class org.mozilla.javascript.BaseFunction
construct, decompile, execIdCall, fillConstructorProperties, findInstanceIdInfo, findPrototypeId, getClassName, getClassPrototype, getInstanceIdName, getInstanceIdValue, getMaxInstanceId, getPrototypeProperty, getTypeOf, hasInstance, hasPrototypeProperty, init, initAsGeneratorFunction, initPrototypeId, isApply, isApplyOrCall, isGeneratorFunction, setImmunePrototypeProperty, setInstanceIdAttributes, setInstanceIdValue, setPrototypeProperty, setPrototypePropertyAttributes, setStandardPropertyAttributes, setupDefaultPrototypeMethods inherited from class org.mozilla.javascript.IdScriptableObject
activatePrototypeMap, addIdFunctionProperty, defaultGet, defaultHas, defaultPut, defineOwnProperty, delete, delete, ensureType, exportAsJSClass, findInstanceIdInfo, findPrototypeId, get, get, getAttributes, getAttributes, getIds, getOwnPropertyDescriptor, has, has, hasPrototypeMap, initPrototypeConstructor, initPrototypeMethod, initPrototypeMethod, initPrototypeMethod, initPrototypeValue, initPrototypeValue, instanceIdInfo, put, put, setAttributesMethods inherited from class org.mozilla.javascript.ScriptableObject
addLazilyInitializedValue, applyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildClassCtor, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, checkValidAttributes, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureSymbolScriptable, equivalentValues, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, has, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, putConst, putConstProperty, putProperty, putProperty, putProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, sizeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.mozilla.javascript.Scriptable
delete, delete, get, get, getDefaultValue, getIds, getParentScope, getPrototype, has, has, put, put, setParentScope, setPrototype
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
VARARGS_METHOD
private static final short VARARGS_METHOD- See Also:
-
VARARGS_CTOR
private static final short VARARGS_CTOR- See Also:
-
sawSecurityException
private static boolean sawSecurityException -
JAVA_UNSUPPORTED_TYPE
public static final int JAVA_UNSUPPORTED_TYPE- See Also:
-
JAVA_STRING_TYPE
public static final int JAVA_STRING_TYPE- See Also:
-
JAVA_INT_TYPE
public static final int JAVA_INT_TYPE- See Also:
-
JAVA_BOOLEAN_TYPE
public static final int JAVA_BOOLEAN_TYPE- See Also:
-
JAVA_DOUBLE_TYPE
public static final int JAVA_DOUBLE_TYPE- See Also:
-
JAVA_SCRIPTABLE_TYPE
public static final int JAVA_SCRIPTABLE_TYPE- See Also:
-
JAVA_OBJECT_TYPE
public static final int JAVA_OBJECT_TYPE- See Also:
-
member
MemberBox member -
functionName
-
typeTags
private transient byte[] typeTags -
parmsLength
private int parmsLength -
hasVoidReturn
private transient boolean hasVoidReturn -
returnTypeTag
private transient int returnTypeTag -
isStatic
private boolean isStatic
-
-
Constructor Details
-
FunctionObject
Create a JavaScript function object from a Java method.The
memberargument 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, int, 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
argsparameter contains the arguments, withthisObjset to the JavaScript 'this' value.funObjis the function object for the invoked function.When the constructor varargs form is called or invoked while evaluating a
newexpression,argscontains the arguments,ctorObjrefers to this FunctionObject, andinNewExpris true if and only if anewexpression 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.)- Parameters:
name- the name of the functionmethodOrConstructor- a java.lang.reflect.Method or a java.lang.reflect.Constructor that defines the objectscope- enclosing scope of function- See Also:
-
-
Method Details
-
getTypeTag
- Returns:
- One of
JAVA_*_TYPEconstants to indicate desired type orJAVA_UNSUPPORTED_TYPEif the convertion is not possible
-
convertArg
-
getArity
public 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).- Overrides:
getArityin classBaseFunction
-
getLength
public int getLength()Return the same value asgetArity().- Overrides:
getLengthin classBaseFunction
-
getFunctionName
- Overrides:
getFunctionNamein classBaseFunction
-
getMethodOrConstructor
Get Java method or constructor this function represent. -
findSingleMethod
-
getMethodList
Returns all public methods declared by the specified class. This excludes inherited methods.- Parameters:
clazz- the class from which to pull public declared methods- Returns:
- the public methods declared in the specified class
- See Also:
-
addAsConstructor
Define this function as a JavaScript constructor.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.- Parameters:
scope- the scope in which to define the constructor (typically the global object)prototype- the prototype object- See Also:
-
initAsConstructor
-
convertArg
@Deprecated public static Object convertArg(Context cx, Scriptable scope, Object arg, Class<?> desired) Deprecated.UsegetTypeTag(Class)andconvertArg(Context, Scriptable, Object, int)for type conversion. -
call
Performs conversions on argument types if needed and invokes the underlying Java method or constructor.Implements Function.call.
- Specified by:
callin interfaceCallable- Specified by:
callin interfaceFunction- Overrides:
callin classBaseFunction- Parameters:
cx- the current Context for this threadscope- the scope to execute the function relative to. This is set to the value returned by getParentScope() except when the function is called from a closure.thisObj- the JavaScriptthisobjectargs- the array of arguments- Returns:
- the result of the call
- See Also:
-
createObject
Return newScriptableinstance using the default constructor for the class of the underlying Java method. Return null to indicate that the call method should be used to create new objects.- Overrides:
createObjectin classBaseFunction
-
isVarArgsMethod
boolean isVarArgsMethod() -
isVarArgsConstructor
boolean isVarArgsConstructor() -
readObject
- Throws:
IOExceptionClassNotFoundException
-
getTypeTag(Class)andconvertArg(Context, Scriptable, Object, int)for type conversion.