Class JSAdapter

  • All Implemented Interfaces:
    org.mozilla.javascript.Callable, org.mozilla.javascript.Function, org.mozilla.javascript.Scriptable

    public final class JSAdapter
    extends java.lang.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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object call​(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, java.lang.Object[] args)  
      org.mozilla.javascript.Scriptable construct​(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, java.lang.Object[] args)  
      void delete​(int index)  
      void delete​(java.lang.String name)  
      java.lang.Object get​(int index, org.mozilla.javascript.Scriptable start)  
      java.lang.Object get​(java.lang.String name, org.mozilla.javascript.Scriptable start)  
      org.mozilla.javascript.Scriptable getAdaptee()  
      java.lang.String getClassName()  
      java.lang.Object getDefaultValue​(java.lang.Class hint)  
      java.lang.Object[] getIds()  
      org.mozilla.javascript.Scriptable getParentScope()  
      org.mozilla.javascript.Scriptable getPrototype()  
      boolean has​(int index, org.mozilla.javascript.Scriptable start)  
      boolean has​(java.lang.String name, org.mozilla.javascript.Scriptable start)  
      boolean hasInstance​(org.mozilla.javascript.Scriptable scriptable)  
      static void init​(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, boolean sealed)  
      void put​(int index, org.mozilla.javascript.Scriptable start, java.lang.Object value)  
      void put​(java.lang.String name, org.mozilla.javascript.Scriptable start, java.lang.Object value)  
      void setAdaptee​(org.mozilla.javascript.Scriptable adaptee)  
      void setParentScope​(org.mozilla.javascript.Scriptable parent)  
      void setPrototype​(org.mozilla.javascript.Scriptable prototype)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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

        public java.lang.String getClassName()
        Specified by:
        getClassName in interface org.mozilla.javascript.Scriptable
      • get

        public java.lang.Object get​(java.lang.String name,
                                    org.mozilla.javascript.Scriptable start)
        Specified by:
        get in interface org.mozilla.javascript.Scriptable
      • get

        public java.lang.Object get​(int index,
                                    org.mozilla.javascript.Scriptable start)
        Specified by:
        get in interface org.mozilla.javascript.Scriptable
      • has

        public boolean has​(java.lang.String name,
                           org.mozilla.javascript.Scriptable start)
        Specified by:
        has in interface org.mozilla.javascript.Scriptable
      • has

        public boolean has​(int index,
                           org.mozilla.javascript.Scriptable start)
        Specified by:
        has in interface org.mozilla.javascript.Scriptable
      • put

        public void put​(java.lang.String name,
                        org.mozilla.javascript.Scriptable start,
                        java.lang.Object value)
        Specified by:
        put in interface org.mozilla.javascript.Scriptable
      • put

        public void put​(int index,
                        org.mozilla.javascript.Scriptable start,
                        java.lang.Object value)
        Specified by:
        put in interface org.mozilla.javascript.Scriptable
      • delete

        public void delete​(java.lang.String name)
        Specified by:
        delete in interface org.mozilla.javascript.Scriptable
      • delete

        public void delete​(int index)
        Specified by:
        delete in interface org.mozilla.javascript.Scriptable
      • getPrototype

        public org.mozilla.javascript.Scriptable getPrototype()
        Specified by:
        getPrototype in interface org.mozilla.javascript.Scriptable
      • setPrototype

        public void setPrototype​(org.mozilla.javascript.Scriptable prototype)
        Specified by:
        setPrototype in interface org.mozilla.javascript.Scriptable
      • getParentScope

        public org.mozilla.javascript.Scriptable getParentScope()
        Specified by:
        getParentScope in interface org.mozilla.javascript.Scriptable
      • setParentScope

        public void setParentScope​(org.mozilla.javascript.Scriptable parent)
        Specified by:
        setParentScope in interface org.mozilla.javascript.Scriptable
      • getIds

        public java.lang.Object[] getIds()
        Specified by:
        getIds in interface org.mozilla.javascript.Scriptable
      • hasInstance

        public boolean hasInstance​(org.mozilla.javascript.Scriptable scriptable)
        Specified by:
        hasInstance in interface org.mozilla.javascript.Scriptable
      • getDefaultValue

        public java.lang.Object getDefaultValue​(java.lang.Class hint)
        Specified by:
        getDefaultValue in interface org.mozilla.javascript.Scriptable
      • call

        public java.lang.Object call​(org.mozilla.javascript.Context cx,
                                     org.mozilla.javascript.Scriptable scope,
                                     org.mozilla.javascript.Scriptable thisObj,
                                     java.lang.Object[] args)
                              throws org.mozilla.javascript.RhinoException
        Specified by:
        call in interface org.mozilla.javascript.Callable
        Specified by:
        call in interface org.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,
                                                           java.lang.Object[] args)
                                                    throws org.mozilla.javascript.RhinoException
        Specified by:
        construct in interface org.mozilla.javascript.Function
        Throws:
        org.mozilla.javascript.RhinoException
      • getAdaptee

        public org.mozilla.javascript.Scriptable getAdaptee()
      • setAdaptee

        public void setAdaptee​(org.mozilla.javascript.Scriptable adaptee)