org.databene.commons
Class BeanUtil

java.lang.Object
  extended by org.databene.commons.BeanUtil

public final class BeanUtil
extends java.lang.Object

Bundles reflection and introspection related operations.

Created: 01.07.2006 08:44:33

Since:
0.1
Author:
Volker Bergmann

Method Summary
static
<T> T
callWithJarClassLoader(java.io.File jarFile, java.util.concurrent.Callable<T> action)
           
static void checkJavaBean(java.lang.Class<?> cls)
          Checks if a class fulfills the JavaBeans contract.
static
<T> T
clone(T object)
           
static
<T> T[]
cloneAll(T[] input)
           
static java.lang.Class<?> commonSubType(java.util.Collection<?> objects)
           
static java.lang.Class<?> commonSuperType(java.util.Collection<?> objects)
           
static java.lang.ClassLoader createJarClassLoader(java.io.File jarFile)
           
static boolean deprecated(java.lang.Class<?> type)
          Tells if a class is deprecated.
static boolean equalsIgnoreType(java.lang.Object o1, java.lang.Object o2)
          Tries to convert both arguments to the same type and then compares them
static
<BEAN,PROP_TYPE>
java.util.List<PROP_TYPE>
extractProperties(java.util.Collection<BEAN> beans, java.lang.String propertyName)
           
static
<T> java.lang.reflect.Constructor<T>
findConstructor(java.lang.Class<T> type, java.lang.Class<?>... paramTypes)
           
static java.lang.reflect.Method findMethod(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Finds a method by reflection.
static java.lang.reflect.Method[] findMethodsByAnnotation(java.lang.Class<?> owner, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
           
static java.lang.reflect.Method[] findMethodsByName(java.lang.Class<?> type, java.lang.String methodName)
           
static
<T> java.lang.Class<T>
forName(java.lang.String name)
          Instantiates the specified class.
static java.lang.Object getAttributeValue(java.lang.Object obj, java.lang.String attributeName)
          Returns an object's attribute value
static java.lang.ClassLoader getContextClassLoader()
           
static java.lang.reflect.Field getField(java.lang.Class<?> type, java.lang.String name)
          Returns a Field object that represents an attribute of a class
static java.lang.Object getFieldValue(java.lang.reflect.Field field, java.lang.Object target, boolean strict)
           
static java.lang.Object getFieldValue(java.lang.Object target, java.lang.String name, boolean strict)
           
static
<C,I> java.lang.reflect.Type[]
getGenericInterfaceParams(java.lang.Class<C> checkedClass, java.lang.Class<I> searchedInterface)
           
static java.lang.Class<?>[] getGenericTypes(java.lang.reflect.Field field)
          Returns the generic type information of an attribute.
static java.lang.reflect.Method getMethod(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Finds a method by reflection.
static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> beanClass, java.lang.String propertyName)
          Returns the bean property descriptor of an attribute
static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> type, java.lang.String propertyName, boolean required)
           
static java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class<?> type)
          Finds all property descriptors of a bean class
static java.lang.Object getPropertyValue(java.lang.Object bean, java.lang.String propertyName)
          Queries a property value on a JavaBean instance
static java.lang.Object getPropertyValue(java.lang.Object bean, java.lang.String propertyName, boolean strict)
           
static java.lang.Object getStaticAttributeValue(java.lang.Class<?> objectType, java.lang.String attributeName)
          Returns a class' static attribute value
static java.lang.Class<?> getWrapper(java.lang.String primitiveClassName)
           
static boolean hasProperty(java.lang.Class<?> beanClass, java.lang.String propertyName)
           
static java.lang.Object invoke(boolean strict, java.lang.Object target, java.lang.String methodName, java.lang.Object... args)
           
static
<T> T
invoke(java.lang.Object target, java.lang.reflect.Method method, boolean strict, java.lang.Object... args)
           
static java.lang.Object invoke(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object... args)
          Invokes a method on a bean
static java.lang.Object invoke(java.lang.Object target, java.lang.String methodName, java.lang.Object... args)
          Invokes a method on a bean.
static java.lang.Object invokeStatic(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... args)
           
static boolean isCollectionType(java.lang.Class<?> type)
          Tells if the specified class is a collection type.
static boolean isDecimalNumberType(java.lang.Class<?> type)
           
static boolean isDecimalNumberType(java.lang.String className)
           
static boolean isIntegralNumberType(java.lang.Class<?> type)
           
