Using KeyObject
The KeyObject class is used by the results table to access methods and fields
of objects. This allows complex Java objects to be accessed using the same dot-syntax used
to access tables.
For example:
nM> res mystr "hello world"
nM> res mystr.charAt(2)
O: MYSTR.CHARAT(2) = l
nM> res mystr.class
O: MYSTR.CLASS = class java.lang.String
Finding the Keys
The simplified look-up of the keys is done as follows (see State
Diagram for details):
- If the root is
null , return null .
- If the root is a
Table or KeyVector then do a normal
Table lookup.
- If the root is an object that implements
Keyable call
getKey(..) or setKey(..) . If null is returned
continue on, otherwise return the result of calling getKey(..) or
setKey(..) .
- If the root is a Java array get the indexed value out of the array. (If the key is
"SIZE" then return the array size.)
- Look for methods/fields to access (see details below). If no methods/fields
exist continue on, otherwise return the result of invoking the method/field.
- If the root implements
Chainable goto the next object in the chain and
repeat.
- If the root is indexable get the indexed value.
When looking for methods to access KeyObject looks for public
methods whose parameters match and whose names...
- Match exactly.
- Match when compared ignoring case.
- Match the method acronym (the uppercase letters in the method name, e.g.
getXDelta has the acronym XD ).
- Match the method abbreviation (the first 2-4 letters of the method name,
getFrequency has the abbreviation FREQ ).
Note: If a class is not public only the public methods
in a public superclass or interface will be accessible.
Implementing Keyable
The Keyable interface allows a class to be accessible via a set of keys. To
use Keyable simply implement the three methods in the interface:
Method Name |
Description |
getKeys() |
Lists the available keys. |
getKey(key) |
Gets a key, returns null if unable to get a key via this interface. |
setKey(key,value) |
Sets a key, returns null if unable to get a key via this interface,
otherwise returns the new value of the key. |
Implementing Chainable
The Chainable interface tells KeyObject that there is another class
sitting below this one that KeyObject can pass the keys on to. To use
Chainable simply implement the two methods in the interface:
Method Name |
Description |
getNextLink() |
Gets the next link in the chain that KeyObject should pass keys down
to. |
getPrevLink() |
Gets the previous link in the chain (or null if not applicable).
KeyObject does not use this method but some of the query functions
do. |
State Diagram
The detailed state diagram for the key lookup performed by KeyObject can be found
in the KeyObject API documentation.
|