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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    convert(Object object, Class<T> targetClass)
    Converts the given object to the targetClass
    static <T> T
    convert(Object object, Class<T> targetClass, String name)
    Converts the given object to the targetClass
    static <T> T
    defaultIfNull(T value, T defaultValue)
    Deprecated.
    use ObjectUtils.defaultIfNull(Object, Object) instead
    static <T, U> T
    defaultIfNull(U object, T defaultValue, Function<U,T> getter)
    Return the defaultValue if the object is null.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ARGUMENT_KEY

      public static String 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, an UnexpectedLiquibaseException run-time exception occurs.
      Parameters:
      object - the object to examine
      propertyName - 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 examine
      propertyName - 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 examined
      propertyName - 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 examined
      propertyName - 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 examined
      propertyName - 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 set
      propertyName - name of the property to set
      propertyValue - 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 set
      propertyName - name of the property to set
      propertyValue - 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
    • 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 public static <T> T defaultIfNull(T value, T defaultValue)
      Deprecated.
      use ObjectUtils.defaultIfNull(Object, Object) instead
      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 type
      U - the type of the object upon which a null check is conducted
    • getDescriptors

      public static PropertyDescriptor[] getDescriptors(Class<?> targetClass) throws IntrospectionException
      Throws:
      IntrospectionException