Class Getter
- java.lang.Object
-
- com.gentlyweb.utils.Getter
-
public class Getter extends 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 theGeneralComparator
, 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:- Public fields with the specified name.
- If no field is found then the name is converted to a "JavaBeans" get method, so a field name of value would be converted to getValue and that method is looked for. The method must take no arguments.
- If we don't find the get* method then we look for a method with the specified name. So a field name of value would mean that a method (that again takes no arguments) is looked for.
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).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getAccessor()
Class
getBaseClass()
static Method
getNoParmJavaGetMethod(String method, Class c)
Class
getType()
Get the class of the type of object we would return from thegetValue(Object)
method.Object
getValue(Object obj)
String
toString()
-
-
-
Constructor Detail
-
Getter
public Getter(String ref, Class clazz) throws IllegalArgumentException
Get the getter associated with the named reference. Return null if there isn't one, or if we can't access it.- Parameters:
ref
- The reference for the getter.clazz
- The Class to get the field from.- Throws:
IllegalArgumentException
-
-
Method Detail
-
getBaseClass
public Class getBaseClass()
-
getType
public Class getType()
Get the class of the type of object we would return from thegetValue(Object)
method.- Returns:
- The class.
-
getValue
public Object getValue(Object obj) throws IllegalAccessException, InvocationTargetException
-
getAccessor
public String getAccessor()
-
-