org.mozilla.javascript
Class FlattenedObject

java.lang.Object
  |
  +--org.mozilla.javascript.FlattenedObject

public class FlattenedObject
extends java.lang.Object

Manipulate a Scriptable object as if its prototype chain were flattened.

Compared to the Scriptable interface, FlattenedObject provides a view of Scriptable objects that is easier to use and more closely matches a script writer's view of a JavaScript object.

A FlattenedObject is "flattened" in the sense that multiple objects in a prototype chain appear to have their properties all appear in the FlattenedObject.

Another convenience provided by Flattened object is the ability to access properties by a single java.lang.Object id. This id is then converted into a String or an int before methods of the Scriptable object are called.

Author:
Norris Boyd
See Also:
Scriptable

Constructor Summary
FlattenedObject(Scriptable object)
          Construct a new FlattenedObject.
 
Method Summary
 java.lang.Object call(Context cx, Scriptable thisObj, java.lang.Object[] args)
          Consider this object to be a function, and call it.
 java.lang.Object callMethod(java.lang.Object id, java.lang.Object[] args)
          Get the property indicated by the id, and invoke it with the specified arguments.
 Scriptable construct(Context cx, java.lang.Object[] args)
          Consider this object to be a function, and invoke it as a constructor call.
 boolean deleteProperty(java.lang.Object id)
          Remove a property.
 java.lang.Object[] getIds()
          Return an array that contains the ids of the properties.
 Scriptable getObject()
          Get the associated Scriptable object.
 java.lang.Object getProperty(java.lang.Object id)
          Get a property of an object.
 boolean hasProperty(java.lang.Object id)
          Determine if a property exists in an object.
 void putProperty(java.lang.Object id, java.lang.Object value)
          Set a property of an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FlattenedObject

public FlattenedObject(Scriptable object)
Construct a new FlattenedObject.
Parameters:
object - the object to be viewed with flattened properties
Method Detail

getObject

public Scriptable getObject()
Get the associated Scriptable object.

hasProperty

public boolean hasProperty(java.lang.Object id)
Determine if a property exists in an object. This is a more convenient (and less efficient) form than Scriptable.has(). It returns true if and only if the property exists in this object or any of the objects in its prototype chain.
Parameters:
id - the property index, which may be either a String or a Number
Returns:
true if and only if the property exists in the prototype chain
See Also:
Scriptable.has(java.lang.String, org.mozilla.javascript.Scriptable)

getProperty

public java.lang.Object getProperty(java.lang.Object id)
Get a property of an object.

This is a more convenient (and less efficient) form than Scriptable.get(). It corresponds exactly to the expression obj[id] in JavaScript. This method will traverse the prototype chain of an object to find the property.

If the property does not exist in the object or its prototype chain, the undefined value will be returned.

Parameters:
id - the property index; can be a String or a Number; the String may contain characters representing a number
Returns:
the value of the property or the undefined value
See Also:
Scriptable.get(java.lang.String, org.mozilla.javascript.Scriptable), Context.getUndefinedValue()

putProperty

public void putProperty(java.lang.Object id,
                        java.lang.Object value)
Set a property of an object. This is a more convenient (and less efficient) form than that provided in Scriptable. It corresponds exactly to the expression obj[id] = val in JavaScript.

Parameters:
id - the property index, which may be either a String or a Number
value - the value of the property
See Also:
Scriptable.put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)

deleteProperty

public boolean deleteProperty(java.lang.Object id)
Remove a property. This method provides the functionality of the delete operator in JavaScript.
Parameters:
id - the property index, which may be either a String or a Number
Returns:
true if the property didn't exist, or existed and was removed
See Also:
Scriptable.delete(java.lang.String)

getIds

public java.lang.Object[] getIds()
Return an array that contains the ids of the properties.

This method will walk the prototype chain and collect the ids of all objects in the prototype chain.

If an id appears in more than one object in the prototype chain, it will only be in the array once. (So all the entries in the array will be unique respective to equals().)

See Also:
Scriptable.getIds()

call

public java.lang.Object call(Context cx,
                             Scriptable thisObj,
                             java.lang.Object[] args)
                      throws NotAFunctionException,
                             JavaScriptException
Consider this object to be a function, and call it.
Parameters:
cx - the current Context for this thread
thisObj - the JavaScript 'this' for the call
args - the arguments for the call
Returns:
the result of the JavaScript function call
Throws:
NotAFunctionException - if this object is not a function
JavaScriptException - if an uncaught JavaScript exception occurred while executing the function
See Also:
Function.call(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[])

construct

public Scriptable construct(Context cx,
                            java.lang.Object[] args)
                     throws NotAFunctionException,
                            JavaScriptException
Consider this object to be a function, and invoke it as a constructor call.
Parameters:
cx - the current Context for this thread
args - the arguments for the constructor call
Returns:
the allocated object
Throws:
NotAFunctionException - if this object is not a function
JavaScriptException - if an uncaught JavaScript exception occurred while executing the constructor
See Also:
Function.construct(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object[])

callMethod

public java.lang.Object callMethod(java.lang.Object id,
                                   java.lang.Object[] args)
                            throws PropertyException,
                                   NotAFunctionException,
                                   JavaScriptException
Get the property indicated by the id, and invoke it with the specified arguments.

For example, for a FlattenedObject obj, and a Java array a consisting of a single string "hi", the call

 obj.callMethod("m", a)
is equivalent to the JavaScript code obj.m("hi").

If the property is not found or is not a function, an exception will be thrown.

Parameters:
id - the Number or String to use to find the function property to call
args - the arguments for the constructor call
Returns:
the result of the call
Throws:
PropertyException - if the designated property was not found
NotAFunctionException - if this object is not a function
JavaScriptException - if an uncaught JavaScript exception occurred while executing the method
See Also:
Function.call(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[])