org.javalid.core.support
Class ReflectionSupport

java.lang.Object
  extended by org.javalid.core.support.ReflectionSupport

public class ReflectionSupport
extends java.lang.Object

Supportive class for reflective and/or class related methods.

Changes for 1.1

Since:
1.0
Version:
1.1
Author:
M.Reuvers

Method Summary
static java.lang.Object convertValueToType(java.lang.String value, java.lang.String type)
          Converts given value to given type, types supported are:
- java.lang.String
- java.lang.Byte
- java.lang.Short
- java.lang.Integer
- java.lang.Float
- java.lang.Double
- java.lang.Long
And for each listed one their primitive equivalent (byte, short, int etc.), except String.
static java.lang.Object createSimpleProxyForInterface(java.lang.ClassLoader loader, java.lang.Class<?> interfaceClass, java.lang.reflect.InvocationHandler handler)
          Creates a simple proxy based on given interface class
static java.lang.reflect.Field findField(java.lang.Class clazz, java.lang.String fieldName)
           
static java.lang.reflect.Field findField(java.lang.Object instance, java.lang.String fieldName)
          Finds field (private fields included).
static java.lang.reflect.Method findMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] paramTypes)
          Finds given methodName on given class, if found returns the method if not raises JavalidException
static java.lang.reflect.Method findMethod(java.lang.Object instance, java.lang.String methodName)
          Finds given methodName on given instance, if found returns the method if not found raises a JvException.
static java.lang.reflect.Method findMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Class[] paramTypes)
          Finds given methodName on given instance, if found returns the method if not found raises a JavalidException.
static java.lang.Class[] getClassValues(JvParam[] params, java.lang.Object[] valueParams, JvConfigurationWrapper jvConfigWrapper)
          Returns the classes for given parameter values (in the same order).
static java.lang.annotation.Annotation getDeclaredAnnotationOnClass(java.lang.Class clazz, java.lang.Class annotationClass)
           
static java.lang.String getPrimitiveAsObjectString(java.lang.String type)
           
static java.lang.Class getPrimitiveClassType(java.lang.String type)
          For given primitive type (byte,short,int,long,float,double) returns equivalent Class.
static java.lang.Object getValueFromField(java.lang.Object object, java.lang.reflect.Field field)
          Retrieves the value from given field, first calls setAccessible(true) to allow access to private fields.
static java.lang.Object getValueFromField(java.lang.Object object, java.lang.reflect.Field field, java.lang.String[] proxyRecognizers)
          Retrieves the value from given field, first calls setAccessible(true) to allow access to private fields.
static java.lang.Object[] getValuesForJvParams(java.lang.Object valueRequiringValidation, java.lang.String currentValidationPath, JvParam[] params, JvConfigurationWrapper configWrapper)
          Returns the values for given parameter.
static java.lang.Object instantiate(java.lang.Class clazz)
          Instantiates given class (must hava a public non-args constructor).
static java.lang.Object instantiate(java.lang.Class clazz, java.lang.Class[] params, java.lang.Object[] values)
          Instantiates given class.
static java.lang.Object instantiate(java.lang.String className)
          Convenience method, which tries to locate and create a class first and then calls instantiate(clazz).
static java.lang.Object instantiate(java.lang.String className, java.lang.Class[] params, java.lang.Object[] values)
          Uses given className and tries to locate it, then calls instantiate(..) method of this class.
static java.lang.Object invokeMethod(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object[] params)
          Invokes given method using the object and parameters provided.
static java.lang.Object invokeMethodByName(java.lang.Object object, java.lang.String methodName)
          Convenience method which invokes given method on given object and returns the value.
static boolean isPrimitiveClassType(java.lang.String type)
          Returns if given type is a primitive class type.
static boolean isPrimitiveSupportedType(java.lang.String type)
          Returns true if given type is supported by the framework (in core validation implementations or just in general).
static boolean isValidFieldForValidation(java.lang.reflect.Field field)
          Returns true if given field is non-static, basically we allow every field for validation except for static fields.
static boolean isValidMethodForValidation(java.lang.reflect.Method method)
          Returns true if given method conforms to the standard javabean standard for a get method.
static java.lang.String methodToProperty(java.lang.String methodName)
          Converts a get method to a property name.
static java.lang.String propertyToGetMethod(java.lang.String propertyName)
          Converts given property name to a valid get method name.
static java.lang.Class removeProxyClass(java.lang.Class currentClass, java.lang.String[] proxyRecognizers)
          Checks given class if it contains a proxyRecognizer, if so goes to the super class and checks again, until none is found anymore.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

propertyToGetMethod

public static final java.lang.String propertyToGetMethod(java.lang.String propertyName)
Converts given property name to a valid get method name. For instance firstName is converted to: getFirstName

Parameters:
propertyName - The name to make a get method name from
Returns:
Get method name for given property
Throws:
JavalidException - If propertyName is null or empty

