com.ibatis.common.beans
Class StaticBeanProbe

java.lang.Object
  extended bycom.ibatis.common.beans.StaticBeanProbe

public class StaticBeanProbe
extends java.lang.Object

StaticBeanProbe provides methods that allow simple, reflective access to JavaBeans style properties. Methods are provided for all simple types as well as object types.

Examples:

StaticBeanProbe.setObject(object, propertyName, value);

Object value = StaticBeanProbe.getObject(object, propertyName);

Author:
clinton_begin

Field Summary
private static java.lang.Object[] NO_ARGUMENTS
           
private static boolean useMetaClasses
           
 
Constructor Summary
StaticBeanProbe()
           
 
Method Summary
static boolean beanEquals(java.lang.Object object1, java.lang.Object object2)
          Compares two beans to see if all properties/values are the same
static boolean beanEquals(java.lang.Object object1, java.lang.Object object2, java.lang.String[] properties)
          Compares two beans to see if a subset of their properties/values are the same
private static boolean beanEquals(java.lang.Object object1, java.lang.Object object2, java.lang.String[] properties, java.util.Set alreadyChecked)
           
static int beanHashCode(java.lang.Object object)
          Calculates a hash code for all readable properties of a bean
static int beanHashCode(java.lang.Object object, java.lang.String[] properties)
          Calculates a hash code for a subset of the readable properties of a bean
private static int beanHashCode(java.lang.Object object, java.lang.String[] properties, java.util.Set alreadyDigested)
           
private static java.lang.Object getArrayProperty(java.lang.Object object, java.lang.String indexedName)
           
static boolean getBoolean(java.lang.Object object, java.lang.String name)
          Gets a boolean value from a bean
static byte getByte(java.lang.Object object, java.lang.String name)
          Gets a byte value from a bean
 char getCharacter(java.lang.Object object, java.lang.String name)
          Gets a char value from a bean
static java.util.Date getDate(java.lang.Object object, java.lang.String name)
          Gets a Date value from a bean
static double getDouble(java.lang.Object object, java.lang.String name)
          Gets a double value from a bean
static java.lang.Object getField(java.lang.Object object, java.lang.String name)
          Provides direct access (get value) to class fields through reflection.
static float getFloat(java.lang.Object object, java.lang.String name)
          Gets a float value from a bean
static int getInteger(java.lang.Object object, java.lang.String name)
          Gets a Integer value from a bean
static long getLong(java.lang.Object object, java.lang.String name)
          Gets a long value from a bean
static java.lang.Object getObject(java.lang.Object object, java.lang.String name)
          Gets an Object property from a bean
private static java.lang.Object getProperty(java.lang.Object object, java.lang.String name)
           
static java.lang.Class getPropertyTypeForGetter(java.lang.Object object, java.lang.String name)
          Returns the class that the getter will return when reading a property value.
static java.lang.Class getPropertyTypeForSetter(java.lang.Object object, java.lang.String name)
          Returns the class that the setter expects to receive as a parameter when setting a property value.
static java.lang.String[] getReadablePropertyNames(java.lang.Object object)
          Returns an array of the readable properties exposed by a bean
static short getShort(java.lang.Object object, java.lang.String name)
          Gets a short value from a bean
static java.lang.String getString(java.lang.Object object, java.lang.String name)
          Gets a String value from a bean
static java.lang.String[] getWriteablePropertyNames(java.lang.Object object)
          Returns an array of the writeable properties exposed by a bean
static boolean hasReadableProperty(java.lang.Object object, java.lang.String propertyName)
          Checks to see if a bean has a readable property be a given name
static boolean hasWritableProperty(java.lang.Object object, java.lang.String propertyName)
          Checks to see if a bean has a writable property be a given name
private static boolean isSimpleType(java.lang.Object value)
           
static boolean isUseMetaClasses()
           
private static void setArrayProperty(java.lang.Object object, java.lang.String indexedName, java.lang.Object value)
           
static void setBoolean(java.lang.Object object, java.lang.String name, boolean value)
          Sets the value of a bean property to a boolean
static void setByte(java.lang.Object object, java.lang.String name, byte value)
          Sets the value of a bean property to a byte
static void setCharacter(java.lang.Object object, java.lang.String name, char value)
          Sets the value of a bean property to a char
static void setDate(java.lang.Object object, java.lang.String name, java.util.Date value)
          Sets the value of a bean property to a Date
