public final class ClassUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
static Class<?> |
CLASS_MASK
Deprecated.
for internal only.
|
static Field |
FIELD_MASK
Deprecated.
for internal only.
|
static Method |
METHOD_MASK
Deprecated.
for internal only.
|
| Modifier and Type | Method and Description |
|---|---|
static MethodHandle |
createMethodHandle(Method method) |
static int |
distanceOfInheritance(Class<?> child,
Class<?> parent)
Returns the number of inheritance hops between two classes.
|
static <T> Class<T> |
forClass(String clsName)
Supports primitive types: boolean, char, byte, short, int, long, float, double.
|
static String |
formalizePropName(String propName)
It's designed for field/method/class/column/table names.
|
static String |
formatParameterizedTypeName(String parameterizedTypeName)
Format parameterized type name.
|
static Set<Class<?>> |
getAllInterfaces(Class<?> cls)
Copied from Apache Commons Lang under Apache License v2.
|
static List<Class<?>> |
getAllSuperclasses(Class<?> cls)
Copied from Apache Commons Lang under Apache License v2.
|
static Set<Class<?>> |
getAllSuperTypes(Class<?> cls)
Returns all the interfaces and super classes the specified class implements or extends, excluding
Object.class. |
static String |
getCanonicalClassName(Class<?> cls)
Gets the canonical class name.
|
static List<Class<?>> |
getClassesByPackage(String pkgName,
boolean isRecursive,
boolean skipClassLoaddingException)
Gets the classes by package.
|
static <E extends Exception> |
getClassesByPackage(String pkgName,
boolean isRecursive,
boolean skipClassLoaddingException,
Throwables.Predicate<? super Class<?>,E> predicate)
Gets the classes by package.
|
static String |
getClassName(Class<?> cls)
Gets the class name.
|
static ImmutableMap<String,String> |
getColumn2PropNameMap(Class<?> entityClass)
Gets the column 2 field name map.
|
static <T> Constructor<T> |
getDeclaredConstructor(Class<T> cls,
Class<?>... parameterTypes)
Returns the constructor declared in the specified
cls with the specified parameterTypes. |
static Method |
getDeclaredMethod(Class<?> cls,
String methodName,
Class<?>... parameterTypes)
Returns the method declared in the specified
cls with the specified methodName and parameterTypes. |
static Class<?> |
getEnclosingClass(Class<?> cls)
Gets the enclosing class.
|
static List<String> |
getIdFieldNames(Class<?> targetClass)
Deprecated.
for internal only.
|
static List<String> |
getIdFieldNames(Class<?> targetClass,
boolean fakeIdForEmpty)
Deprecated.
for internal only.
|
static List<String> |
getNonNullPropNames(Object entity) |
static Package |
getPackage(Class<?> cls)
Gets the package.
|
static String |
getPackageName(Class<?> cls)
Gets the package name.
|
static String |
getParameterizedTypeNameByField(Field field)
Gets the parameterized type name by method.
|
static String |
getParameterizedTypeNameByMethod(Method method)
Gets the parameterized type name by method.
|
static ImmutableMap<String,String> |
getProp2ColumnNameMap(Class<?> entityClass,
NamingPolicy namingPolicy)
Gets the prop column name map.
|
static Field |
getPropField(Class<?> cls,
String propName)
Gets the prop field.
|
static ImmutableMap<String,Field> |
getPropFields(Class<?> cls) |
static Method |
getPropGetMethod(Class<?> cls,
String propName)
Returns the property get method declared in the specified
cls
with the specified property name propName. |
static ImmutableMap<String,Method> |
getPropGetMethods(Class<?> cls)
Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property
getter/setter method for the class/bean generated/wrote by JAXB
specification.
|
static String |
getPropNameByMethod(Method getSetMethod)
Gets the prop name by method.
|
static ImmutableList<String> |
getPropNameList(Class<?> cls)
Returns an immutable entity property name List by the specified class.
|
static <E extends Exception> |
getPropNames(Object entity,
Throwables.BiPredicate<Object,com.landawn.abacus.type.Type<Object>,E> propValueFilter) |
static <E extends Exception> |
getPropNames(Object entity,
Throwables.Predicate<Object,E> propValueFilter) |
static List<String> |
getPropNamesExclusively(Class<?> cls,
Collection<String> propNameToExcluded)
Gets the prop name list exclusively.
|
static List<String> |
getPropNamesExclusively(Class<?> cls,
Set<String> propNameToExcluded)
Gets the prop name list exclusively.
|
static Method |
getPropSetMethod(Class<?> cls,
String propName)
Returns the property set method declared in the specified
cls
with the specified property name propName. |
static ImmutableMap<String,Method> |
getPropSetMethods(Class<?> cls)
Gets the prop set method list.
|
static <T> T |
getPropValue(Object entity,
Method propGetMethod)
Return the specified
propValue got by the specified method
propSetMethod in the specified entity. |
static <T> T |
getPropValue(Object entity,
String propName)
Refer to getPropValue(Method, Object).
|
static <T> T |
getPropValue(Object entity,
String propName,
boolean ignoreUnmatchedProperty)
Gets the prop value.
|
static String |
getSimpleClassName(Class<?> cls)
Gets the simple class name.
|
static String |
getTypeName(Type type) |
static ObjIterator<Class<?>> |
hierarchy(Class<?> type)
Gets an
Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order,
excluding interfaces. |
static ObjIterator<Class<?>> |
hierarchy(Class<?> type,
boolean includeInterface)
Gets an
Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order. |
static <T> T |
invokeConstructor(Constructor<T> constructor,
Object... args) |
static <T> T |
invokeMethod(Method method,
Object... args) |
static <T> T |
invokeMethod(Object instance,
Method method,
Object... args) |
static boolean |
isDirtyMarker(Class<?> cls) |
static boolean |
isEntity(Class<?> cls)
Checks if is entity.
|
static boolean |
isFakeId(List<String> idPropNames)
Deprecated.
for internal only.
|
static void |
registerNonEntityClass(Class<?> cls)
Register non entity class.
|
static void |
registerNonPropGetSetMethod(Class<?> cls,
String propName)
Register non prop get set method.
|
static void |
registerPropGetSetMethod(String propName,
Method method)
Register prop get set method.
|
static void |
registerXMLBindingClass(Class<?> cls)
The property maybe only has get method if its type is collection or map by xml binding specification
Otherwise, it will be ignored if not registered by calling this method.
|
static void |
setAccessible(AccessibleObject accessibleObject,
boolean flag) |
static boolean |
setAccessibleQuietly(AccessibleObject accessibleObject,
boolean flag) |
static void |
setPropValue(Object entity,
Method propSetMethod,
Object propValue)
Set the specified
propValue to entity by the specified
method propSetMethod. |
static void |
setPropValue(Object entity,
String propName,
Object propValue)
Deprecated.
|
static boolean |
setPropValue(Object entity,
String propName,
Object propValue,
boolean ignoreUnmatchedProperty)
Deprecated.
|
static void |
setPropValueByGet(Object entity,
Method propGetMethod,
Object propValue)
Sets the prop value by get.
|
static void |
toCamelCase(Map<String,Object> props)
To camel case.
|
static String |
toCamelCase(String propName)
It's designed for field/method/class/column/table names.
|
static void |
toLowerCaseKeyWithUnderscore(Map<String,Object> props)
To lower case key with underscore.
|
static String |
toLowerCaseWithUnderscore(String str)
It's designed for field/method/class/column/table names.
|
static void |
toUpperCaseKeyWithUnderscore(Map<String,Object> props)
To upper case key with underscore.
|
static String |
toUpperCaseWithUnderscore(String str)
It's designed for field/method/class/column/table names.
|
@Deprecated @Internal public static final Class<?> CLASS_MASK
@Deprecated @Internal public static final Method METHOD_MASK
@Deprecated @Internal public static final Field FIELD_MASK
public static void registerXMLBindingClass(Class<?> cls)
cls - public static void registerNonEntityClass(Class<?> cls)
cls - public static void registerNonPropGetSetMethod(Class<?> cls, String propName)
cls - propName - public static void registerPropGetSetMethod(String propName, Method method)
propName - method - public static boolean isEntity(Class<?> cls)
cls - public static boolean isDirtyMarker(Class<?> cls)
cls - public static <T> Class<T> forClass(String clsName) throws IllegalArgumentException
java.lang.String[]T - clsName - IllegalArgumentException - if class not found.public static List<Class<?>> getAllSuperclasses(Class<?> cls)
Gets a List of super classes for the given class, excluding Object.class.
public static Set<Class<?>> getAllInterfaces(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.public static Set<Class<?>> getAllSuperTypes(Class<?> cls)
Object.class.cls - public static String getParameterizedTypeNameByField(Field field)
field - public static String getParameterizedTypeNameByMethod(Method method)
method - public static String formatParameterizedTypeName(String parameterizedTypeName)
parameterizedTypeName - public static Package getPackage(Class<?> cls)
cls - null if it's primitive type or no package defined for the class.public static String getPackageName(Class<?> cls)
cls - null if it's primitive type or no package defined for the class.public static List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException) throws UncheckedIOException
pkgName - isRecursive - skipClassLoaddingException - UncheckedIOException - the unchecked IO exceptionpublic static <E extends Exception> List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException, Throwables.Predicate<? super Class<?>,E> predicate) throws UncheckedIOException, E extends Exception
E - pkgName - isRecursive - skipClassLoaddingException - predicate - UncheckedIOException - the unchecked IO exceptionE - the eE extends Exceptionpublic static String getClassName(Class<?> cls)
cls - public static String getSimpleClassName(Class<?> cls)
cls - public static String getCanonicalClassName(Class<?> cls)
cls - public static Class<?> getEnclosingClass(Class<?> cls)
cls - @SafeVarargs public static <T> Constructor<T> getDeclaredConstructor(Class<T> cls, Class<?>... parameterTypes)
cls with the specified parameterTypes.T - cls - parameterTypes - null if no constructor is found@SafeVarargs public static Method getDeclaredMethod(Class<?> cls, String methodName, Class<?>... parameterTypes)
cls with the specified methodName and parameterTypes.cls - methodName - parameterTypes - null if no method is found@SafeVarargs public static <T> T invokeConstructor(Constructor<T> constructor, Object... args)
T - constructor - args - @SafeVarargs public static <T> T invokeMethod(Method method, Object... args)
T - method - args - @SafeVarargs public static <T> T invokeMethod(Object instance, Method method, Object... args)
T - instance - method - args - public static ImmutableList<String> getPropNameList(Class<?> cls)
cls - public static List<String> getPropNamesExclusively(Class<?> cls, Set<String> propNameToExcluded)
cls - propNameToExcluded - public static List<String> getPropNamesExclusively(Class<?> cls, Collection<String> propNameToExcluded)
cls - propNameToExcluded - public static List<String> getNonNullPropNames(Object entity)
entity - public static <E extends Exception> List<String> getPropNames(Object entity, Throwables.Predicate<Object,E> propValueFilter) throws E extends Exception
E - entity - propValueFilter - EE extends Exceptionpublic static <E extends Exception> List<String> getPropNames(Object entity, Throwables.BiPredicate<Object,com.landawn.abacus.type.Type<Object>,E> propValueFilter) throws E extends Exception
E - entity - propValueFilter - first parameter is property value, second parameter is property type.EE extends Exceptionpublic static ImmutableMap<String,Field> getPropFields(Class<?> cls)
public static Field getPropField(Class<?> cls, String propName)
cls - propName - public static ImmutableMap<String,Method> getPropGetMethods(Class<?> cls)
cls - public static Method getPropGetMethod(Class<?> cls, 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
specificationcls - propName - public static ImmutableMap<String,Method> getPropSetMethods(Class<?> cls)
cls - public static Method getPropSetMethod(Class<?> cls, String propName)
cls
with the specified property name propName.
null is returned if no method is found.cls - propName - public static <T> T getPropValue(Object entity, Method propGetMethod)
propValue got by the specified method
propSetMethod in the specified entity.T - entity MapEntity is not supportedpropGetMethod - public static void setPropValue(Object entity, Method propSetMethod, 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(Object entity, Method propGetMethod, Object propValue)
entity - propGetMethod - propValue - public static <T> T getPropValue(Object entity, String propName)
T - entity - is case insensitivegetPropValue(Object, Method)public static <T> T getPropValue(Object entity, String propName, boolean ignoreUnmatchedProperty)
T - entity - propName - ignoreUnmatchedProperty - IllegalArgumentException - if the specified property can't be gotten and ignoreUnmatchedProperty is false.@Deprecated public static void setPropValue(Object entity, String propName, Object propValue)
ParserUtil.EntityInfo.setPropValue(Object, String, Object)entity - is case insensitivepropValue - @Deprecated public static boolean setPropValue(Object entity, String propName, Object propValue, boolean ignoreUnmatchedProperty)
ParserUtil.EntityInfo.setPropValue(Object, String, Object, boolean)entity - propName - propValue - ignoreUnmatchedProperty - IllegalArgumentException - if the specified property can't be set and ignoreUnmatchedProperty is false.public static String getPropNameByMethod(Method getSetMethod)
getSetMethod - public static String formalizePropName(String propName)
propName - public static String toCamelCase(String propName)
propName - public static void toCamelCase(Map<String,Object> props)
props - public static String toLowerCaseWithUnderscore(String str)
str - public static void toLowerCaseKeyWithUnderscore(Map<String,Object> props)
props - public static String toUpperCaseWithUnderscore(String str)
str - public static void toUpperCaseKeyWithUnderscore(Map<String,Object> props)
props - @Deprecated @Internal @Immutable public static List<String> getIdFieldNames(Class<?> targetClass)
targetClass - @Deprecated @Internal @Immutable public static List<String> getIdFieldNames(Class<?> targetClass, boolean fakeIdForEmpty)
targetClass - fakeIdForEmpty - @Deprecated @Internal public static boolean isFakeId(List<String> idPropNames)
idPropNames - public static void setAccessible(AccessibleObject accessibleObject, boolean flag)
accessibleObject - flag - public static boolean setAccessibleQuietly(AccessibleObject accessibleObject, boolean flag)
accessibleObject - flag - true if no error happens, otherwise false is returned.public static MethodHandle createMethodHandle(Method method)
public static ImmutableMap<String,String> getColumn2PropNameMap(Class<?> entityClass)
entityClass - public static ImmutableMap<String,String> getProp2ColumnNameMap(Class<?> entityClass, NamingPolicy namingPolicy)
public static ObjIterator<Class<?>> hierarchy(Class<?> type)
Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order,
excluding interfaces.type - the type to get the class hierarchy frompublic static ObjIterator<Class<?>> hierarchy(Class<?> type, boolean includeInterface)
Iterator that can iterate over a class hierarchy in ascending (subclass to superclass) order.type - the type to get the class hierarchy fromincludeInterface - switch indicating whether to include or exclude interfacespublic static int distanceOfInheritance(Class<?> child, Class<?> parent)
Returns the number of inheritance hops between two classes.
child - the child class, may be nullparent - the parent class, may be nullCopyright © 2021. All rights reserved.