net.sf.easyweb4j.util
Class ReflectionUtil

java.lang.Object
  extended by net.sf.easyweb4j.util.ReflectionUtil

public class ReflectionUtil
extends Object

Utility class to facilitate reflections in EasyWeb4J.

This class is not a part of EasyWeb4J API. Its behavior may change in future versions and hence its direct use in applications is discouraged.

Author:
Chandra Sekar S

Constructor Summary
ReflectionUtil()
           
 
Method Summary
static boolean containsProperty(Object obj, String property)
          Checks whether the given object contains the specified property.
static Object getField(Class<?> clazz, String fieldName, Object source)
          Gets the value of a field within the given object.
static Object getField(Field field, Object source)
          Gets the value of a field within the given object.
static Object readProperty(Object obj, String property, ModelIntrospector introspector)
          Introspects the given Java Bean object and reads the value of the specified property.
static Field resolveField(Class<?> clazz, String fieldName)
          Resolves a field on a class with the given name.
static void setField(Field field, Object target, Object value)
          Sets the value of a field within the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil

public ReflectionUtil()
Method Detail

resolveField

public static Field resolveField(Class<?> clazz,
                                 String fieldName)
Resolves a field on a class with the given name. The purpose of this method is to convert all the checked exceptions reflection throws into unchecked exceptions to allow having static fields of type Field in classes.

Parameters:
clazz - The class whose field is to be resolved.
fieldName - The name of the field to resolve.
Returns:
The Field.

setField

public static void setField(Field field,
                            Object target,
                            Object value)
                     throws IllegalAccessException
Sets the value of a field within the given object.

Parameters:
field - The field to set.
target - The target object on which the field is to be set.
value - The value to set for the field.
Throws:
IllegalAccessException - If setting the field value fails.

getField

public static Object getField(Field field,
                              Object source)
                       throws IllegalAccessException
Gets the value of a field within the given object.

Parameters:
field - The field to get.
source - The source object from which the field is to be read.
Throws:
IllegalAccessException - If getting the field value fails.

getField

public static Object getField(Class<?> clazz,
                              String fieldName,
                              Object source)
                       throws SecurityException,
                              NoSuchFieldException,
                              IllegalAccessException
Gets the value of a field within the given object.

Parameters:
clazz - The class to which the field belongs.
fieldName - The name of the field to set.
source - The source object from which the field is to be read.
Returns:
The value of the field.
Throws:
NoSuchFieldException - If getting the field value fails.
SecurityException - If getting the field value fails.
IllegalAccessException - If getting the field value fails.

containsProperty

public static boolean containsProperty(Object obj,
                                       String property)
                                throws IntrospectionException
Checks whether the given object contains the specified property.

Parameters:
obj - The object to introspect.
property - The property to check for.
Returns:
true if the object contains the property false otherwise.
Throws:
IntrospectionException - If introspection of the object fails.

readProperty

public static Object readProperty(Object obj,
                                  String property,
                                  ModelIntrospector introspector)
                           throws IntrospectionException,
                                  InvocationTargetException,
                                  IllegalAccessException
Introspects the given Java Bean object and reads the value of the specified property. If the given object is an instance of Model, it uses ModelIntrospector.readProperty(Model, String) to read the value.

Parameters:
obj - The object to introspect.
property - The property whose value is to be read.
Returns:
Value of the property.
Throws:
IntrospectionException - If introspection of the object fails.
InvocationTargetException - If introspection of the object fails.
IllegalAccessException - If introspection of the object fails.


Copyright © 2009-2010 Chandra Sekar S. All Rights Reserved.