static void setDouble(java.lang.Object object, java.lang.String name, double value)
          Sets the value of a bean property to a double
static void setField(java.lang.Object object, java.lang.String name, java.lang.Object value)
          Provides direct access (set value) to class fields through reflection.
static void setFloat(java.lang.Object object, java.lang.String name, float value)
          Sets the value of a bean property to a float
static void setInteger(java.lang.Object object, java.lang.String name, int value)
          Sets the value of a bean property to a int
static void setLong(java.lang.Object object, java.lang.String name, long value)
          Sets the value of a bean property to a long
static void setObject(java.lang.Object object, java.lang.String name, java.lang.Object value)
          Sets the value of a bean property to an Object
private static void setProperty(java.lang.Object object, java.lang.String name, java.lang.Object value)
           
static void setShort(java.lang.Object object, java.lang.String name, short value)
          Sets the value of a bean property to a short
static void setString(java.lang.Object object, java.lang.String name, java.lang.String value)
          Sets the value of a bean property to a String
static void setUseMetaClasses(boolean useMetaClasses)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ARGUMENTS

private static final java.lang.Object[] NO_ARGUMENTS

useMetaClasses

private static boolean useMetaClasses
Constructor Detail

StaticBeanProbe

public StaticBeanProbe()
Method Detail

isUseMetaClasses

public static boolean isUseMetaClasses()

setUseMetaClasses

public static void setUseMetaClasses(boolean useMetaClasses)

setField

public static void setField(java.lang.Object object,
                            java.lang.String name,
                            java.lang.Object value)
Provides direct access (set value) to class fields through reflection.

Warning: This is a dangerous method that should never be used in production. However, it may be quite valueable for testing purposes.

Parameters:
object - The object to modify
name - The name of the property to modify
value - The new value to set

getField

public static java.lang.Object getField(java.lang.Object object,
                                        java.lang.String name)
Provides direct access (get value) to class fields through reflection.

Warning: This is a dangerous method that should never be used in production. However, it may be quite valueable for testing purposes.

Parameters:
object - The bean to get the value from
name - The name of the property to read
Returns:
The value in the field

getReadablePropertyNames

public static java.lang.String[] getReadablePropertyNames(java.lang.Object object)
Returns an array of the readable properties exposed by a bean

Parameters:
object - The bean
Returns:
The properties

getWriteablePropertyNames

public static java.lang.String[] getWriteablePropertyNames(java.lang.Object object)
Returns an array of the writeable properties exposed by a bean

Parameters:
object - The bean
Returns:
The properties

beanEquals

public static boolean beanEquals(java.lang.Object object1,
                                 java.lang.Object object2)
Compares two beans to see if all properties/values are the same

Parameters:
object1 - Bean one
object2 - Bean two
Returns:
True if the two beans are equal

beanEquals

public static boolean beanEquals(java.lang.Object object1,
                                 java.lang.Object object2,
                                 java.lang.String[] properties)
Compares two beans to see if a subset of their properties/values are the same

Parameters:
object1 - Bean one
object2 - Bean two
properties - The subset of properties to check
Returns:
True if the two beans are equal

beanEquals

private static boolean beanEquals(java.lang.Object object1,
                                  java.lang.Object object2,
                                  java.lang.String[] properties,
                                  java.util.Set alreadyChecked)

beanHashCode

public static int beanHashCode(java.lang.Object object)
Calculates a hash code for all readable properties of a bean

Parameters:
object - The bean to calculate the hash code for
Returns:
The hash code

beanHashCode

public static int beanHashCode(java.lang.Object object,
                               java.lang.String[] properties)
Calculates a hash code for a subset of the readable properties of a bean

Parameters:
object - The bean to calculate the hash code for
properties - A list of the properties to hash
Returns:
The hash code

beanHashCode

private static int beanHashCode(java.lang.Object object,
                                java.lang.String[] properties,
                                java.util.Set alreadyDigested)

isSimpleType

private static boolean isSimpleType(java.lang.Object value)

getPropertyTypeForSetter

public static java.lang.Class getPropertyTypeForSetter(java.lang.Object object,
                                                       java.lang.String name)
Returns the class that the setter expects to receive as a parameter when setting a property value.

Parameters:
object - The bean to check
name - The name of the property
Returns:
The type of the property

