jlibs.core.lang
Class BeanUtil

java.lang.Object
  extended by jlibs.core.lang.BeanUtil

public class BeanUtil
extends Object

This class contains helper methods for working with java beans

Author:
Santhosh Kumar T

Field Summary
static String GET
          prefix used by non-boolean getter methods
static String IS
          prefix used by boolean getter methods
static String SET
          prefix used by setter methods
 
Constructor Summary
BeanUtil()
           
 
Method Summary
static Method getGetterMethod(Class<?> beanClass, String property)
          Returns getter method for property in specified beanClass
static Method getGetterMethod(Class<?> beanClass, String property, Class propertyType)
          Returns getter method for property in specified beanClass
static String getMethodSuffix(String property)
           
static
<T> T
getProperty(Object bean, String property)
          Returns the value of the specified property in given bean
static String getPropertyName(String methodName)
           
static Class getPropertyType(Class<?> beanClass, String property)
          Returns the type of property in given beanClass
static Method getSetterMethod(Class<?> beanClass, String property)
          Returns setter method for property in specified beanClass
static Method getSetterMethod(Class<?> beanClass, String property, Class propertyType)
          Returns setter method for property in specified beanClass
static void setProperty(Object bean, String property, Object value)
          Sets the value of the specified property in given bean
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET

public static final String GET
prefix used by non-boolean getter methods

See Also:
Constant Field Values

IS

public static final String IS
prefix used by boolean getter methods

See Also:
Constant Field Values

SET

public static final String SET
prefix used by setter methods

See Also:
Constant Field Values
Constructor Detail

BeanUtil

public BeanUtil()
Method Detail

getMethodSuffix

public static String getMethodSuffix(String property)

getPropertyName

public static String getPropertyName(String methodName)

getGetterMethod

public static Method getGetterMethod(Class<?> beanClass,
                                     String property)
Returns getter method for property in specified beanClass

Parameters:
beanClass - bean class
property - name of the property
Returns:
getter method. null if property is not found
See Also:
getGetterMethod(Class, String, Class)

getGetterMethod

public static Method getGetterMethod(Class<?> beanClass,
                                     String property,
                                     Class propertyType)
Returns getter method for property in specified beanClass

Parameters:
beanClass - bean class
property - name of the property
propertyType - type of the property. This is used to compute getter method name.
Returns:
getter method. null if property is not found
See Also:
getGetterMethod(Class, String)

getSetterMethod

public static Method getSetterMethod(Class<?> beanClass,
                                     String property)
Returns setter method for property in specified beanClass

Parameters:
beanClass - bean class
property - name of the property
Returns:
setter method. null if property is not found, or it is readonly property
See Also:
getSetterMethod(Class, String, Class)

getSetterMethod

public static Method getSetterMethod(Class<?> beanClass,
                                     String property,
                                     Class propertyType)
Returns setter method for property in specified beanClass

Parameters:
beanClass - bean class
property - name of the property
propertyType - type of the property. This is used to compute setter method name.
Returns:
setter method. null if property is not found, or it is readonly property
See Also:
getSetterMethod(Class, String)

getPropertyType

public static Class getPropertyType(Class<?> beanClass,
                                    String property)
Returns the type of property in given beanClass

Parameters:
beanClass - bean class
property - name of the property
Returns:
null if the property is not found. otherwise returns the type of the property

getProperty

public static <T> T getProperty(Object bean,
                                String property)
                     throws InvocationTargetException
Returns the value of the specified property in given bean

Parameters:
bean - bean object
property - property name whose value needs to be returned
Returns:
value of the property.
Throws:
InvocationTargetException - if method invocation fails
NullPointerException - if property is not found in bean

setProperty

public static void setProperty(Object bean,
                               String property,
                               Object value)
                        throws InvocationTargetException
Sets the value of the specified property in given bean

Parameters:
bean - bean object
property - property name whose value needs to be set
value - value to be set
Throws:
InvocationTargetException - if method invocation fails
NullPointerException - if property is not found in bean or it is readonly property


Copyright © 2018. All rights reserved.