static boolean isIntegralNumberType(java.lang.String className)
           
static boolean isNumberType(java.lang.Class<?> type)
           
static boolean isPrimitiveNumberType(java.lang.String className)
           
static boolean isPrimitiveType(java.lang.String className)
           
static boolean isSimpleType(java.lang.String className)
          Tells if the provided class name is the name of a simple Java type
static
<T> T
newInstance(java.lang.Class<T> type, boolean strict, java.lang.Object... parameters)
           
static
<T> T
newInstance(java.lang.Class<T> type, java.lang.Object... parameters)
          Creates an object of the specified type.
static
<T> T
newInstance(java.lang.reflect.Constructor<T> constructor, boolean strict, java.lang.Object... parameters)
           
static
<T> T
newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... params)
          Creates a new instance of a Class.
static java.lang.Object newInstance(java.lang.String className)
          Instantiates a class by the default constructor.
static void printClassInfo(java.lang.Object object, java.io.PrintWriter printer)
          Prints information about a class' parents and methods to a PrintWriter
static java.lang.String readMethodName(java.lang.String propertyName, java.lang.Class<?> propertyType)
          returns the name of a property read method.
static void runWithJarClassLoader(java.io.File jarFile, java.lang.Runnable action)
           
static void setAttributeValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object value)
          Sets an attribute value of an object.
static void setPropertyValue(java.lang.Object bean, java.lang.String propertyName, java.lang.Object propertyValue)
          sets a property value on a JavaBean instance.
static void setPropertyValue(java.lang.Object bean, java.lang.String propertyName, java.lang.Object propertyValue, boolean strict)
           
static void setPropertyValue(java.lang.Object bean, java.lang.String propertyName, java.lang.Object propertyValue, boolean required, boolean autoConvert)
           
static void setStaticAttributeValue(java.lang.Class<?> objectType, java.lang.String fieldName, java.lang.Object value)
          Sets a static attribute value of a class.
static
<T> java.lang.String
simpleClassName(java.lang.Object o)
           
static java.lang.String toString(java.lang.Object bean)
           
static java.lang.String toString(java.lang.Object bean, boolean simple)
           
static boolean typesMatch(java.lang.Class<?>[] foundTypes, java.lang.Class<?>[] expectedTypes)
           
static java.lang.String writeMethodName(java.lang.String propertyName)
          returns the name of a property write method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

commonSuperType

public static java.lang.Class<?> commonSuperType(java.util.Collection<?> objects)

commonSubType

public static java.lang.Class<?> commonSubType(java.util.Collection<?> objects)

isSimpleType

public static boolean isSimpleType(java.lang.String className)
Tells if the provided class name is the name of a simple Java type

Parameters:
className - the name to check
Returns:
true if it is a simple type, else false

isPrimitiveType

public static boolean isPrimitiveType(java.lang.String className)

isPrimitiveNumberType

public static boolean isPrimitiveNumberType(java.lang.String className)

isNumberType

public static boolean isNumberType(java.lang.Class<?> type)

isIntegralNumberType

public static boolean isIntegralNumberType(java.lang.Class<?> type)

isIntegralNumberType

public static boolean isIntegralNumberType(java.lang.String className)

isDecimalNumberType

public static boolean isDecimalNumberType(java.lang.Class<?> type)

isDecimalNumberType

public static boolean isDecimalNumberType(java.lang.String className)

getWrapper

public static java.lang.Class<?> getWrapper(java.lang.String primitiveClassName)

isCollectionType

public static boolean isCollectionType(java.lang.Class<?> type)
Tells if the specified class is a collection type.

Parameters:
type - the class to check
Returns:
true if the class is a collection type, false otherwise

getAttributeValue

public static java.lang.Object getAttributeValue(java.lang.Object obj,
                                                 java.lang.String attributeName)
Returns an object's attribute value

Parameters:
obj - the object to query
attributeName - the name of the attribute
Returns:
the attribute value

setAttributeValue

public static void setAttributeValue(java.lang.Object obj,
                                     java.lang.String fieldName,
                                     java.lang.Object value)
Sets an attribute value of an object.

Parameters:
obj - the object to modify
fieldName - the name of the attribute to set
value - the value to assign to the field

getStaticAttributeValue

public static java.lang.Object getStaticAttributeValue(java.lang.Class<?> objectType,
                                                       java.lang.String attributeName)
Returns a class' static attribute value

