public class LangUtils extends Object
| Constructor and Description |
|---|
LangUtils() |
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
forName(String className)
Load a class by first checking the thread's class loader and then the caller's class loader.
|
static Class<?> |
forName(String className,
ClassLoader classLoader)
Load a class by directly specifying a class loader.
|
static boolean |
getBoolean(Object o)
Converts the object to a boolean.
|
static Class<?> |
getClass(Type type)
Get the underlying class for a type, or null if the type is a variable type.
|
static Date |
getDate(Object o)
Converts the object to a
Date. |
static Date |
getDate(Object o,
DateFormat format)
Converts the object to a date.
|
static Date |
getDate(Object o,
DateFormat[] formats)
Returns the date using the first
DateFormat that produces a result. |
static Date |
getDate(Object o,
DateFormat[] formats,
Date defaultValue)
Returns the date using the first
DateFormat that produces a result. |
static Date |
getDate(Object o,
DateFormat format,
Date defaultValue)
Converts the object to a date.
|
static Date |
getDate(Object o,
DateFormat format,
Date defaultValue,
boolean logError)
Converts the object to a date.
|
static double |
getDouble(Object o)
Converts the object to a double.
|
static double |
getDouble(Object o,
double defaultValue)
Converts the object to a double.
|
static float |
getFloat(Object o)
Converts the object to a float.
|
static float |
getFloat(Object o,
float defaultValue)
Converts the object to a float.
|
static int |
getInt(Object o)
Converts the object to an int.
|
static int |
getInt(Object o,
int defaultValue)
Converts the object to an int.
|
static Locale |
getLocale(String id)
Returns the locale with the id.
|
static Locale |
getLocale(String id,
Locale defaultLocale)
Returns the locale with the id.
|
static long |
getLong(Object o)
Converts the object to a long.
|
static long |
getLong(Object o,
long defaultValue)
Converts the object to a long.
|
static RsDate |
getRsDate(Object o)
Converts the object to a
RsDate. |
static RsDate |
getRsDate(Object o,
DateFormat format)
Converts the object to a
RsDate. |
static RsDate |
getRsDate(Object o,
DateFormat[] formats)
Returns the date using the first
DateFormat that produces a result. |
static RsDate |
getRsDate(Object o,
DateFormat[] formats,
RsDate defaultValue)
Returns the date using the first
DateFormat that produces a result. |
static RsDate |
getRsDate(Object o,
DateFormat format,
RsDate defaultValue)
Converts the object to a
RsDate. |
static RsDate |
getRsDate(Object o,
DateFormat format,
RsDate defaultValue,
boolean logError)
Converts the object to a
RsDate. |
static String |
getString(Object o)
Converts the object to a string.
|
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.
|
static boolean |
isJava6()
Returns true when the runtime is Java 6.
|
static boolean |
isNoCopy(PropertyDescriptor desc)
Returns true when given property is not allowed to be copied with
IBean.copyTo(Object). |
static boolean |
isTransient(PropertyDescriptor desc)
Returns true when given property is transient.
|
static Object |
newInstance(String className)
Instantiates a class object from class name.
|
static Object |
newInstance(String className,
ClassLoader classLoader)
Instantiates a class object from class name.
|
static byte[] |
serialize(Object value)
Serializes the given value.
|
static String |
serializeBase64(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 String |
toString(String className,
Object... properties)
Creates a unified
toString() output. |
static Object |
unserialize(byte[] bytes)
Unserializes the given value.
|
static Object |
unserialize(String value)
Unserializes the given value.
|
public static Class<?> forName(String className) throws ClassNotFoundException
className - name of cass to be loadedClassNotFoundException - when the class cannot be foundpublic static Class<?> forName(String className, ClassLoader classLoader) throws ClassNotFoundException
className - name of cass to be loadedclassLoader - the class loader to be used - if null the thread's class loader will be used firstClassNotFoundException - when the class cannot be foundpublic static Object newInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
forName(String).newInstance().className - the name of classClassNotFoundException - when class cannot be foundIllegalAccessException - when constructor cannot be calledInstantiationException - when constructor throws errorspublic static Object newInstance(String className, ClassLoader classLoader) throws ClassNotFoundException, IllegalAccessException, InstantiationException
forName(String,ClassLoader).newInstance().className - the name of classclassLoader - the class loader to be used - if null the thread's class loader will be used firstClassNotFoundException - when class cannot be foundIllegalAccessException - when constructor cannot be calledInstantiationException - when constructor throws errorspublic static Class<?> getClass(Type type)
type - the typepublic static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass)
baseClass - the base classchildClass - the child classpublic static String getString(Object o)
o - object to be convertedpublic static int getInt(Object o)
o - object to be convertedpublic static int getInt(Object o, int defaultValue)
o - object to be converteddefaultValue - the default value to returnpublic static long getLong(Object o)
o - object to be convertedpublic static long getLong(Object o, long defaultValue)
o - object to be converteddefaultValue - the default value to returnpublic static float getFloat(Object o)
o - object to be convertedpublic static float getFloat(Object o, float defaultValue)
o - object to be converteddefaultValue - the default value to returnpublic static double getDouble(Object o)
o - object to be convertedpublic static double getDouble(Object o, double defaultValue)
o - object to be converteddefaultValue - the default value to returnpublic static boolean getBoolean(Object o)
true are 1, true, on, yes, y (case-insensitive).o - object to be convertedpublic static RsDate getRsDate(Object o, DateFormat[] formats)
DateFormat that produces a result.o - object to be transformedformats - formats to be usednullpublic static RsDate getRsDate(Object o, DateFormat[] formats, RsDate defaultValue)
DateFormat that produces a result.o - object to be transformedformats - formats to be useddefaultValue - value to be returned when object cannot be transformed.public static RsDate getRsDate(Object o)
RsDate.o - object to be convertedRsDate value of object, null otherwisepublic static RsDate getRsDate(Object o, DateFormat format)
RsDate.o - object to be convertedformat - format to be appliedRsDate value of object, null otherwisepublic static RsDate getRsDate(Object o, DateFormat format, RsDate defaultValue)
RsDate.o - object to be convertedformat - format to be applieddefaultValue - value to be returned when object cannot be transformed.RsDate value of object, the default value otherwisepublic static RsDate getRsDate(Object o, DateFormat format, RsDate defaultValue, boolean logError)
RsDate.o - object to be convertedformat - format to be applieddefaultValue - value to be returned when object cannot be transformed.logError - whether parsing error shall be loggedRsDate value of object, the default value otherwisepublic static Date getDate(Object o, DateFormat[] formats)
DateFormat that produces a result.o - object to be transformedformats - formats to be usednull otherwisepublic static Date getDate(Object o, DateFormat[] formats, Date defaultValue)
DateFormat that produces a result.o - object to be transformedformats - formats to be useddefaultValue - value to be returned when object cannot be transformed.public static Date getDate(Object o)
Date.o - object to be convertedDate value of object, null otherwisepublic static Date getDate(Object o, DateFormat format)
o - object to be convertedformat - format to be appliedDate value of object, null otherwisepublic static Date getDate(Object o, DateFormat format, Date defaultValue)
o - object to be convertedformat - format to be applieddefaultValue - value to be returned when object cannot be transformed.Date value of object, the default value otherwisepublic static Date getDate(Object o, DateFormat format, Date defaultValue, boolean logError)
o - object to be convertedformat - format to be applieddefaultValue - value to be returned when object cannot be transformed.logError - whether parsing error shall be loggedDate value of object, the default value otherwisepublic static boolean isJava6()
public static Locale getLocale(String id)
id - id of locale as returned by toString()public static Locale getLocale(String id, Locale defaultLocale)
id - id of locale as returned by toString()defaultLocale - default to return when locale cannot be foundpublic static String serializeBase64(Object value) throws IOException
value - value to be serializedIOExceptionpublic static byte[] serialize(Object value) throws IOException
value - value to be serializedIOExceptionpublic static Object unserialize(String value) throws ClassNotFoundException, IOException
value - the serialized string (BASE64 encoded)ClassNotFoundExceptionIOExceptionpublic static Object unserialize(byte[] bytes) throws ClassNotFoundException, IOException
bytes - the bytes of serialized objectClassNotFoundExceptionIOExceptionpublic static String toString(String className, Object... properties)
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].
className - the class name to be usedproperties - the properties where each property is represented with two values: name of property and its value.public static boolean isTransient(PropertyDescriptor desc)
Transient annotation.desc - property descriptorpublic static boolean isNoCopy(PropertyDescriptor desc)
IBean.copyTo(Object).
A NoCopy property has either a missing getter or setter or its getter is
marked with NoCopy annotation.desc - property descriptorpublic static void sleep(long milliseconds)
Thread.sleep(long) to avoid try/catch blocks.
The method throws a RuntimeException in case of exceptions.milliseconds - milliseconds to sleepCopyright © 2014. All rights reserved.