Class BeanUtil


  • public final class BeanUtil
    extends java.lang.Object
    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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void configure​(java.lang.Object pBean, java.util.Map<java.lang.String,​?> pMapping)
      Configures the bean according to the given mapping.
      static void configure​(java.lang.Object pBean, java.util.Map<java.lang.String,​?> pMapping, boolean pLispToCamel)
      Configures the bean according to the given mapping.
      static <T> T createInstance​(java.lang.Class<T> pClass, java.lang.Object pParam)
      Creates an object from the given class' single argument constructor.
      static <T> T createInstance​(java.lang.Class<T> pClass, java.lang.Object... pParams)
      Creates an object from the given class' constructor that matches the given paramaters.
      static java.lang.Object getPropertyValue​(java.lang.Object pObject, java.lang.String pProperty)
      Gets a property value from the given object, using reflection.
      static java.lang.Object invokeStaticMethod​(java.lang.Class<?> pClass, java.lang.String pMethod, java.lang.Object pParam)
      Gets an object from any given static method, with the given parameter.
      static java.lang.Object invokeStaticMethod​(java.lang.Class<?> pClass, java.lang.String pMethod, java.lang.Object... pParams)
      Gets an object from any given static method, with the given parameter.
      static void setPropertyValue​(java.lang.Object pObject, java.lang.String pProperty, java.lang.Object pValue)
      Sets the property value to an object using reflection.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getPropertyValue

        public static java.lang.Object getPropertyValue​(java.lang.Object pObject,
                                                        java.lang.String pProperty)
        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 from
        pProperty - 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​(java.lang.Object pObject,
                                            java.lang.String pProperty,
                                            java.lang.Object pValue)
                                     throws java.lang.NoSuchMethodException,
                                            java.lang.reflect.InvocationTargetException,
                                            java.lang.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 from
        pProperty - The name of the property
        pValue - The property value
        Throws:
        java.lang.NoSuchMethodException - if there's no write method for the given property
        java.lang.reflect.InvocationTargetException - if invoking the write method failed
        java.lang.IllegalAccessException - if the caller class has no access to the write method
      • createInstance

        public static <T> T createInstance​(java.lang.Class<T> pClass,
                                           java.lang.Object pParam)
                                    throws java.lang.reflect.InvocationTargetException
        Creates an object from the given class' single argument constructor.
        Parameters:
        pClass - The class to create instance from
        pParam - 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:
        java.lang.reflect.InvocationTargetException - if the constructor failed
      • createInstance

        public static <T> T createInstance​(java.lang.Class<T> pClass,
                                           java.lang.Object... pParams)
                                    throws java.lang.reflect.InvocationTargetException
        Creates an object from the given class' constructor that matches the given paramaters.
        Parameters:
        pClass - The class to create instance from
        pParams - 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:
        java.lang.reflect.InvocationTargetException - if the constructor failed
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.Class<?> pClass,
                                                          java.lang.String pMethod,
                                                          java.lang.Object pParam)
                                                   throws java.lang.reflect.InvocationTargetException
        Gets an object from any given static method, with the given parameter.
        Parameters:
        pClass - The class to invoke method on
        pMethod - The name of the method to invoke
        pParam - 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:
        java.lang.reflect.InvocationTargetException - if the invocation failed
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.Class<?> pClass,
                                                          java.lang.String pMethod,
                                                          java.lang.Object... pParams)
                                                   throws java.lang.reflect.InvocationTargetException
        Gets an object from any given static method, with the given parameter.
        Parameters:
        pClass - The class to invoke method on
        pMethod - The name of the method to invoke
        pParams - 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:
        java.lang.reflect.InvocationTargetException - if the invocation failed
      • configure

        public static void configure​(java.lang.Object pBean,
                                     java.util.Map<java.lang.String,​?> pMapping)
                              throws java.lang.reflect.InvocationTargetException
        Configures the bean according to the given mapping. For each Map.Entry in Map.values(), a method named set + capitalize(entry.getKey()) is called on the bean, with entry.getValue() as its argument.

        Properties that has no matching set-method in the bean, are simply discarded.

        Parameters:
        pBean - The bean to configure
        pMapping - The mapping for the bean
        Throws:
        java.lang.NullPointerException - if any of the parameters are null.
        java.lang.reflect.InvocationTargetException - if an error occurs when invoking the setter-method.
      • configure

        public static void configure​(java.lang.Object pBean,
                                     java.util.Map<java.lang.String,​?> pMapping,
                                     boolean pLispToCamel)
                              throws java.lang.reflect.InvocationTargetException
        Configures the bean according to the given mapping. For each Map.Entry in Map.values(), a method named set + capitalize(entry.getKey()) is called on the bean, with entry.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 configure
        pMapping - The mapping for the bean
        pLispToCamel - Allow lisp-style names, and automatically convert them to Java-style camel-case.
        Throws:
        java.lang.NullPointerException - if any of the parameters are null.
        java.lang.reflect.InvocationTargetException - if an error occurs when invoking the setter-method.
        See Also:
        StringUtil.lispToCamel(String)