Parameters:
objectType - the class to query
attributeName - the name of the attribute
Returns:
the attribute value

setStaticAttributeValue

public static void setStaticAttributeValue(java.lang.Class<?> objectType,
                                           java.lang.String fieldName,
                                           java.lang.Object value)
Sets a static attribute value of a class.

Parameters:
objectType - the class to modify
fieldName - the name of the attribute to set
value - the value to assign to the field

getGenericTypes

public static java.lang.Class<?>[] getGenericTypes(java.lang.reflect.Field field)
Returns the generic type information of an attribute.

Parameters:
field - the field representation of the attribute.
Returns:
an array of types that are used to parameterize the attribute.

forName

public static <T> java.lang.Class<T> forName(java.lang.String name)
Instantiates the specified class.

Parameters:
name - the name of the class to instantiate
Returns:
the Class instance

getContextClassLoader

public static java.lang.ClassLoader getContextClassLoader()

createJarClassLoader

public static java.lang.ClassLoader createJarClassLoader(java.io.File jarFile)
                                                  throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

runWithJarClassLoader

public static void runWithJarClassLoader(java.io.File jarFile,
                                         java.lang.Runnable action)
                                  throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

callWithJarClassLoader

public static <T> T callWithJarClassLoader(java.io.File jarFile,
                                           java.util.concurrent.Callable<T> action)
                                throws java.lang.Exception
Throws:
java.lang.Exception

newInstance

public static java.lang.Object newInstance(java.lang.String className)
Instantiates a class by the default constructor.

Parameters:
className - the name of the class to instantiate
Returns:
an instance of the class

newInstance

public static <T> T newInstance(java.lang.Class<T> type,
                                java.lang.Object... parameters)
Creates an object of the specified type.

Parameters:
type - the class to instantiate
parameters - the constructor parameters
Returns:
an object of the specified class

newInstance

public static <T> T newInstance(java.lang.Class<T> type,
                                boolean strict,
                                java.lang.Object... parameters)

newInstance

public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor,
                                java.lang.Object... params)
Creates a new instance of a Class.

Parameters:
constructor -
params -
Returns:
a new instance of the class

newInstance

public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor,
                                boolean strict,
                                java.lang.Object... parameters)

clone

public static <T> T clone(T object)

cloneAll

public static <T> T[] cloneAll(T[] input)

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> type,
                                                 java.lang.String methodName,
                                                 java.lang.Class<?>... paramTypes)
Finds a method by reflection. This iterates all methods of the class, comparing names and parameter types. Unlike the method Class.getMethod(String, Class ...), this method is able to match primitive and wrapper types. If no appropriate method is found, a ConfigurationError is raised.

Parameters:
type -
methodName -
paramTypes -
Returns:
a method with matching names and parameters

findConstructor

public static <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> type,
                                                                   java.lang.Class<?>... paramTypes)

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class<?> type,
                                                  java.lang.String methodName,
                                                  java.lang.Class<?>... paramTypes)
Finds a method by reflection. This iterates all methods of the class, comparing names and parameter types. Unlike the method Class.getMethod(String, Class ...), this method is able to match primitive and wrapper types. If no appropriate method is found, 'null' is returned

Parameters:
type -
methodName -
paramTypes -
Returns:
a method with matching names and parameters

findMethodsByName

public static java.lang.reflect.Method[] findMethodsByName(java.lang.Class<?> type,
                                                           java.lang.String methodName)

invoke

public static java.lang.Object invoke(java.lang.Object target,
                                      java.lang.String methodName,
                                      java.lang.Object... args)
Invokes a method on a bean.

Parameters:
target -
methodName -
args -
Returns:
the invoked method's return value.

invoke

public static java.lang.Object invoke(boolean strict,
                                      java.lang.Object target,
                                      java.lang.String methodName,
                                      java.lang.Object... args)

invokeStatic

public static java.lang.Object invokeStatic(java.lang.Class<?> targetClass,
                                            java.lang.String methodName,
                                            java.lang.Object... args)

invoke

public static java.lang.Object invoke(java.lang.Object target,
                                      java.lang.reflect.Method method,
                                      java.lang.Object... args)
Invokes a method on a bean

Parameters:
target -
method -
args -
Returns:
the invoked method's return value.

invoke

public static <T> T invoke(java.lang.Object target,
                           java.lang.reflect.Method method,
                           boolean strict,
                           java.lang.Object... args)

typesMatch