methodToProperty

public static final java.lang.String methodToProperty(java.lang.String methodName)
Converts a get method to a property name. E.g. getXXX will return xXX. getMyName returns myName etc. Raises exception if not a get method or too short.

Parameters:
methodName - The method name
Returns:
Method name as a property

isValidMethodForValidation

public static final boolean isValidMethodForValidation(java.lang.reflect.Method method)
Returns true if given method conforms to the standard javabean standard for a get method. E.g. valid is: getString(), getMyValue(), getthis() etc. Get methods do not have parameters and are non-static and return a value (non-void).

Parameters:
method - The method to check
Returns:
True if a valid get method, false otherwise.

isValidFieldForValidation

public static final boolean isValidFieldForValidation(java.lang.reflect.Field field)
Returns true if given field is non-static, basically we allow every field for validation except for static fields.

Parameters:
field - The field to check
Returns:
True if it can be used, false otherwise.

invokeMethod

public static final java.lang.Object invokeMethod(java.lang.Object object,
                                                  java.lang.reflect.Method method,
                                                  java.lang.Object[] params)
Invokes given method using the object and parameters provided. Returns the value if the method returns a value, null if the method does not return a value.

Parameters:
object - The object to invoke the method on (or null if static method)
method - The method to invoke
params - The parameters of the method (can be null if no parameters)
Returns:
Value of the method invocation or null if method does not return a value

invokeMethodByName

public static final java.lang.Object invokeMethodByName(java.lang.Object object,
                                                        java.lang.String methodName)
Convenience method which invokes given method on given object and returns the value.

Parameters:
object - The object to call the method on
methodName - The method to invoke
Returns:
Object
Since:
1.2

getValueFromField

public static final java.lang.Object getValueFromField(java.lang.Object object,
                                                       java.lang.reflect.Field field)
Retrieves the value from given field, first calls setAccessible(true) to allow access to private fields.

Parameters:
object - The instance the field is from
field - The field
Returns:
Value retrieved from field

getValueFromField

public static final java.lang.Object getValueFromField(java.lang.Object object,
                                                       java.lang.reflect.Field field,
                                                       java.lang.String[] proxyRecognizers)
Retrieves the value from given field, first calls setAccessible(true) to allow access to private fields. Looks up actual instance if proxies are involved (if given).

Parameters:
object - The instanace the field is from
field - The field
proxyRecognizers - Proxy recognizers, assures that the actual instance is looked up instead, can be null.
Returns:
Value retrieved from the field
Since:
1.1 TODO: Move interface proxy checks to configuration input!

instantiate

public static final java.lang.Object instantiate(java.lang.String className)
Convenience method, which tries to locate and create a class first and then calls instantiate(clazz). Raises JvException if lookup class and/or initialization of default instance failed.

Parameters:
className - The className to create instance for
Returns:
Initialized instance of className

instantiate

public static final java.lang.Object instantiate(java.lang.Class clazz)
Instantiates given class (must hava a public non-args constructor). Raises JvException if instantiation failed for some reason.

Parameters:
clazz - The class to instantiate.
Returns:
Initialized instance of clazz

instantiate

public static final java.lang.Object instantiate(java.lang.String className,
                                                 java.lang.Class[] params,
                                                 java.lang.Object[] values)
Uses given className and tries to locate it, then calls instantiate(..) method of this class.

Parameters:
className - The name of a class
params -
values -
Returns:
Instance
See Also:
instantiate(Class clazz, Class[] params, Object[] values)

instantiate

public static final java.lang.Object instantiate(java.lang.Class clazz,
                                                 java.lang.Class[] params,
                                                 java.lang.Object[] values)
Instantiates given class. Locates constructor using given params (types) and then creates a new instance with it.

Parameters:
clazz - The clazz to use
params - The parameter types of the constructor to lookup
values - The values to use when instantiating
Returns:
Newly created instance
Throws:
JavalidException - for any error that occurs during creation

findMethod

public static final java.lang.reflect.Method findMethod(java.lang.Object instance,
                                                        java.lang.String methodName)
Finds given methodName on given instance, if found returns the method if not found raises a JvException. Parameter must not have parameters.

Parameters:
instance - The instance to check
methodName - The methodname to lookup
Returns:
Method if found

findMethod

public static final java.lang.reflect.Method findMethod(java.lang.Object instance,
                                                        java.lang.String methodName,
                                                        java.lang.Class[] paramTypes)
Finds given methodName on given instance, if found returns the method if not found raises a JavalidException.

Parameters:
instance - The instance to check
methodName - The methodname to lookup
paramTypes - The formal parameter types for this method
Returns:
Method if found

findMethod

public static final java.lang.reflect.Method findMethod(java.lang.Class clazz,
                                                        java.lang.String methodName,
                                                        java.lang.Class[] paramTypes)
Finds given methodName on given class, if found returns the method if not raises JavalidException

