net.sf.sasl.common.helper
Class ReflectionHelper

java.lang.Object
  extended by net.sf.sasl.common.helper.ReflectionHelper

public class ReflectionHelper
extends Object

The class defines some helper methods for reflection operations.
Actually all of them could have been defined in a static way. Because of the impossibility to mock static method calls without changing the byte code or do some jar-hell tricks, non of them is defined static.

Since:
0.0.1 (sasl-common-aspect-library)
Author:
Philipp Förmer

Constructor Summary
ReflectionHelper()
           
 
Method Summary
 List<Class<?>> getAllSuperClasses(Class<?> clazz)
          Returns a list of all super classes of the over given class.
 Class<?> getClassByName(String className)
          Tries to find the class object to the corresponding class name.
 List<Field> getCompletePropertyList(Class<?> clazz)
          Returns a list of all declared properties of the class and its super classes (not including Object).
 Method getPropertyGetterMethod(Class<?> clazz, String propertyName)
          Returns the bean getter method for the property or null, if there exists no such.
 Object getPropertyOverMethodOrField(Object targetObject, String propertyName)
          Tries to fetch the value of the property.
 Object getPropertyViaPath(Object targetObject, String path)
          Resolves the value of a property by a property path and returns the resolved value.
 boolean hasProperty(Class<?> clazz, String propertyName)
          Checks if there is a field or setter for the given propertyName defined in the class.
<T> T
instantiateObject(Class<T> clazz)
          Tries to instantiate a new object of the given class via the no argument constructor.
 boolean setPropertyOverMethodOrField(Object targetObject, String propertyName, Object value)
          Tries to set the given property with the given value on the given targetObject.
 String toBeanSetterMethodName(String propertyName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

getClassByName

public Class<?> getClassByName(String className)
Tries to find the class object to the corresponding class name. If no class for the name is found, than null will be returned.

Parameters:
className - null or non null
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)

getAllSuperClasses

public List<Class<?>> getAllSuperClasses(Class<?> clazz)
Returns a list of all super classes of the over given class. If clazz is null, than an empty list returned.

Parameters:
clazz - null or non null
Returns:
non null
Since:
0.0.1 (sasl-common-aspect-library)

setPropertyOverMethodOrField

public boolean setPropertyOverMethodOrField(Object targetObject,
                                            String propertyName,
                                            Object value)
Tries to set the given property with the given value on the given targetObject. Returns true, if the property could be setted via the corresponding setter method or via direct field access. The setter method has precedence over the direct field access. Returns false, if there is no such property or any exception occured by trying to set the value for the property.

Parameters:
targetObject - non null
propertyName - non null, non empty
value - null or non null
Returns:
Since:
0.0.1 (sasl-common-aspect-library)

hasProperty

public boolean hasProperty(Class<?> clazz,
                           String propertyName)
Checks if there is a field or setter for the given propertyName defined in the class. Returns true if, false else.

Parameters:
clazz - null or non null
propertyName - null or non null
Returns:
Since:
0.0.1 (sasl-common-aspect-library)

instantiateObject

public <T> T instantiateObject(Class<T> clazz)
Tries to instantiate a new object of the given class via the no argument constructor. If the instantiation succeeded than the instance is returned, else null.

Parameters:
clazz - null or non null
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)

toBeanSetterMethodName

public String toBeanSetterMethodName(String propertyName)
                              throws IllegalArgumentException
Parameters:
propertyName -
Returns:
Throws:
IllegalArgumentException
Since:
0.0.1 (sasl-common-aspect-library)

getCompletePropertyList

public List<Field> getCompletePropertyList(Class<?> clazz)
                                    throws IllegalArgumentException
Returns a list of all declared properties of the class and its super classes (not including Object).

Parameters:
clazz - non null.
Returns:
non null.
Throws:
IllegalArgumentException - if parameter clazz is null.
Since:
0.0.2 (sasl-common-aspect-library)

getPropertyGetterMethod

public Method getPropertyGetterMethod(Class<?> clazz,
                                      String propertyName)
                               throws IllegalArgumentException
Returns the bean getter method for the property or null, if there exists no such.

Parameters:
clazz - non null.
propertyName - non null
Returns:
null or non null.
Throws:
IllegalArgumentException - if clazz is null or propertyName does not have text.
Since:
0.0.2 (sasl-common-aspect-library)

getPropertyOverMethodOrField

public Object getPropertyOverMethodOrField(Object targetObject,
                                           String propertyName)
                                    throws IntrospectionException,
                                           IllegalArgumentException
Tries to fetch the value of the property. Firstly tries to fetch the value over the bean getter method. If this fails, than a normal field fetch will be tried.

Parameters:
targetObject - non null.
propertyName - non null.
Returns:
null or non null.
Throws:
IntrospectionException - if the value of the property could not be fetched, because there exists no bean getter method for the property or the property does not exists.
IllegalArgumentException - if targetObject is null or propertyName does not have text.
Since:
0.0.2 (sasl-common-aspect-library)

getPropertyViaPath

public Object getPropertyViaPath(Object targetObject,
                                 String path)
                          throws ReflectionException,
                                 NotResolveableException
Resolves the value of a property by a property path and returns the resolved value. For example if a "Customer" object is passed and the path is "address.street", than the value of "street" will be returned (under the assumption that "address" and "street" are properties of "Customer" and "Address"). The resolution only works if there exists no property with a null for all property path elements lesser than pathIndex(lastPropertyInPath).

Parameters:
targetObject - null or non null.
path - non null.
Returns:
null or non null.
Throws:
ReflectionException - if any reflection operation exception occurs, for example a property is not declared at a class.
NotResolveableException - if at some point of the path resolution a null value for a property occurs and the path could not get resolved anymore as of missing meta informations.
Since:
0.0.2 (sasl-common-aspect-library)


Copyright © 2010. All Rights Reserved.