Package helma.scripting.rhino
Class JSAdapter
java.lang.Object
helma.scripting.rhino.JSAdapter
- All Implemented Interfaces:
org.mozilla.javascript.Callable
,org.mozilla.javascript.Function
,org.mozilla.javascript.Scriptable
public final class JSAdapter
extends Object
implements org.mozilla.javascript.Scriptable, org.mozilla.javascript.Function
JSAdapter is java.lang.reflect.Proxy equivalent for JavaScript. JSAdapter
calls specially named JavaScript methods on an adaptee object when property
access is attempted on it.
Example:
var y = {
__get__ : function (name) { ... }
__has__ : function (name) { ... }
__put__ : function (name, value) {...}
__delete__ : function (name) { ... }
__getIds__ : function () { ... }
};
var x = new JSAdapter(y);
x.i; // calls y.__get__
i in x; // calls y.__has__
x.p = 10; // calls y.__put__
delete x.p; // calls y.__delete__
for (i in x) { print(i); } // calls y.__getIds__
If a special JavaScript method is not found in the adaptee, then JSAdapter
forwards the property access to the adaptee itself.
JavaScript caller of adapter object is isolated from the fact that
the property access/mutation/deletion are really calls to
JavaScript methods on adaptee. Use cases include 'smart'
properties, property access tracing/debugging, encaptulation with
easy client access - in short JavaScript becomes more "Self" like.
Note that Rhino already supports special properties like __proto__
(to set, get prototype), __parent__ (to set, get parent scope). We
follow the same double underscore nameing convention here. Similarly
the name JSAdapter is derived from JavaAdapter -- which is a facility
to extend, implement Java classes/interfaces by JavaScript.
- Since:
- 1.6
-
Field Summary
Fields inherited from interface org.mozilla.javascript.Scriptable
NOT_FOUND
-
Method Summary
Modifier and TypeMethodDescriptioncall
(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, Object[] args) org.mozilla.javascript.Scriptable
construct
(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, Object[] args) void
delete
(int index) void
get
(int index, org.mozilla.javascript.Scriptable start) org.mozilla.javascript.Scriptable
getDefaultValue
(Class hint) Object[]
getIds()
org.mozilla.javascript.Scriptable
org.mozilla.javascript.Scriptable
boolean
has
(int index, org.mozilla.javascript.Scriptable start) boolean
boolean
hasInstance
(org.mozilla.javascript.Scriptable scriptable) static void
init
(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, boolean sealed) void
void
void
setAdaptee
(org.mozilla.javascript.Scriptable adaptee) void
setParentScope
(org.mozilla.javascript.Scriptable parent) void
setPrototype
(org.mozilla.javascript.Scriptable prototype)
-
Method Details
-
init
public static void init(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, boolean sealed) throws org.mozilla.javascript.RhinoException - Throws:
org.mozilla.javascript.RhinoException
-
getClassName
- Specified by:
getClassName
in interfaceorg.mozilla.javascript.Scriptable
-
get
- Specified by:
get
in interfaceorg.mozilla.javascript.Scriptable
-
get
- Specified by:
get
in interfaceorg.mozilla.javascript.Scriptable
-
has
- Specified by:
has
in interfaceorg.mozilla.javascript.Scriptable
-
has
public boolean has(int index, org.mozilla.javascript.Scriptable start) - Specified by:
has
in interfaceorg.mozilla.javascript.Scriptable
-
put
- Specified by:
put
in interfaceorg.mozilla.javascript.Scriptable
-
put
- Specified by:
put
in interfaceorg.mozilla.javascript.Scriptable
-
delete
- Specified by:
delete
in interfaceorg.mozilla.javascript.Scriptable
-
delete
public void delete(int index) - Specified by:
delete
in interfaceorg.mozilla.javascript.Scriptable
-
getPrototype
public org.mozilla.javascript.Scriptable getPrototype()- Specified by:
getPrototype
in interfaceorg.mozilla.javascript.Scriptable
-
setPrototype
public void setPrototype(org.mozilla.javascript.Scriptable prototype) - Specified by:
setPrototype
in interfaceorg.mozilla.javascript.Scriptable
-
getParentScope
public org.mozilla.javascript.Scriptable getParentScope()- Specified by:
getParentScope
in interfaceorg.mozilla.javascript.Scriptable
-
setParentScope
public void setParentScope(org.mozilla.javascript.Scriptable parent) - Specified by:
setParentScope
in interfaceorg.mozilla.javascript.Scriptable
-
getIds
- Specified by:
getIds
in interfaceorg.mozilla.javascript.Scriptable
-
hasInstance
public boolean hasInstance(org.mozilla.javascript.Scriptable scriptable) - Specified by:
hasInstance
in interfaceorg.mozilla.javascript.Scriptable
-
getDefaultValue
- Specified by:
getDefaultValue
in interfaceorg.mozilla.javascript.Scriptable
-
call
public Object call(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, Object[] args) throws org.mozilla.javascript.RhinoException - Specified by:
call
in interfaceorg.mozilla.javascript.Callable
- Specified by:
call
in interfaceorg.mozilla.javascript.Function
- Throws:
org.mozilla.javascript.RhinoException
-
construct
public org.mozilla.javascript.Scriptable construct(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, Object[] args) throws org.mozilla.javascript.RhinoException - Specified by:
construct
in interfaceorg.mozilla.javascript.Function
- Throws:
org.mozilla.javascript.RhinoException
-
getAdaptee
public org.mozilla.javascript.Scriptable getAdaptee() -
setAdaptee
public void setAdaptee(org.mozilla.javascript.Scriptable adaptee)
-