Package liquibase.util
Class ObjectUtil
java.lang.Object
liquibase.util.ObjectUtil
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.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Converts the given object to the targetClassstatic <T> T
Converts the given object to the targetClassstatic <T> T
defaultIfNull
(T value, T defaultValue) Deprecated.static <T,
U> T defaultIfNull
(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.
-
Field Details
-
ARGUMENT_KEY
-
-
Constructor Details
-
ObjectUtil
public ObjectUtil()
-
-
Method Details
-
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
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
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
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
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
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
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
Converts the given object to the targetClass- Throws:
IllegalArgumentException
-
convert
public static <T> T convert(Object object, Class<T> targetClass, String name) throws IllegalArgumentException Converts the given object to the targetClass- Parameters:
name
- The name of the argument being converted, which can be used in error messages for more descriptiveness. If null, the name will not be used in any error messages.- Throws:
IllegalArgumentException
-
defaultIfNull
Deprecated.useObjectUtils.defaultIfNull(Object, Object)
insteadReturn the defaultValue if the passed value is null. Otherwise, return the original value. -
defaultIfNull
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
-
ObjectUtils.defaultIfNull(Object, Object)
instead