public final class ClassUtil
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> java.lang.Class<T> |
forClass(java.lang.String clsName)
Supports primitive types: boolean, char, byte, short, int, long, float, double.
|
static java.lang.String |
formalizePropName(java.lang.String propName)
It's designed for field/method/class/column/table names.
|
static java.util.Set<java.lang.Class<?>> |
getAllInterfaces(java.lang.Class<?> cls)
Copied from Apache Commons Lang under Apache License v2.
|
static java.util.List<java.lang.Class<?>> |
getAllSuperclasses(java.lang.Class<?> cls)
Copied from Apache Commons Lang under Apache License v2.
|
static java.util.Set<java.lang.Class<?>> |
getAllSuperTypes(java.lang.Class<?> cls)
Returns all the interfaces and super classes the specified class implements or extends, excluding
Object.class . |
static java.lang.String |
getCanonicalClassName(java.lang.Class<?> cls) |
static java.util.List<java.lang.Class<?>> |
getClassesByPackage(java.lang.String pkgName,
boolean isRecursive,
boolean skipClassLoaddingException) |
static <E extends java.lang.Exception> |
getClassesByPackage(java.lang.String pkgName,
boolean isRecursive,
boolean skipClassLoaddingException,
Try.Predicate<? super java.lang.Class<?>,E> predicate) |
static java.lang.String |
getClassName(java.lang.Class<?> cls) |
static <T> java.lang.reflect.Constructor<T> |
getDeclaredConstructor(java.lang.Class<T> cls,
java.lang.Class<?>... parameterTypes)
Returns the constructor declared in the specified
cls with the specified parameterTypes . |
static java.lang.reflect.Method |
getDeclaredMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns the method declared in the specified
cls with the specified methodName and parameterTypes . |
static java.lang.Class<?> |
getEnclosingClass(java.lang.Class<?> cls) |
static java.lang.Package |
getPackage(java.lang.Class<?> cls) |
static java.lang.String |
getPackageName(java.lang.Class<?> cls) |
static java.lang.String |
getParameterizedTypeNameByMethod(java.lang.reflect.Method method) |
static java.lang.reflect.Field |
getPropField(java.lang.Class<?> cls,
java.lang.String propName) |
static java.lang.reflect.Method |
getPropGetMethod(java.lang.Class<?> cls,
java.lang.String propName)
Returns the property get method declared in the specified
cls
with the specified property name propName . |
static java.util.Map<java.lang.String,java.lang.reflect.Method> |
getPropGetMethodList(java.lang.Class<?> cls)
Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property
getter/setter method for the class/bean generated/wrote by JAXB
specificatio
|
static java.lang.String |
getPropNameByMethod(java.lang.reflect.Method getSetMethod) |
static java.util.List<java.lang.String> |
getPropNameList(java.lang.Class<?> cls)
Returns an immutable entity property name List by the specified class.
|
static java.util.List<java.lang.String> |
getPropNameListExclusively(java.lang.Class<?> cls,
java.util.Collection<java.lang.String> propNameToExcluded) |
static java.util.List<java.lang.String> |
getPropNameListExclusively(java.lang.Class<?> cls,
java.util.Set<java.lang.String> propNameToExcluded) |
static java.lang.reflect.Method |
getPropSetMethod(java.lang.Class<?> cls,
java.lang.String propName)
Returns the property set method declared in the specified
cls
with the specified property name propName . |
static java.util.Map<java.lang.String,java.lang.reflect.Method> |
getPropSetMethodList(java.lang.Class<?> cls) |
static <T> T |
getPropValue(java.lang.Object entity,
java.lang.reflect.Method propGetMethod)
Return the specified
propValue got by the specified method
propSetMethod in the specified entity . |
static <T> T |
getPropValue(java.lang.Object entity,
java.lang.String propName)
Refer to getPropValue(Method, Object)
|
static <T> T |
getPropValue(java.lang.Object entity,
java.lang.String propName,
boolean ignoreUnknownProperty) |
static java.lang.String |
getSimpleClassName(java.lang.Class<?> cls) |
static java.lang.Class<?>[] |
getTypeArgumentsByMethod(java.lang.reflect.Method method) |
static <T> T |
invokeConstructor(java.lang.reflect.Constructor<T> constructor,
java.lang.Object... args) |
static <T> T |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object... args) |
static <T> T |
invokeMethod(java.lang.Object instance,
java.lang.reflect.Method method,
java.lang.Object... args) |
static void |
registerNonEntityClass(java.lang.Class<?> cls) |
static void |
registerNonPropGetSetMethod(java.lang.Class<?> cls,
java.lang.String propName) |
static void |
registerPropGetSetMethod(java.lang.String propName,
java.lang.reflect.Method method) |
static void |
registerXMLBindingClassForPropGetSetMethod(java.lang.Class<?> cls)
The property maybe only has get method if its type is collection or map by xml binding specificatio
Otherwise, it will be ignored if not registered by calling this method.
|
static void |
setPropValue(java.lang.Object entity,
java.lang.reflect.Method propSetMethod,
java.lang.Object propValue)
Set the specified
propValue to entity by the specified
method propSetMethod . |
static void |
setPropValue(java.lang.Object entity,
java.lang.String propName,
java.lang.Object propValue)
Refer to setPropValue(Method, Object, Object).
|
static boolean |
setPropValue(java.lang.Object entity,
java.lang.String propName,
java.lang.Object propValue,
boolean ignoreUnknownProperty) |
static void |
setPropValueByGet(java.lang.Object entity,
java.lang.reflect.Method propGetMethod,
java.lang.Object propValue) |
static void |
toLowerCaseKeyWithUnderscore(java.util.Map<java.lang.String,java.lang.Object> props) |
static java.lang.String |
toLowerCaseWithUnderscore(java.lang.String str)
It's designed for field/method/class/column/table names.
|
static void |
toUpperCaseKeyWithUnderscore(java.util.Map<java.lang.String,java.lang.Object> props) |
static java.lang.String |
toUpperCaseWithUnderscore(java.lang.String str)
It's designed for field/method/class/column/table names.
|
public static void registerXMLBindingClassForPropGetSetMethod(java.lang.Class<?> cls)
cls
- public static void registerNonEntityClass(java.lang.Class<?> cls)
public static void registerNonPropGetSetMethod(java.lang.Class<?> cls, java.lang.String propName)
public static void registerPropGetSetMethod(java.lang.String propName, java.lang.reflect.Method method)
public static <T> java.lang.Class<T> forClass(java.lang.String clsName) throws java.lang.IllegalArgumentException
java.lang.String[]
clsName
- java.lang.IllegalArgumentException
- if class not found.public static java.util.List<java.lang.Class<?>> getAllSuperclasses(java.lang.Class<?> cls)
Gets a List
of super classes for the given class, excluding Object.class
.
cls
- the class to look up.List
of super classes in order going up from this one.public static java.util.Set<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> cls)
Gets a List
of all interfaces implemented by the given
class and its super classes.
The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained.
cls
- the class to look up.List
of interfaces in orderpublic static java.util.Set<java.lang.Class<?>> getAllSuperTypes(java.lang.Class<?> cls)
Object.class
.cls
- public static java.lang.String getParameterizedTypeNameByMethod(java.lang.reflect.Method method)
public static java.lang.Class<?>[] getTypeArgumentsByMethod(java.lang.reflect.Method method)
public static java.lang.Package getPackage(java.lang.Class<?> cls)
cls
- null
if it's primitive type or no package defined for the class.public static java.lang.String getPackageName(java.lang.Class<?> cls)
cls
- null
if it's primitive type or no package defined for the class.public static java.util.List<java.lang.Class<?>> getClassesByPackage(java.lang.String pkgName, boolean isRecursive, boolean skipClassLoaddingException)
public static <E extends java.lang.Exception> java.util.List<java.lang.Class<?>> getClassesByPackage(java.lang.String pkgName, boolean isRecursive, boolean skipClassLoaddingException, Try.Predicate<? super java.lang.Class<?>,E> predicate) throws E extends java.lang.Exception
E extends java.lang.Exception
public static java.lang.String getClassName(java.lang.Class<?> cls)
public static java.lang.String getSimpleClassName(java.lang.Class<?> cls)
public static java.lang.String getCanonicalClassName(java.lang.Class<?> cls)
public static java.lang.Class<?> getEnclosingClass(java.lang.Class<?> cls)
@SafeVarargs public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor(java.lang.Class<T> cls, java.lang.Class<?>... parameterTypes)
cls
with the specified parameterTypes
.cls
- parameterTypes
- null
if no constructor is found@SafeVarargs public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
cls
with the specified methodName
and parameterTypes
.cls
- methodName
- parameterTypes
- null
if no method is found@SafeVarargs public static <T> T invokeConstructor(java.lang.reflect.Constructor<T> constructor, java.lang.Object... args)
@SafeVarargs public static <T> T invokeMethod(java.lang.reflect.Method method, java.lang.Object... args)
@SafeVarargs public static <T> T invokeMethod(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... args)
public static java.util.List<java.lang.String> getPropNameList(java.lang.Class<?> cls)
cls
- public static java.util.List<java.lang.String> getPropNameListExclusively(java.lang.Class<?> cls, java.util.Set<java.lang.String> propNameToExcluded)
cls
- propNameToExcluded
- public static java.util.List<java.lang.String> getPropNameListExclusively(java.lang.Class<?> cls, java.util.Collection<java.lang.String> propNameToExcluded)
public static java.util.Map<java.lang.String,java.lang.reflect.Method> getPropGetMethodList(java.lang.Class<?> cls)
cls
- public static java.util.Map<java.lang.String,java.lang.reflect.Method> getPropSetMethodList(java.lang.Class<?> cls)
public static java.lang.reflect.Method getPropGetMethod(java.lang.Class<?> cls, java.lang.String propName)
cls
with the specified property name propName
.
null
is returned if no method is found.
Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property
getter/setter method for the class/bean generated/wrote by JAXB
specificatiocls
- propName
- public static java.lang.reflect.Method getPropSetMethod(java.lang.Class<?> cls, java.lang.String propName)
cls
with the specified property name propName
.
null
is returned if no method is found.cls
- propName
- public static java.lang.reflect.Field getPropField(java.lang.Class<?> cls, java.lang.String propName)
public static <T> T getPropValue(java.lang.Object entity, java.lang.reflect.Method propGetMethod)
propValue
got by the specified method
propSetMethod
in the specified entity
.entity
- MapEntity is not supportedpropGetMethod
- public static void setPropValue(java.lang.Object entity, java.lang.reflect.Method propSetMethod, java.lang.Object propValue)
propValue
to entity
by the specified
method propSetMethod
. This method will try to convert
propValue
to appropriate type and set again if fails to set in
the first time. The final value which is set to the property will be
returned if property is set successfully finally. it could be the input
propValue
or converted property value, otherwise, exception will
be threw if the property value is set unsuccessfully.entity
- MapEntity is not supportedpropSetMethod
- propValue
- public static void setPropValueByGet(java.lang.Object entity, java.lang.reflect.Method propGetMethod, java.lang.Object propValue)
public static <T> T getPropValue(java.lang.Object entity, java.lang.String propName)
entity
- propName
- is case insensitivegetPropValue(Object, Method)
public static <T> T getPropValue(java.lang.Object entity, java.lang.String propName, boolean ignoreUnknownProperty)
entity
- propName
- ignoreUnknownProperty
- java.lang.IllegalArgumentException
- if the specified property can't be gotten and ignoreUnknownProperty is false.public static void setPropValue(java.lang.Object entity, java.lang.String propName, java.lang.Object propValue)
entity
- propName
- is case insensitivepropValue
- public static boolean setPropValue(java.lang.Object entity, java.lang.String propName, java.lang.Object propValue, boolean ignoreUnknownProperty)
entity
- propName
- propValue
- ignoreUnknownProperty
- java.lang.IllegalArgumentException
- if the specified property can't be set and ignoreUnknownProperty is false.public static java.lang.String getPropNameByMethod(java.lang.reflect.Method getSetMethod)
public static java.lang.String formalizePropName(java.lang.String propName)
propName
- public static java.lang.String toLowerCaseWithUnderscore(java.lang.String str)
str
- public static void toLowerCaseKeyWithUnderscore(java.util.Map<java.lang.String,java.lang.Object> props)
public static java.lang.String toUpperCaseWithUnderscore(java.lang.String str)
str
- public static void toUpperCaseKeyWithUnderscore(java.util.Map<java.lang.String,java.lang.Object> props)