public class WrapFactory
extends java.lang.Object
Context.setWrapFactory(WrapFactory)
Once an instance of this class or an extension of this class is enabled
for a given context (by calling setWrapFactory on that context), Rhino
will call the methods of this class whenever it needs to wrap a value
resulting from a call to a Java method or an access to a Java field.Context.setWrapFactory(WrapFactory)
Constructor and Description |
---|
WrapFactory() |
Modifier and Type | Method and Description |
---|---|
boolean |
isJavaPrimitiveWrap()
Return
false if result of Java method, which is instance of
String , Number , Boolean and
Character , should be used directly as JavaScript primitive
type. |
void |
setJavaPrimitiveWrap(boolean value) |
java.lang.Object |
wrap(Context cx,
Scriptable scope,
java.lang.Object obj,
java.lang.Class<?> staticType)
Wrap the object.
|
Scriptable |
wrapAsJavaObject(Context cx,
Scriptable scope,
java.lang.Object javaObject,
java.lang.Class<?> staticType)
Wrap Java object as Scriptable instance to allow full access to its
methods and fields from JavaScript.
|
Scriptable |
wrapJavaClass(Context cx,
Scriptable scope,
java.lang.Class javaClass)
Wrap a Java class as Scriptable instance to allow access to its static
members and fields and use as constructor from JavaScript.
|
Scriptable |
wrapNewObject(Context cx,
Scriptable scope,
java.lang.Object obj)
Wrap an object newly created by a constructor call.
|
public java.lang.Object wrap(Context cx, Scriptable scope, java.lang.Object obj, java.lang.Class<?> staticType)
The value returned must be one of
cx
- the current Context for this threadscope
- the scope of the executing scriptobj
- the object to be wrapped. Note it can be null.staticType
- type hint. If security restrictions prevent to wrap
object based on its class, staticType will be used instead.public Scriptable wrapNewObject(Context cx, Scriptable scope, java.lang.Object obj)
cx
- the current Context for this threadscope
- the scope of the executing scriptobj
- the object to be wrappedpublic Scriptable wrapAsJavaObject(Context cx, Scriptable scope, java.lang.Object javaObject, java.lang.Class<?> staticType)
wrap(Context, Scriptable, Object, Class)
and
wrapNewObject(Context, Scriptable, Object)
call this method
when they can not convert javaObject to JavaScript primitive
value or JavaScript array.
Subclasses can override the method to provide custom wrappers for Java objects.
cx
- the current Context for this threadscope
- the scope of the executing scriptjavaObject
- the object to be wrappedstaticType
- type hint. If security restrictions prevent to wrap
object based on its class, staticType will be used instead.public Scriptable wrapJavaClass(Context cx, Scriptable scope, java.lang.Class javaClass)
Subclasses can override this method to provide custom wrappers for Java classes.
cx
- the current Context for this threadscope
- the scope of the executing scriptjavaClass
- the class to be wrappedpublic final boolean isJavaPrimitiveWrap()
false
if result of Java method, which is instance of
String
, Number
, Boolean
and
Character
, should be used directly as JavaScript primitive
type.
By default the method returns true to indicate that instances of
String
, Number
, Boolean
and
Character
should be wrapped as any other Java object and
scripts can access any Java method available in these objects.
Use setJavaPrimitiveWrap(boolean)
to change this.public final void setJavaPrimitiveWrap(boolean value)
isJavaPrimitiveWrap()