Parameters:
clazz - The clazz
methodName - The method name
paramTypes - The formal parameter types for this method
Returns:
Method if found

findField

public static final java.lang.reflect.Field findField(java.lang.Object instance,
                                                      java.lang.String fieldName)
Finds field (private fields included).

Parameters:
instance - Instance
fieldName - Name of field
Returns:
Field found or null if not found

findField

public static final java.lang.reflect.Field findField(java.lang.Class clazz,
                                                      java.lang.String fieldName)

getValuesForJvParams

public static final java.lang.Object[] getValuesForJvParams(java.lang.Object valueRequiringValidation,
                                                            java.lang.String currentValidationPath,
                                                            JvParam[] params,
                                                            JvConfigurationWrapper configWrapper)
Returns the values for given parameter. The parameters are initialized based on their configuration. The return value of each parameter is stored inside the Object[] array. Any error occurring raises a JvException.

Parameters:
valueRequiringValidation - The actual value that requires validation, this is used when one of the JvParam instances require to pass this as parameter.
currentValidationPath - The current path of the validation
params - The parameters to initialize
configWrapper - The configuration of the jv framework
Returns:
Object[] array containing the initialized parameters (their values)

convertValueToType

public static final java.lang.Object convertValueToType(java.lang.String value,
                                                        java.lang.String type)
Converts given value to given type, types supported are:
- java.lang.String
- java.lang.Byte
- java.lang.Short
- java.lang.Integer
- java.lang.Float
- java.lang.Double
- java.lang.Long
And for each listed one their primitive equivalent (byte, short, int etc.), except String.

Parameters:
value - The value to convert, if null returns null. If empty and type is NOT java.lang.String it is set to null for the other types (problem with annotations that don't allow null values)
type - One of the supported types
Returns:
Converted value, which can be used as input for a parameter

isPrimitiveSupportedType

public static final boolean isPrimitiveSupportedType(java.lang.String type)
Returns true if given type is supported by the framework (in core validation implementations or just in general).

Currently supported are:
- java.lang.String
- java.lang.Byte
- java.lang.Short
- java.lang.Integer
- java.lang.Float
- java.lang.Double
- java.lang.Long
And for each listed one their primitive equivalent (byte, short, int etc.).

Parameters:
type - The type to check, if null false is returned.
Returns:
True if supported

getClassValues

public static final java.lang.Class[] getClassValues(JvParam[] params,
                                                     java.lang.Object[] valueParams,
                                                     JvConfigurationWrapper jvConfigWrapper)
Returns the classes for given parameter values (in the same order). If type on a JvParam is set, that one is used. Otherwise it is determined based on the value. If an index of the value array is null, the method tries to use the given type on params[index] and find a class for it instead.

Parameters:
params - The matching JvParams array with the actual values
valueParams - The value array to get classes from
jvConfigWrapper - Configuration, to check proxy of classes parameters (if required)
Returns:
Class array containing the classes of given values, careful null may be in one of the indices if one of the values was null!
Throws:
JavalidException - if classnotfoundexception occurs

isPrimitiveClassType

public static final boolean isPrimitiveClassType(java.lang.String type)
Returns if given type is a primitive class type. E.g. long is primitive class type (java.lang.Long isnt). byte,short,int,long,float,double are supported.

Parameters:
type - The type to check
Returns:
True if the type is a primitive class type, false otherwise

getPrimitiveClassType

public static final java.lang.Class getPrimitiveClassType(java.lang.String type)
For given primitive type (byte,short,int,long,float,double) returns equivalent Class.

Parameters:
type - Primitive type
Returns:
Class

getPrimitiveAsObjectString

public static final java.lang.String getPrimitiveAsObjectString(java.lang.String type)

removeProxyClass

public static final java.lang.Class removeProxyClass(java.lang.Class currentClass,
                                                     java.lang.String[] proxyRecognizers)
Checks given class if it contains a proxyRecognizer, if so goes to the super class and checks again, until none is found anymore. Returns the class that has no proxy recognizer anymore (thus the parent of the last class with proxy recognizer). If currentClass does not have a recognizer, returns the same one as the parameter.

Parameters:
currentClass - The class to check
proxyRecognizers - Array with proxyRecognizers
Returns:
The class without a proxy recognizer

createSimpleProxyForInterface

public static final java.lang.Object createSimpleProxyForInterface(java.lang.ClassLoader loader,
                                                                   java.lang.Class<?> interfaceClass,
                                                                   java.lang.reflect.InvocationHandler handler)
Creates a simple proxy based on given interface class

Parameters:
loader - The classloader to use
interfaceClass - The interface
handler - Handler to invoke
Returns:
Proxy
Since:
1.2

getDeclaredAnnotationOnClass

public static final java.lang.annotation.Annotation getDeclaredAnnotationOnClass(java.lang.Class clazz,
                                                                                 java.lang.Class annotationClass)