Package rs.baselib.lang
Class LangUtils
java.lang.Object
rs.baselib.lang.LangUtils
Reflection and language Utils.
- Author:
- ralph
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
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 BigDecimal
Converts the object to a BigDecimal.static BigDecimal
getBigDecimal
(Object o, BigDecimal defaultValue) Converts the object to a BigDecimal.static boolean
getBoolean
(Object o) Converts the object to a boolean.static Class<?>
Deprecated.static Date
Converts the object to aDate
.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 firstDateFormat
that produces a result.static Date
getDate
(Object o, DateFormat[] formats, Date defaultValue) Returns the date using the firstDateFormat
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
Converts the object to a double.static double
Converts the object to a double.static float
Converts the object to a float.static float
Converts the object to a float.static int
Converts the object to an int.static int
Converts the object to an int.static Locale
Returns the locale with the id.static Locale
Returns the locale with the id.static long
Converts the object to a long.static long
Converts the object to a long.static String
Converts the object to a string.getTypeArguments
(Class<T> baseClass, Class<? extends T> childClass) Deprecated.static boolean
isInstanceOf
(Class<?> inspectedClass, String className) Deprecated.static boolean
isInstanceOf
(Object o, String className) Deprecated.static boolean
Returns true when the given string is a number.static boolean
Returns true when the given string is a number with a given radix.static boolean
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[]
Serializes the given value.static String
serializeBase64
(Object value) Serializes the given value into BASE64.static void
sleep
(long milliseconds) A wrapper forThread.sleep(long)
to avoid try/catch blocks.static String
Creates a unifiedtoString()
output.static Object
unserialize
(byte[] bytes) Unserializes the given value.static Object
unserialize
(String value) Unserializes the given value.
-
Constructor Details
-
LangUtils
public LangUtils()
-
-
Method Details
-
forName
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 loadedclassLoader
- 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
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 foundIllegalAccessException
- when constructor cannot be calledInstantiationException
- when constructor throws errorsReflectiveOperationException
-
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 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:
ClassNotFoundException
- when class cannot be foundIllegalAccessException
- when constructor cannot be calledInstantiationException
- when constructor throws errorsReflectiveOperationException
-
getClass
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> List<Class<?>> getTypeArguments(Class<T> baseClass, 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
Returns true when the given string is a number.- Parameters:
s
- string to be verified- Returns:
true
when the string contains a number.
-
isNumber
Returns true when the given string is a number with a given radix.- Parameters:
s
- string to be verifiedradix
- the radix to check- Returns:
true
when the string contains a number.
-
getString
Converts the object to a string.- Parameters:
o
- object to be converted- Returns:
- null if object is null, string value of object otherwise
-
getInt
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
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
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
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
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
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
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
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
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
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
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
Returns the date using the firstDateFormat
that produces a result.- Parameters:
o
- object to be transformedformats
- formats to be used- Returns:
- the date if it could be parsed,
null
otherwise
-
getDate
Returns the date using the firstDateFormat
that 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
Converts the object to aDate
.- Parameters:
o
- object to be converted- Returns:
Date
value of object,null
otherwise
-
getDate
Converts the object to a date.- Parameters:
o
- object to be convertedformat
- format to be applied- Returns:
Date
value of object,null
otherwise
-
getDate
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:
Date
value of object, the default value otherwise
-
getDate
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:
Date
value of object, the default value otherwise
-
getLocale
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
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
Serializes the given value into BASE64.- Parameters:
value
- value to be serialized- Returns:
- the serialized string
- Throws:
IOException
- when serialization fails
-
serialize
Serializes the given value.- Parameters:
value
- value to be serialized- Returns:
- the serialized string
- Throws:
IOException
- when serialization fails
-
unserialize
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 foundIOException
- when unserialization fails
-
unserialize
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 foundIOException
- when unserialization fails
-
toString
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
Returns true when given property is transient. A transient property has either a missing getter or setter or its getter is marked withTransient
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 forThread.sleep(long)
to avoid try/catch blocks. The method throws aRuntimeException
in case of exceptions.- Parameters:
milliseconds
- milliseconds to sleep
-
isInstanceOf
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.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
-
ReflectionUtils.getClass(Type)