public static boolean typesMatch(java.lang.Class<?>[] foundTypes,
                                 java.lang.Class<?>[] expectedTypes)

getPropertyDescriptor

public static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> beanClass,
                                                                  java.lang.String propertyName)
Returns the bean property descriptor of an attribute

Parameters:
beanClass - the class that holds the attribute
propertyName - the name of the property
Returns:
the attribute's property descriptor

getPropertyDescriptor

public static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> type,
                                                                  java.lang.String propertyName,
                                                                  boolean required)

hasProperty

public static boolean hasProperty(java.lang.Class<?> beanClass,
                                  java.lang.String propertyName)

readMethodName

public static java.lang.String readMethodName(java.lang.String propertyName,
                                              java.lang.Class<?> propertyType)
returns the name of a property read method.

Parameters:
propertyName - the name of the property
propertyType - the type of the property
Returns:
the name of the property read method

writeMethodName

public static java.lang.String writeMethodName(java.lang.String propertyName)
returns the name of a property write method.

Parameters:
propertyName - the name of the property
Returns:
the name of the property write method

getPropertyDescriptors

public static java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.Class<?> type)
Finds all property descriptors of a bean class

Parameters:
type - the class to check
Returns:
all found property descriptors

getPropertyValue

public static java.lang.Object getPropertyValue(java.lang.Object bean,
                                                java.lang.String propertyName)
Queries a property value on a JavaBean instance

Parameters:
bean -
propertyName -
Returns:
the property value

getPropertyValue

public static java.lang.Object getPropertyValue(java.lang.Object bean,
                                                java.lang.String propertyName,
                                                boolean strict)

setPropertyValue

public static void setPropertyValue(java.lang.Object bean,
                                    java.lang.String propertyName,
                                    java.lang.Object propertyValue)
sets a property value on a JavaBean instance.

Parameters:
bean -
propertyName -
propertyValue -

setPropertyValue

public static void setPropertyValue(java.lang.Object bean,
                                    java.lang.String propertyName,
                                    java.lang.Object propertyValue,
                                    boolean strict)

setPropertyValue

public static void setPropertyValue(java.lang.Object bean,
                                    java.lang.String propertyName,
                                    java.lang.Object propertyValue,
                                    boolean required,
                                    boolean autoConvert)

extractProperties

public static <BEAN,PROP_TYPE> java.util.List<PROP_TYPE> extractProperties(java.util.Collection<BEAN> beans,
                                                                           java.lang.String propertyName)

printClassInfo

public static void printClassInfo(java.lang.Object object,
                                  java.io.PrintWriter printer)
Prints information about a class' parents and methods to a PrintWriter

Parameters:
object -
printer -

checkJavaBean

public static void checkJavaBean(java.lang.Class<?> cls)
Checks if a class fulfills the JavaBeans contract.

Parameters:
cls - the class to check

deprecated

public static boolean deprecated(java.lang.Class<?> type)
Tells if a class is deprecated.

Parameters:
type - the class to check for deprecation
Returns:
true if the class is deprecated, else false
Since:
0.2.05

getFieldValue

public static java.lang.Object getFieldValue(java.lang.Object target,
                                             java.lang.String name,
                                             boolean strict)

getFieldValue

public static java.lang.Object getFieldValue(java.lang.reflect.Field field,
                                             java.lang.Object target,
                                             boolean strict)

getField

public static java.lang.reflect.Field getField(java.lang.Class<?> type,
                                               java.lang.String name)
Returns a Field object that represents an attribute of a class

Parameters:
type - the class that holds the attribute
name - the name of the attribute
Returns:
a Field object that represents the attribute

findMethodsByAnnotation

public static java.lang.reflect.Method[] findMethodsByAnnotation(java.lang.Class<?> owner,
                                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)

getGenericInterfaceParams

public static <C,I> java.lang.reflect.Type[] getGenericInterfaceParams(java.lang.Class<C> checkedClass,
                                                                       java.lang.Class<I> searchedInterface)

toString

public static java.lang.String toString(java.lang.Object bean)

toString

public static java.lang.String toString(java.lang.Object bean,
                                        boolean simple)

simpleClassName

public static <T> java.lang.String simpleClassName(java.lang.Object o)

equalsIgnoreType

public static boolean equalsIgnoreType(java.lang.Object o1,
                                       java.lang.Object o2)
Tries to convert both arguments to the same type and then compares them



Copyright © 2010. All Rights Reserved.