com.gentlyweb.utils
Class Accessor

java.lang.Object
  extended by com.gentlyweb.utils.Accessor

public class Accessor
extends java.lang.Object

This class is used to perform access into a Java object using a String value with a specific notation.

The Accessor uses a dot notation such as field1.method1.method2 to perform the access on an object. Each value in the notation refers to a field or method (a no argument method) of the type of the previous value. For instance if you have the following class structure:

 public class A 
 {
    public B = new B ();
 }
 
 public class B
 {
    public C = new C ();
 }
 
 public class C
 {
    String d = "";
 }
 

You would then use the notation: B.C.d to get access to field d in Class C.

The Accessor also supports a [ ] notation for accessing into Lists/Maps and Arrays. If the value between the [ ] is an integer then we look for the associated type to be either an array or a List, we then index into it with the integer. If the value is NOT an integer then we use assume the type is a Map and use it as a key into the Map.

For instance changing the example above:

 public class A 
 {
    public List vals = new ArrayList ();
 }
 

Now we could use: vals[X] where X is a positive integer. Or changing again:

 public class A 
 {
    public Map vals = new HashMap ();
 }
 

We could use: vals[VALUE] where VALUE would then be used as a Key into the vals HashMap.

Note: The Accessor is NOT designed to be an all purpose method of gaining access to a class. It has specific uses and for most will be of no use at all. It should be used for general purpose applications where you want to access specific fields of an object without having to know the exact type. One such application is in the GeneralComparator, in that case arbitrary Objects can be sorted without having to write complex Comparators or implementing the Comparable interface AND it gives the flexibility that sorting can be changed ad-hoc.

The Accessor looks for in the following order:

Note: we have had to add the 3rd type to allow for methods that don't follow JavaBeans conventions (there are loads in the standard Java APIs which makes accessing impossible otherwise).


Constructor Summary
Accessor()
           
 
Method Summary
static java.util.List getAccessorChain(java.lang.String accessorRef, java.lang.Class clazz)
          Get the Java field associated with the named field.
 java.lang.String getIndex()
           
static java.lang.Object getValueForIndex(java.lang.Object data, java.lang.String index)
           
static java.lang.Object getValueFromAccessorChain(java.lang.Object obj, java.util.List chain)
           
 boolean isIndexCorrectForType(java.lang.Class c)
           
 void setAccessor(java.lang.reflect.Field f)
           
 void setAccessor(java.lang.reflect.Method m)
           
 void setIndex(java.lang.String index)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Accessor

public Accessor()
Method Detail

getIndex

public java.lang.String getIndex()

setIndex

public void setIndex(java.lang.String index)

setAccessor

public void setAccessor(java.lang.reflect.Field f)

setAccessor

public void setAccessor(java.lang.reflect.Method m)

isIndexCorrectForType

public boolean isIndexCorrectForType(java.lang.Class c)

getValueFromAccessorChain

public static java.lang.Object getValueFromAccessorChain(java.lang.Object obj,
                                                         java.util.List chain)
                                                  throws java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException
Throws:
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

getValueForIndex

public static java.lang.Object getValueForIndex(java.lang.Object data,
                                                java.lang.String index)

getAccessorChain

public static java.util.List getAccessorChain(java.lang.String accessorRef,
                                              java.lang.Class clazz)
                                       throws java.lang.IllegalArgumentException
Get the Java field associated with the named field. Return null if there isn't one, or if we can't access it.

Parameters:
field - The name of the field.
clazz - The Class to get the field from.
Returns:
A List of Accessor objects used for delving into the classes to be sorted.
Throws:
java.lang.IllegalArgumentException