Class LangUtils

java.lang.Object
rs.baselib.lang.LangUtils

public class LangUtils
extends java.lang.Object
Reflection and language Utils.
Author:
ralph
  • Constructor Summary

    Constructors 
    Constructor Description
    LangUtils()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.Class<?> forName​(java.lang.String className)
    Load a class by first checking the thread's class loader and then the caller's class loader.
    static java.lang.Class<?> forName​(java.lang.String className, java.lang.ClassLoader classLoader)
    Load a class by directly specifying a class loader.
    static java.math.BigDecimal getBigDecimal​(java.lang.Object o)
    Converts the object to a BigDecimal.
    static java.math.BigDecimal getBigDecimal​(java.lang.Object o, java.math.BigDecimal defaultValue)
    Converts the object to a BigDecimal.
    static boolean getBoolean​(java.lang.Object o)
    Converts the object to a boolean.
    static java.lang.Class<?> getClass​(java.lang.reflect.Type type)
    static java.util.Date getDate​(java.lang.Object o)
    Converts the object to a Date.
    static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format)
    Converts the object to a date.
    static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat[] formats)
    Returns the date using the first DateFormat that produces a result.
    static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat[] formats, java.util.Date defaultValue)
    Returns the date using the first DateFormat that produces a result.
    static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue)
    Converts the object to a date.
    static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue, boolean logError)
    Converts the object to a date.
    static double getDouble​(java.lang.Object o)
    Converts the object to a double.
    static double getDouble​(java.lang.Object o, double defaultValue)
    Converts the object to a double.
    static float getFloat​(java.lang.Object o)
    Converts the object to a float.
    static float getFloat​(java.lang.Object o, float defaultValue)
    Converts the object to a float.
    static int getInt​(java.lang.Object o)
    Converts the object to an int.
    static int getInt​(java.lang.Object o, int defaultValue)
    Converts the object to an int.
    static java.util.Locale getLocale​(java.lang.String id)
    Returns the locale with the id.
    static java.util.Locale getLocale​(java.lang.String id, java.util.Locale defaultLocale)
    Returns the locale with the id.
    static long getLong​(java.lang.Object o)
    Converts the object to a long.
    static long getLong​(java.lang.Object o, long defaultValue)
    Converts the object to a long.
    static RsDate getRsDate​(java.lang.Object o)
    Converts the object to a RsDate.
    static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format)
    Converts the object to a RsDate.
    static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat[] formats)
    Returns the date using the first DateFormat that produces a result.
    static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat[] formats, RsDate defaultValue)
    Returns the date using the first DateFormat that produces a result.
    static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue)
    Converts the object to a RsDate.
    static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue, boolean logError)
    Converts the object to a RsDate.
    static java.lang.String getString​(java.lang.Object o)
    Converts the object to a string.
    static <T> java.util.List<java.lang.Class<?>> getTypeArguments​(java.lang.Class<T> baseClass, java.lang.Class<? extends T> childClass)
    static boolean isInstanceOf​(java.lang.Class<?> inspectedClass, java.lang.String className)
    static boolean isInstanceOf​(java.lang.Object o, java.lang.String className)
    static boolean isJava6()
    Returns true when the runtime is Java 6.
    static boolean isNoCopy​(java.beans.PropertyDescriptor desc)
    Returns true when given property is not allowed to be copied with IBean.copyTo(Object).
    static boolean isNumber​(java.lang.String s)
    Returns true when the given string is a number.
    static boolean isNumber​(java.lang.String s, int radix)
    Returns true when the given string is a number with a given radix.
    static boolean isTransient​(java.beans.PropertyDescriptor desc)
    Returns true when given property is transient.
    static java.lang.Object newInstance​(java.lang.String className)
    Instantiates a class object from class name.
    static java.lang.Object newInstance​(java.lang.String className, java.lang.ClassLoader classLoader)
    Instantiates a class object from class name.
    static byte[] serialize​(java.lang.Object value)
    Serializes the given value.
    static java.lang.String serializeBase64​(java.lang.Object value)
    Serializes the given value into BASE64.
    static void sleep​(long milliseconds)
    A wrapper for Thread.sleep(long) to avoid try/catch blocks.
    static java.lang.String toString​(java.lang.String className, java.lang.Object... properties)
    Creates a unified toString() output.
    static java.lang.Object unserialize​(byte[] bytes)
    Unserializes the given value.
    static java.lang.Object unserialize​(java.lang.String value)
    Unserializes the given value.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • forName

      public static java.lang.Class<?> forName​(java.lang.String className) throws java.lang.ClassNotFoundException
      Load a class by first checking the thread's class loader and then the caller's class loader.
      Parameters:
      className - name of cass to be loaded
      Returns:
      the class found
      Throws:
      java.lang.ClassNotFoundException - when the class cannot be found
    • forName

      public static java.lang.Class<?> forName​(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException
      Load a class by directly specifying a class loader.
      Parameters:
      className - name of cass to be loaded
      classLoader - the class loader to be used - if null the thread's class loader will be used first
      Returns:
      the class found
      Throws:
      java.lang.ClassNotFoundException - when the class cannot be found
    • newInstance

      public static java.lang.Object newInstance​(java.lang.String className) throws java.lang.ReflectiveOperationException
      Instantiates a class object from class name. This is a shortcut method for forName(String).newInstance().
      Parameters:
      className - the name of class
      Returns:
      an instance of the named class
      Throws:
      java.lang.ClassNotFoundException - when class cannot be found
      java.lang.IllegalAccessException - when constructor cannot be called
      java.lang.InstantiationException - when constructor throws errors
      java.lang.ReflectiveOperationException
    • newInstance

      public static java.lang.Object newInstance​(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ReflectiveOperationException
      Instantiates a class object from class name. This is a shortcut method for forName(String,ClassLoader).newInstance().
      Parameters:
      className - the name of class
      classLoader - the class loader to be used - if null the thread's class loader will be used first
      Returns:
      an instance of the named class
      Throws:
      java.lang.ClassNotFoundException - when class cannot be found
      java.lang.IllegalAccessException - when constructor cannot be called
      java.lang.InstantiationException - when constructor throws errors
      java.lang.ReflectiveOperationException
    • getClass

      @Deprecated public static java.lang.Class<?> getClass​(java.lang.reflect.Type type)
      Get the underlying class for a type, or null if the type is a variable type.
      Parameters:
      type - the type
      Returns:
      the underlying class
    • getTypeArguments

      @Deprecated public static <T> java.util.List<java.lang.Class<?>> getTypeArguments​(java.lang.Class<T> baseClass, java.lang.Class<? extends T> childClass)
      Get the actual type arguments a child class has used to extend a generic base class.
      Type Parameters:
      T - type of base class
      Parameters:
      baseClass - the base class
      childClass - the child class
      Returns:
      a list of the raw classes for the actual type arguments.
    • isNumber

      public static boolean isNumber​(java.lang.String s)
      Returns true when the given string is a number.
      Parameters:
      s - string to be verified
      Returns:
      true when the string contains a number.
    • isNumber

      public static boolean isNumber​(java.lang.String s, int radix)
      Returns true when the given string is a number with a given radix.
      Parameters:
      s - string to be verified
      radix - the radix to check
      Returns:
      true when the string contains a number.
    • getString

      public static java.lang.String getString​(java.lang.Object o)
      Converts the object to a string.
      Parameters:
      o - object to be converted
      Returns:
      null if object is null, string value of object otherwise
    • getInt

      public static int getInt​(java.lang.Object o)
      Converts the object to an int. The method will throw an exception when the object is not null and cannot be parsed.
      Parameters:
      o - object to be converted
      Returns:
      0 if object is null, int value of object otherwise
    • getInt

      public static int getInt​(java.lang.Object o, int defaultValue)
      Converts the object to an int.
      Parameters:
      o - object to be converted
      defaultValue - the default value to return
      Returns:
      default if object is null or cannot be parsed, int value of object otherwise
    • getLong

      public static long getLong​(java.lang.Object o)
      Converts the object to a long. The method will throw an exception when the object is not null and cannot be parsed.
      Parameters:
      o - object to be converted
      Returns:
      0 if object is null, long value of object otherwise
    • getLong

      public static long getLong​(java.lang.Object o, long defaultValue)
      Converts the object to a long.
      Parameters:
      o - object to be converted
      defaultValue - the default value to return
      Returns:
      default if object is null or cannot be parsed, long value of object otherwise
    • getFloat

      public static float getFloat​(java.lang.Object o)
      Converts the object to a float. The method will throw an exception when the object is not null and cannot be parsed.
      Parameters:
      o - object to be converted
      Returns:
      0 if object is null, float value of object otherwise
    • getFloat

      public static float getFloat​(java.lang.Object o, float defaultValue)
      Converts the object to a float.
      Parameters:
      o - object to be converted
      defaultValue - the default value to return
      Returns:
      default if object is null or cannot be parsed, float value of object otherwise
    • getBigDecimal

      public static java.math.BigDecimal getBigDecimal​(java.lang.Object o)
      Converts the object to a BigDecimal.
      Parameters:
      o - object to be converted
      Returns:
      BigDecimal.ZERO if o is null or cannot be parsed, BigDecimal value of object otherwise
      Since:
      1.3.2
    • getBigDecimal

      public static java.math.BigDecimal getBigDecimal​(java.lang.Object o, java.math.BigDecimal defaultValue)
      Converts the object to a BigDecimal.
      Parameters:
      o - object to be converted
      defaultValue - the default value to return
      Returns:
      default if object is null or cannot be parsed, BigDecimal value of object otherwise
      Since:
      1.3.2
    • getDouble

      public static double getDouble​(java.lang.Object o)
      Converts the object to a double. The method will throw an exception when the object is not null and cannot be parsed.
      Parameters:
      o - object to be converted
      Returns:
      0 if object is null, double value of object otherwise
    • getDouble

      public static double getDouble​(java.lang.Object o, double defaultValue)
      Converts the object to a double.
      Parameters:
      o - object to be converted
      defaultValue - the default value to return
      Returns:
      default if object is null or cannot be parsed, double value of object otherwise
    • getBoolean

      public static boolean getBoolean​(java.lang.Object o)
      Converts the object to a boolean. Values recognized a strue are 1, true, on, yes, y (case-insensitive).
      Parameters:
      o - object to be converted
      Returns:
      false if object is null, boolean value of object otherwise
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat[] formats)
      Returns the date using the first DateFormat that produces a result.
      Parameters:
      o - object to be transformed
      formats - formats to be used
      Returns:
      the date if it could be parsed or null
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat[] formats, RsDate defaultValue)
      Returns the date using the first DateFormat that produces a result.
      Parameters:
      o - object to be transformed
      formats - formats to be used
      defaultValue - value to be returned when object cannot be transformed.
      Returns:
      the date if it could be parsed, the default value otherwise
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o)
      Converts the object to a RsDate.
      Parameters:
      o - object to be converted
      Returns:
      RsDate value of object, null otherwise
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format)
      Converts the object to a RsDate.
      Parameters:
      o - object to be converted
      format - format to be applied
      Returns:
      RsDate value of object, null otherwise
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue)
      Converts the object to a RsDate.
      Parameters:
      o - object to be converted
      format - format to be applied
      defaultValue - value to be returned when object cannot be transformed.
      Returns:
      RsDate value of object, the default value otherwise
    • getRsDate

      public static RsDate getRsDate​(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue, boolean logError)
      Converts the object to a RsDate.
      Parameters:
      o - object to be converted
      format - format to be applied
      defaultValue - value to be returned when object cannot be transformed.
      logError - whether parsing error shall be logged
      Returns:
      RsDate value of object, the default value otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat[] formats)
      Returns the date using the first DateFormat that produces a result.
      Parameters:
      o - object to be transformed
      formats - formats to be used
      Returns:
      the date if it could be parsed, null otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat[] formats, java.util.Date defaultValue)
      Returns the date using the first DateFormat that produces a result.
      Parameters:
      o - object to be transformed
      formats - formats to be used
      defaultValue - value to be returned when object cannot be transformed.
      Returns:
      the date if it could be parsed, the default value otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o)
      Converts the object to a Date.
      Parameters:
      o - object to be converted
      Returns:
      Date value of object, null otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format)
      Converts the object to a date.
      Parameters:
      o - object to be converted
      format - format to be applied
      Returns:
      Date value of object, null otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue)
      Converts the object to a date.
      Parameters:
      o - object to be converted
      format - format to be applied
      defaultValue - value to be returned when object cannot be transformed.
      Returns:
      Date value of object, the default value otherwise
    • getDate

      public static java.util.Date getDate​(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue, boolean logError)
      Converts the object to a date.
      Parameters:
      o - object to be converted
      format - format to be applied
      defaultValue - value to be returned when object cannot be transformed.
      logError - whether parsing error shall be logged
      Returns:
      Date value of object, the default value otherwise
    • isJava6

      public static boolean isJava6()
      Returns true when the runtime is Java 6.
      Returns:
      true if Java 6, false otherwise.
    • getLocale

      public static java.util.Locale getLocale​(java.lang.String id)
      Returns the locale with the id.
      Parameters:
      id - id of locale as returned by toString()
      Returns:
      the locale or the default Locale if not found.
    • getLocale

      public static java.util.Locale getLocale​(java.lang.String id, java.util.Locale defaultLocale)
      Returns the locale with the id.
      Parameters:
      id - id of locale as returned by toString()
      defaultLocale - default to return when locale cannot be found
      Returns:
      the locale or the default if not found.
    • serializeBase64

      public static java.lang.String serializeBase64​(java.lang.Object value) throws java.io.IOException
      Serializes the given value into BASE64.
      Parameters:
      value - value to be serialized
      Returns:
      the serialized string
      Throws:
      java.io.IOException - when serialization fails
    • serialize

      public static byte[] serialize​(java.lang.Object value) throws java.io.IOException
      Serializes the given value.
      Parameters:
      value - value to be serialized
      Returns:
      the serialized string
      Throws:
      java.io.IOException - when serialization fails
    • unserialize

      public static java.lang.Object unserialize​(java.lang.String value) throws java.lang.ClassNotFoundException, java.io.IOException
      Unserializes the given value.
      Parameters:
      value - the serialized string (BASE64 encoded)
      Returns:
      the object (or null)
      Throws:
      java.lang.ClassNotFoundException - when class to be unserialized cannot be found
      java.io.IOException - when unserialization fails
    • unserialize

      public static java.lang.Object unserialize​(byte[] bytes) throws java.lang.ClassNotFoundException, java.io.IOException
      Unserializes the given value.
      Parameters:
      bytes - the bytes of serialized object
      Returns:
      the object (or null)
      Throws:
      java.lang.ClassNotFoundException - when class to be unserialized cannot be found
      java.io.IOException - when unserialization fails
    • toString

      public static java.lang.String toString​(java.lang.String className, java.lang.Object... properties)
      Creates a unified toString() output.

      Properties must be represented with two values: name of property and its value, e.g. the call

          toString("ClassName", "property1", "value1", "property2", "value2")
       

      will produce ClassName[property1=value1;property2=value2].

      Parameters:
      className - the class name to be used
      properties - the properties where each property is represented with two values: name of property and its value.
      Returns:
      a unified toString() output, .
    • isTransient

      public static boolean isTransient​(java.beans.PropertyDescriptor desc)
      Returns true when given property is transient. A transient property has either a missing getter or setter or its getter is marked with Transient annotation.
      Parameters:
      desc - property descriptor
      Returns:
      true when property is transient and must not be persisted
    • isNoCopy

      public static boolean isNoCopy​(java.beans.PropertyDescriptor desc)
      Returns true when given property is not allowed to be copied with IBean.copyTo(Object). A NoCopy property has either a missing getter or setter or its getter is marked with NoCopy annotation.
      Parameters:
      desc - property descriptor
      Returns:
      true when property is transient and must not be persisted
    • sleep

      public static void sleep​(long milliseconds)
      A wrapper for Thread.sleep(long) to avoid try/catch blocks. The method throws a RuntimeException in case of exceptions.
      Parameters:
      milliseconds - milliseconds to sleep
    • isInstanceOf

      @Deprecated public static boolean isInstanceOf​(java.lang.Object o, java.lang.String className)
      Replacement for "instanceof" operator when it cannot be guaranteed that the class is available in classpath at runtime.
      Parameters:
      o - the object to be checked
      className - the complete class name
      Returns:
      when the object is of that class
    • isInstanceOf

      @Deprecated public static boolean isInstanceOf​(java.lang.Class<?> inspectedClass, java.lang.String className)
      Replacement for Class.isAssignableFrom(Class) when it cannot be guaranteed that the class is available in classpath at runtime.
      Parameters:
      inspectedClass - the class to be checked
      className - the complete class name that should be implemented or a superclass of the inspected class
      Returns:
      when the inspected class implements or derived from the class with given name