Package liquibase.util
Class ObjectUtil
- java.lang.Object
-
- liquibase.util.ObjectUtil
-
public class ObjectUtil extends Object
Various methods that make it easier to read and write object properties using the propertyName, instead of having to look up the correct reader/writer methods manually first. All methods in this class are static by nature.
-
-
Constructor Summary
Constructors Constructor Description ObjectUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
convert(Object object, Class<T> targetClass)
Converts the given object to the targetClassstatic <T> T
defaultIfNull(T value, T defaultValue)
Return the defaultValue if the passed value is null.static <T,U>
TdefaultIfNull(U object, T defaultValue, Function<U,T> getter)
Return the defaultValue if the object is null.static PropertyDescriptor[]
getDescriptors(Class<?> targetClass)
static Object
getProperty(Object object, String propertyName)
For a given object, try to find the appropriate reader method and return the value, if set for the object.static Class
getPropertyType(Object object, String propertyName)
Tried to determine the appropriate reader method for a given propertyName of a given object and, if found, returns the class of its return type.static boolean
hasProperty(Object object, String propertyName)
Examines the given object's class and returns true if reader and writer methods both exist for the given property name.static boolean
hasReadProperty(Object object, String propertyName)
Examines the given object's class and returns true if a reader method exists for the given property name.static boolean
hasWriteProperty(Object object, String propertyName)
Examines the given object's class and returns true if a writer method exists for the given property name.static void
setProperty(Object object, String propertyName, Object propertyValue)
Sets the selected property of the given object to propertyValue.static void
setProperty(Object object, String propertyName, String propertyValue)
Tries to guess the "real" data type of propertyValue by the given propertyName, then sets the selected property of the given object to that value.
-
-
-
Method Detail
-
getProperty
public static Object getProperty(Object object, String propertyName) throws UnexpectedLiquibaseException
For a given object, try to find the appropriate reader method and return the value, if set for the object. If the property is currently not set for the object, anUnexpectedLiquibaseException
run-time exception occurs.- Parameters:
object
- the object to examinepropertyName
- the property name for which the value should be read- Returns:
- the stored value
- Throws:
UnexpectedLiquibaseException
-
getPropertyType
public static Class getPropertyType(Object object, String propertyName)
Tried to determine the appropriate reader method for a given propertyName of a given object and, if found, returns the class of its return type.- Parameters:
object
- the object to examinepropertyName
- the property name whose reading method should be searched- Returns:
- the class name of the return type if the reading method is found, null if it is not found.
-
hasProperty
public static boolean hasProperty(Object object, String propertyName)
Examines the given object's class and returns true if reader and writer methods both exist for the given property name.- Parameters:
object
- the object for which the class should be examinedpropertyName
- the property name to search- Returns:
- true if both reader and writer methods exist
-
hasReadProperty
public static boolean hasReadProperty(Object object, String propertyName)
Examines the given object's class and returns true if a reader method exists for the given property name.- Parameters:
object
- the object for which the class should be examinedpropertyName
- the property name to search- Returns:
- true if a reader method exists
-
hasWriteProperty
public static boolean hasWriteProperty(Object object, String propertyName)
Examines the given object's class and returns true if a writer method exists for the given property name.- Parameters:
object
- the object for which the class should be examinedpropertyName
- the property name to search- Returns:
- true if a writer method exists
-
setProperty
public static void setProperty(Object object, String propertyName, String propertyValue)
Tries to guess the "real" data type of propertyValue by the given propertyName, then sets the selected property of the given object to that value.- Parameters:
object
- the object whose property should be setpropertyName
- name of the property to setpropertyValue
- new value of the property, as String
-
setProperty
public static void setProperty(Object object, String propertyName, Object propertyValue)
Sets the selected property of the given object to propertyValue. A run-time exception will occur if the type of value is incompatible with the reader/writer method signatures of the given propertyName.- Parameters:
object
- the object whose property should be setpropertyName
- name of the property to setpropertyValue
- new value of the property
-
convert
public static <T> T convert(Object object, Class<T> targetClass) throws IllegalArgumentException
Converts the given object to the targetClass- Throws:
IllegalArgumentException
-
defaultIfNull
public static <T> T defaultIfNull(T value, T defaultValue)
Return the defaultValue if the passed value is null. Otherwise, return the original value.
-
defaultIfNull
public static <T,U> T defaultIfNull(U object, T defaultValue, Function<U,T> getter)
Return the defaultValue if the object is null. Otherwise, call the getter on the supplied object and return that value. This is essentially equivalent to the ternary operation:return object == null ? defaultValue : getter.apply(object)
- Type Parameters:
T
- the return typeU
- the type of the object upon which a null check is conducted
-
getDescriptors
public static PropertyDescriptor[] getDescriptors(Class<?> targetClass) throws IntrospectionException
- Throws:
IntrospectionException
-
-