Package com.twelvemonkeys.lang
Class BeanUtil
java.lang.Object
com.twelvemonkeys.lang.BeanUtil
A utility class with some useful bean-related functions.
NOTE: This class is not considered part of the public API and may be changed without notice
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/BeanUtil.java#2 $
- Author:
- Harald Kuhr, last modified by $Author: haku $
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Configures the bean according to the given mapping.static void
Configures the bean according to the given mapping.static <T> T
createInstance
(Class<T> pClass, Object pParam) Creates an object from the given class' single argument constructor.static <T> T
createInstance
(Class<T> pClass, Object... pParams) Creates an object from the given class' constructor that matches the given paramaters.static Object
getPropertyValue
(Object pObject, String pProperty) Gets a property value from the given object, using reflection.static Object
invokeStaticMethod
(Class<?> pClass, String pMethod, Object pParam) Gets an object from any given static method, with the given parameter.static Object
invokeStaticMethod
(Class<?> pClass, String pMethod, Object... pParams) Gets an object from any given static method, with the given parameter.static void
setPropertyValue
(Object pObject, String pProperty, Object pValue) Sets the property value to an object using reflection.
-
Method Details
-
getPropertyValue
Gets a property value from the given object, using reflection. Now supports getting values from properties of properties (recursive).- Parameters:
pObject
- The object to get the property frompProperty
- The name of the property- Returns:
- A string containing the value of the given property, or
null
if it can not be found.
-
setPropertyValue
public static void setPropertyValue(Object pObject, String pProperty, Object pValue) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Sets the property value to an object using reflection. Supports setting values of properties that are properties of properties (recursive).- Parameters:
pObject
- The object to get a property frompProperty
- The name of the propertypValue
- The property value- Throws:
NoSuchMethodException
- if there's no write method for the given propertyInvocationTargetException
- if invoking the write method failedIllegalAccessException
- if the caller class has no access to the write method
-
createInstance
Creates an object from the given class' single argument constructor.- Parameters:
pClass
- The class to create instance frompParam
- The parameters to the constructor- Returns:
- The object created from the constructor. If the constructor could not be invoked for any reason, null is returned.
- Throws:
InvocationTargetException
- if the constructor failed
-
createInstance
public static <T> T createInstance(Class<T> pClass, Object... pParams) throws InvocationTargetException Creates an object from the given class' constructor that matches the given paramaters.- Parameters:
pClass
- The class to create instance frompParams
- The parameters to the constructor- Returns:
- The object created from the constructor. If the constructor could not be invoked for any reason, null is returned.
- Throws:
InvocationTargetException
- if the constructor failed
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object pParam) throws InvocationTargetException Gets an object from any given static method, with the given parameter.- Parameters:
pClass
- The class to invoke method onpMethod
- The name of the method to invokepParam
- The parameter to the method- Returns:
- The object returned by the static method. If the return type of the method is a primitive type, it is wrapped in the corresponding wrapper object (int is wrapped in an Integer). If the return type of the method is void, null is returned. If the method could not be invoked for any reason, null is returned.
- Throws:
InvocationTargetException
- if the invocation failed
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object... pParams) throws InvocationTargetException Gets an object from any given static method, with the given parameter.- Parameters:
pClass
- The class to invoke method onpMethod
- The name of the method to invokepParams
- The parameters to the method- Returns:
- The object returned by the static method. If the return type of the method is a primitive type, it is wrapped in the corresponding wrapper object (int is wrapped in an Integer). If the return type of the method is void, null is returned. If the method could not be invoked for any reason, null is returned.
- Throws:
InvocationTargetException
- if the invocation failed
-
configure
Configures the bean according to the given mapping. For eachMap.Entry
inMap.values()
, a method namedset + capitalize(entry.getKey())
is called on the bean, withentry.getValue()
as its argument.Properties that has no matching set-method in the bean, are simply discarded.
- Parameters:
pBean
- The bean to configurepMapping
- The mapping for the bean- Throws:
NullPointerException
- if any of the parameters are null.InvocationTargetException
- if an error occurs when invoking the setter-method.
-
configure
public static void configure(Object pBean, Map<String, ?> pMapping, boolean pLispToCamel) throws InvocationTargetExceptionConfigures the bean according to the given mapping. For eachMap.Entry
inMap.values()
, a method namedset + capitalize(entry.getKey())
is called on the bean, withentry.getValue()
as its argument.Optionally, lisp-style names are allowed, and automatically converted to Java-style camel-case names.
Properties that has no matching set-method in the bean, are simply discarded.
- Parameters:
pBean
- The bean to configurepMapping
- The mapping for the beanpLispToCamel
- Allow lisp-style names, and automatically convert them to Java-style camel-case.- Throws:
NullPointerException
- if any of the parameters are null.InvocationTargetException
- if an error occurs when invoking the setter-method.- See Also:
-