Package rs.baselib.lang
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.BigDecimalgetBigDecimal(java.lang.Object o)Converts the object to a BigDecimal.static java.math.BigDecimalgetBigDecimal(java.lang.Object o, java.math.BigDecimal defaultValue)Converts the object to a BigDecimal.static booleangetBoolean(java.lang.Object o)Converts the object to a boolean.static java.lang.Class<?>getClass(java.lang.reflect.Type type)Deprecated.static java.util.DategetDate(java.lang.Object o)Converts the object to aDate.static java.util.DategetDate(java.lang.Object o, java.text.DateFormat format)Converts the object to a date.static java.util.DategetDate(java.lang.Object o, java.text.DateFormat[] formats)Returns the date using the firstDateFormatthat produces a result.static java.util.DategetDate(java.lang.Object o, java.text.DateFormat[] formats, java.util.Date defaultValue)Returns the date using the firstDateFormatthat produces a result.static java.util.DategetDate(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue)Converts the object to a date.static java.util.DategetDate(java.lang.Object o, java.text.DateFormat format, java.util.Date defaultValue, boolean logError)Converts the object to a date.static doublegetDouble(java.lang.Object o)Converts the object to a double.static doublegetDouble(java.lang.Object o, double defaultValue)Converts the object to a double.static floatgetFloat(java.lang.Object o)Converts the object to a float.static floatgetFloat(java.lang.Object o, float defaultValue)Converts the object to a float.static intgetInt(java.lang.Object o)Converts the object to an int.static intgetInt(java.lang.Object o, int defaultValue)Converts the object to an int.static java.util.LocalegetLocale(java.lang.String id)Returns the locale with the id.static java.util.LocalegetLocale(java.lang.String id, java.util.Locale defaultLocale)Returns the locale with the id.static longgetLong(java.lang.Object o)Converts the object to a long.static longgetLong(java.lang.Object o, long defaultValue)Converts the object to a long.static RsDategetRsDate(java.lang.Object o)Converts the object to aRsDate.static RsDategetRsDate(java.lang.Object o, java.text.DateFormat format)Converts the object to aRsDate.static RsDategetRsDate(java.lang.Object o, java.text.DateFormat[] formats)Returns the date using the firstDateFormatthat produces a result.static RsDategetRsDate(java.lang.Object o, java.text.DateFormat[] formats, RsDate defaultValue)Returns the date using the firstDateFormatthat produces a result.static RsDategetRsDate(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue)Converts the object to aRsDate.static RsDategetRsDate(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue, boolean logError)Converts the object to aRsDate.static java.lang.StringgetString(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)Deprecated.static booleanisInstanceOf(java.lang.Class<?> inspectedClass, java.lang.String className)Deprecated.static booleanisInstanceOf(java.lang.Object o, java.lang.String className)Deprecated.static booleanisJava6()Returns true when the runtime is Java 6.static booleanisNoCopy(java.beans.PropertyDescriptor desc)Returns true when given property is not allowed to be copied withIBean.copyTo(Object).static booleanisNumber(java.lang.String s)Returns true when the given string is a number.static booleanisNumber(java.lang.String s, int radix)Returns true when the given string is a number with a given radix.static booleanisTransient(java.beans.PropertyDescriptor desc)Returns true when given property is transient.static java.lang.ObjectnewInstance(java.lang.String className)Instantiates a class object from class name.static java.lang.ObjectnewInstance(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.StringserializeBase64(java.lang.Object value)Serializes the given value into BASE64.static voidsleep(long milliseconds)A wrapper forThread.sleep(long)to avoid try/catch blocks.static java.lang.StringtoString(java.lang.String className, java.lang.Object... properties)Creates a unifiedtoString()output.static java.lang.Objectunserialize(byte[] bytes)Unserializes the given value.static java.lang.Objectunserialize(java.lang.String value)Unserializes the given value.
-
Constructor Details
-
LangUtils
public LangUtils()
-
-
Method Details
-
forName
public static java.lang.Class<?> forName(java.lang.String className) throws java.lang.ClassNotFoundExceptionLoad 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.ClassNotFoundExceptionLoad a class by directly specifying a class loader.- Parameters:
className- name of cass to be loadedclassLoader- 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.ReflectiveOperationExceptionInstantiates 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 foundjava.lang.IllegalAccessException- when constructor cannot be calledjava.lang.InstantiationException- when constructor throws errorsjava.lang.ReflectiveOperationException
-
newInstance
public static java.lang.Object newInstance(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ReflectiveOperationExceptionInstantiates a class object from class name. This is a shortcut method for.forName(String,ClassLoader).newInstance()- Parameters:
className- the name of classclassLoader- 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 foundjava.lang.IllegalAccessException- when constructor cannot be calledjava.lang.InstantiationException- when constructor throws errorsjava.lang.ReflectiveOperationException
-
getClass
@Deprecated public static java.lang.Class<?> getClass(java.lang.reflect.Type type)Deprecated.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)Deprecated.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 classchildClass- 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:
truewhen 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 verifiedradix- the radix to check- Returns:
truewhen 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 converteddefaultValue- 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 converteddefaultValue- 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 converteddefaultValue- 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.ZEROif 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 converteddefaultValue- 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 converteddefaultValue- 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 strueare 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
Returns the date using the firstDateFormatthat produces a result.- Parameters:
o- object to be transformedformats- 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 firstDateFormatthat produces a result.- Parameters:
o- object to be transformedformats- formats to be useddefaultValue- value to be returned when object cannot be transformed.- Returns:
- the date if it could be parsed, the default value otherwise
-
getRsDate
Converts the object to aRsDate.- Parameters:
o- object to be converted- Returns:
RsDatevalue of object,nullotherwise
-
getRsDate
Converts the object to aRsDate.- Parameters:
o- object to be convertedformat- format to be applied- Returns:
RsDatevalue of object,nullotherwise
-
getRsDate
public static RsDate getRsDate(java.lang.Object o, java.text.DateFormat format, RsDate defaultValue)Converts the object to aRsDate.- Parameters:
o- object to be convertedformat- format to be applieddefaultValue- value to be returned when object cannot be transformed.- Returns:
RsDatevalue 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 aRsDate.- Parameters:
o- object to be convertedformat- format to be applieddefaultValue- value to be returned when object cannot be transformed.logError- whether parsing error shall be logged- Returns:
RsDatevalue 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 firstDateFormatthat produces a result.- Parameters:
o- object to be transformedformats- formats to be used- Returns:
- the date if it could be parsed,
nullotherwise
-
getDate
public static java.util.Date getDate(java.lang.Object o, java.text.DateFormat[] formats, java.util.Date defaultValue)Returns the date using the firstDateFormatthat produces a result.- Parameters:
o- object to be transformedformats- formats to be useddefaultValue- 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 aDate.- Parameters:
o- object to be converted- Returns:
Datevalue of object,nullotherwise
-
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 convertedformat- format to be applied- Returns:
Datevalue of object,nullotherwise
-
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 convertedformat- format to be applieddefaultValue- value to be returned when object cannot be transformed.- Returns:
Datevalue 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 convertedformat- format to be applieddefaultValue- value to be returned when object cannot be transformed.logError- whether parsing error shall be logged- Returns:
Datevalue 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.IOExceptionSerializes 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.IOExceptionSerializes 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.IOExceptionUnserializes 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 foundjava.io.IOException- when unserialization fails
-
unserialize
public static java.lang.Object unserialize(byte[] bytes) throws java.lang.ClassNotFoundException, java.io.IOExceptionUnserializes 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 foundjava.io.IOException- when unserialization fails
-
toString
public static java.lang.String toString(java.lang.String className, java.lang.Object... properties)Creates a unifiedtoString()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 usedproperties- 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 withTransientannotation.- 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 withIBean.copyTo(Object). ANoCopyproperty has either a missing getter or setter or its getter is marked withNoCopyannotation.- Parameters:
desc- property descriptor- Returns:
- true when property is transient and must not be persisted
-
sleep
public static void sleep(long milliseconds)A wrapper forThread.sleep(long)to avoid try/catch blocks. The method throws aRuntimeExceptionin case of exceptions.- Parameters:
milliseconds- milliseconds to sleep
-
isInstanceOf
@Deprecated public static boolean isInstanceOf(java.lang.Object o, java.lang.String className)Deprecated.Replacement for "instanceof" operator when it cannot be guaranteed that the class is available in classpath at runtime.- Parameters:
o- the object to be checkedclassName- 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)Deprecated.Replacement forClass.isAssignableFrom(Class)when it cannot be guaranteed that the class is available in classpath at runtime.- Parameters:
inspectedClass- the class to be checkedclassName- 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
-