getPropertyTypeForGetter

public static java.lang.Class getPropertyTypeForGetter(java.lang.Object object,
                                                       java.lang.String name)
Returns the class that the getter will return when reading a property value.

Parameters:
object - The bean to check
name - The name of the property
Returns:
The type of the property

getArrayProperty

private static java.lang.Object getArrayProperty(java.lang.Object object,
                                                 java.lang.String indexedName)

getProperty

private static java.lang.Object getProperty(java.lang.Object object,
                                            java.lang.String name)

setArrayProperty

private static void setArrayProperty(java.lang.Object object,
                                     java.lang.String indexedName,
                                     java.lang.Object value)

setProperty

private static void setProperty(java.lang.Object object,
                                java.lang.String name,
                                java.lang.Object value)

getObject

public static java.lang.Object getObject(java.lang.Object object,
                                         java.lang.String name)
Gets an Object property from a bean

Parameters:
object - The bean
name - The property name
Returns:
The property value (as an Object)

setObject

public static void setObject(java.lang.Object object,
                             java.lang.String name,
                             java.lang.Object value)
Sets the value of a bean property to an Object

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getString

public static java.lang.String getString(java.lang.Object object,
                                         java.lang.String name)
Gets a String value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setString

public static void setString(java.lang.Object object,
                             java.lang.String name,
                             java.lang.String value)
Sets the value of a bean property to a String

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getInteger

public static int getInteger(java.lang.Object object,
                             java.lang.String name)
Gets a Integer value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setInteger

public static void setInteger(java.lang.Object object,
                              java.lang.String name,
                              int value)
Sets the value of a bean property to a int

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getLong

public static long getLong(java.lang.Object object,
                           java.lang.String name)
Gets a long value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setLong

public static void setLong(java.lang.Object object,
                           java.lang.String name,
                           long value)
Sets the value of a bean property to a long

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getShort

public static short getShort(java.lang.Object object,
                             java.lang.String name)
Gets a short value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setShort

public static void setShort(java.lang.Object object,
                            java.lang.String name,
                            short value)
Sets the value of a bean property to a short

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getByte

public static byte getByte(java.lang.Object object,
                           java.lang.String name)
Gets a byte value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setByte

public static void setByte(java.lang.Object object,
                           java.lang.String name,
                           byte value)
Sets the value of a bean property to a byte

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getCharacter

public char getCharacter(java.lang.Object object,
                         java.lang.String name)
Gets a char value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setCharacter

public static void setCharacter(java.lang.Object object,
                                java.lang.String name,
                                char value)
Sets the value of a bean property to a char

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getDouble

public static double getDouble(java.lang.Object object,
                               java.lang.String name)
Gets a double value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setDouble

public static void setDouble(java.lang.Object object,
                             java.lang.String name,
                             double value)
Sets the value of a bean property to a double

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getFloat

public static float getFloat(java.lang.Object object,
                             java.lang.String name)
Gets a float value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setFloat

public static void setFloat(java.lang.Object object,
                            java.lang.String name,
                            float value)
Sets the value of a bean property to a float

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getBoolean

public static boolean getBoolean(java.lang.Object object,
                                 java.lang.String name)
Gets a boolean value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setBoolean

public static void setBoolean(java.lang.Object object,
                              java.lang.String name,
                              boolean value)
Sets the value of a bean property to a boolean

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

getDate

public static java.util.Date getDate(java.lang.Object object,
                                     java.lang.String name)
Gets a Date value from a bean

Parameters:
object - The bean to get the value from
name - The name of the property to get
Returns:
The value of the property cast to a String

setDate

public static void setDate(java.lang.Object object,
                           java.lang.String name,
                           java.util.Date value)
Sets the value of a bean property to a Date

Parameters:
object - The bean to change
name - The name of the property to set
value - The new value to set

hasWritableProperty

public static boolean hasWritableProperty(java.lang.Object object,
                                          java.lang.String propertyName)
Checks to see if a bean has a writable property be a given name

Parameters:
object - The bean to check
propertyName - The property to check for
Returns:
True if the property exists and is writable

hasReadableProperty

public static boolean hasReadableProperty(java.lang.Object object,
                                          java.lang.String propertyName)
Checks to see if a bean has a readable property be a given name

Parameters:
object - The bean to check
propertyName - The property to check for
Returns:
True if the property exists and is readable