com.massimo.utils
Class ReflectionHelper

java.lang.Object
  extended by com.massimo.utils.ReflectionHelper

public final class ReflectionHelper
extends Object

Helper for using reflection


Method Summary
static void clearFieldCache()
          Clear the Fields cache.
static void clearMethodCache()
          Clear the Methods cache.
static Object createObject(String className)
          Create an object of the supplied classname.
static Object createParameter(Object target, String name, Object val)
          Find the type of and create a parameter for attribute name's set method Introspect the target to get the parameter type
static Object createType(Class type, Object val)
          Utility method to create primitive types
static Object createType(String factoryClass, String factoryMethod, Object val)
          Create an object using the supplied factory class and method
static Field findField(Class clazz, String fieldName)
          Find a field in clazz called fieldName that satisfies the given modifiers.
static Method findMethod(Class clazz, String methodName, int modifiers)
          Find a method in clazz called methodName that satisfies the given modifiers.
static Method findSetterMethod(Object target, String name)
          Find a public setter method on a target
static void setAttr(Object target, Object value, String attrName)
          Use reflection to set an attribute to a value on the target object.
static void setAttr(Object target, Object value, String attrName, int attrIndex)
          Use reflection to set an attribute to a value on the target object.
static void setAttr(Object target, String name, Object value, int index)
          Use refection to set an attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clearFieldCache

public static void clearFieldCache()
Clear the Fields cache. Use to free memory after use.


clearMethodCache

public static void clearMethodCache()
Clear the Methods cache. Use to free memory after use.


createObject

public static Object createObject(String className)
Create an object of the supplied classname.

Parameters:
className -
Returns:
Object

createParameter

public static Object createParameter(Object target,
                                     String name,
                                     Object val)
Find the type of and create a parameter for attribute name's set method Introspect the target to get the parameter type

Parameters:
target -
name -
val -
Returns:
Object

createType

public static Object createType(Class type,
                                Object val)
Utility method to create primitive types

Parameters:
type -
val -
Returns:
Object If type is a primitive wrapper, returns (val.toString()). Otherwise, returns val
See Also:
Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Short.TYPE, Boolean.TYPE, Character.TYPE, Byte.TYPE, Void.TYPE

createType

public static Object createType(String factoryClass,
                                String factoryMethod,
                                Object val)
Create an object using the supplied factory class and method

Parameters:
factoryClass -
factoryMethod -
val -
Returns:
Object

findField

public static Field findField(Class clazz,
                              String fieldName)
Find a field in clazz called fieldName that satisfies the given modifiers. The result is cached for faster lookup on the next call.

Parameters:
clazz -
fieldName -
Returns:
Field

findMethod

public static Method findMethod(Class clazz,
                                String methodName,
                                int modifiers)
Find a method in clazz called methodName that satisfies the given modifiers. The result is cached for faster lookup on the next call.

Parameters:
clazz -
methodName -
modifiers -
Returns:
Method

findSetterMethod

public static Method findSetterMethod(Object target,
                                      String name)
Find a public setter method on a target

Parameters:
target - Object whose attribute we want to set
name - Name of the attribute we want to set
Returns:
setter Method object

setAttr

public static void setAttr(Object target,
                           Object value,
                           String attrName)
Use reflection to set an attribute to a value on the target object. Assumes set/get naming.

Parameters:
target -
value -
attrName -

setAttr

public static void setAttr(Object target,
                           Object value,
                           String attrName,
                           int attrIndex)
Use reflection to set an attribute to a value on the target object. Assumes set/get naming.

Parameters:
target -
value -
attrName -
attrIndex -

setAttr

public static void setAttr(Object target,
                           String name,
                           Object value,
                           int index)
Use refection to set an attribute. Create value then use reflection to set attribute name in the target Object

Parameters:
target -
name -
value -
index -