Class LangUtils

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

public class LangUtils extends Object
Reflection and language Utils.
Author:
ralph
  • Constructor Details

    • LangUtils

      public LangUtils()
  • Method Details

    • forName

      public static Class<?> forName(String className) throws 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:
      ClassNotFoundException - when the class cannot be found
    • forName

      public static Class<?> forName(String className, ClassLoader classLoader) throws 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:
      ClassNotFoundException - when the class cannot be found
    • newInstance

      public static Object newInstance(String className) throws 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:
      ClassNotFoundException - when class cannot be found
      IllegalAccessException - when constructor cannot be called
      InstantiationException - when constructor throws errors
      ReflectiveOperationException
    • newInstance

      public static Object newInstance(String className, ClassLoader classLoader) throws 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:
      ClassNotFoundException - when class cannot be found
      IllegalAccessException - when constructor cannot be called
      InstantiationException - when constructor throws errors
      ReflectiveOperationException
    • getClass

      @Deprecated public static Class<?> getClass(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> List<Class<?>> getTypeArguments(Class<T> baseClass, 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(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(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 String getString(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(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(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(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(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(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(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 BigDecimal getBigDecimal(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 BigDecimal getBigDecimal(Object o, 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(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(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(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
    • getDate

      public static Date getDate(Object o, 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 Date getDate(Object o, DateFormat[] formats, 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 Date getDate(Object o)
      Converts the object to a Date.
      Parameters:
      o - object to be converted
      Returns:
      Date value of object, null otherwise
    • getDate

      public static Date getDate(Object o, 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 Date getDate(Object o, DateFormat format, 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 Date getDate(Object o, DateFormat format, 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
    • getLocale

      public static Locale getLocale(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 Locale getLocale(String id, 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 String serializeBase64(Object value) throws IOException
      Serializes the given value into BASE64.
      Parameters:
      value - value to be serialized
      Returns:
      the serialized string
      Throws:
      IOException - when serialization fails
    • serialize

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

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

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

      public static String toString(String className, 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(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
    • 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(Object o, 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(Class<?> inspectedClass, 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