Package com.landawn.abacus.util
Class ClassUtil
java.lang.Object
com.landawn.abacus.util.ClassUtil
- Since:
- 0.9
- Author:
- Haiyang Li
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandle
createMethodHandle
(Method method) static int
distanceOfInheritance
(Class<?> child, Class<?> parent) Returns the number of inheritance hops between two classes.static <T> Class<T>
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.getAllInterfaces
(Class<?> cls) Copied from Apache Commons Lang under Apache License v2.getAllSuperclasses
(Class<?> cls) Copied from Apache Commons Lang under Apache License v2.getAllSuperTypes
(Class<?> cls) Returns all the interfaces and super classes the specified class implements or extends, excludingObject.class
.getBuilderInfo
(Class<?> cls) static String
getCanonicalClassName
(Class<?> cls) Gets the canonical class name.getClassesByPackage
(String pkgName, boolean isRecursive, boolean skipClassLoaddingException) Gets the classes by package.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 <T> Constructor<T>
getDeclaredConstructor
(Class<T> cls, Class<?>... parameterTypes) Returns the constructor declared in the specifiedcls
with the specifiedparameterTypes
.static Method
getDeclaredMethod
(Class<?> cls, String methodName, Class<?>... parameterTypes) Returns the method declared in the specifiedcls
with the specifiedmethodName
andparameterTypes
.static Class<?>
getEnclosingClass
(Class<?> cls) Gets the enclosing class.getNonNullPropNames
(Object entity) static Package
getPackage
(Class<?> cls) Gets the package.static String
getPackageName
(Class<?> cls) Gets the package name.static String
Gets the parameterized type name by method.static String
Gets the parameterized type name by method.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 specifiedcls
with the specified property namepropName
.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.getPropNames
(Object entity, Throwables.BiPredicate<Object, com.landawn.abacus.type.Type<Object>, E> propValueFilter) getPropNames
(Object entity, Throwables.Predicate<Object, E> propValueFilter) getPropNamesExclusively
(Class<?> cls, Collection<String> propNameToExcluded) Gets the prop name list exclusively.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 specifiedcls
with the specified property namepropName
.static ImmutableMap<String,
Method> getPropSetMethods
(Class<?> cls) Gets the prop set method list.static <T> T
getPropValue
(Object entity, Method propGetMethod) Return the specifiedpropValue
got by the specified methodpropSetMethod
in the specifiedentity
.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<?>>
Gets anIterator
that can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.static ObjIterator<Class<?>>
Gets anIterator
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
(Object instance, Method method, Object... args) static <T> T
invokeMethod
(Method method, Object... args) static boolean
isAnonymousClass
(Class<?> cls) static boolean
isAnonymousOrMemeberClass
(Class<?> cls) static boolean
Checks if is entity.static boolean
isMemberClass
(Class<?> cls) static boolean
isRecordClass
(Class<?> cls) static boolean
isRegisteredXMLBindingClass
(Class<?> cls) 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 specifiedpropValue
toentity
by the specified methodpropSetMethod
.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 String
toCamelCase
(String propName) It's designed for field/method/class/column/table names.static void
toCamelCase
(Map<String, Object> props) To camel case.static void
toLowerCaseKeyWithUnderscore
(Map<String, Object> props) To lower case key with underscore.static String
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
It's designed for field/method/class/column/table names.
-
Field Details
-
CLASS_MASK
Deprecated.for internal only.The Constant CLASS_MASK. -
METHOD_MASK
Deprecated.for internal only.The Constant METHOD_MASK. -
FIELD_MASK
Deprecated.for internal only.The Constant FIELD_MASK.
-
-
Method Details
-
registerNonEntityClass
Register non entity class.- Parameters:
cls
-
-
registerNonPropGetSetMethod
Register non prop get set method.- Parameters:
cls
-propName
-
-
registerPropGetSetMethod
Register prop get set method.- Parameters:
propName
-method
-
-
registerXMLBindingClass
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.- Parameters:
cls
-
-
isRegisteredXMLBindingClass
-
createMethodHandle
-
distanceOfInheritance
Returns the number of inheritance hops between two classes.
- Parameters:
child
- the child class, may benull
parent
- the parent class, may benull
- Returns:
- the number of generations between the child and parent; 0 if the same class; -1 if the classes are not related as child and parent (includes where either class is null)
- Since:
- 3.2
-
forClass
Supports primitive types: boolean, char, byte, short, int, long, float, double. And array type with formatjava.lang.String[]
- Type Parameters:
T
-- Parameters:
clsName
-- Returns:
- Throws:
IllegalArgumentException
- if class not found.
-
formalizePropName
It's designed for field/method/class/column/table names. and source and target Strings will be cached.- Parameters:
propName
-- Returns:
-
formatParameterizedTypeName
Format parameterized type name.- Parameters:
parameterizedTypeName
-- Returns:
-
getCanonicalClassName
Gets the canonical class name.- Parameters:
cls
-- Returns:
-
getClassName
Gets the class name.- Parameters:
cls
-- Returns:
-
getSimpleClassName
Gets the simple class name.- Parameters:
cls
-- Returns:
-
getPackage
Gets the package.- Parameters:
cls
-- Returns:
null
if it's primitive type or no package defined for the class.
-
getPackageName
Gets the package name.- Parameters:
cls
-- Returns:
null
if it's primitive type or no package defined for the class.
-
getClassesByPackage
public static List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException) throws UncheckedIOException Gets the classes by package.- Parameters:
pkgName
-isRecursive
-skipClassLoaddingException
-- Returns:
- Throws:
UncheckedIOException
- the unchecked IO exception
-
getClassesByPackage
public static <E extends Exception> List<Class<?>> getClassesByPackage(String pkgName, boolean isRecursive, boolean skipClassLoaddingException, Throwables.Predicate<? super Class<?>, E> predicate) throws UncheckedIOException, EGets the classes by package.- Type Parameters:
E
-- Parameters:
pkgName
-isRecursive
-skipClassLoaddingException
-predicate
-- Returns:
- Throws:
UncheckedIOException
- the unchecked IO exceptionE
- the e
-
getAllInterfaces
Copied from Apache Commons Lang under Apache License v2.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.
- Parameters:
cls
- the class to look up.- Returns:
-
getAllSuperclasses
Copied from Apache Commons Lang under Apache License v2.Gets a
List
of super classes for the given class, excludingObject.class
.- Parameters:
cls
- the class to look up.- Returns:
-
getAllSuperTypes
Returns all the interfaces and super classes the specified class implements or extends, excludingObject.class
.- Parameters:
cls
-- Returns:
-
getEnclosingClass
Gets the enclosing class.- Parameters:
cls
-- Returns:
-
getDeclaredConstructor
@SafeVarargs public static <T> Constructor<T> getDeclaredConstructor(Class<T> cls, Class<?>... parameterTypes) Returns the constructor declared in the specifiedcls
with the specifiedparameterTypes
.- Type Parameters:
T
-- Parameters:
cls
-parameterTypes
-- Returns:
null
if no constructor is found
-
getDeclaredMethod
@SafeVarargs public static Method getDeclaredMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) Returns the method declared in the specifiedcls
with the specifiedmethodName
andparameterTypes
.- Parameters:
cls
-methodName
-parameterTypes
-- Returns:
null
if no method is found
-
getParameterizedTypeNameByField
Gets the parameterized type name by method.- Parameters:
field
-- Returns:
-
getParameterizedTypeNameByMethod
Gets the parameterized type name by method.- Parameters:
method
-- Returns:
-
getPropNameByMethod
Gets the prop name by method.- Parameters:
getSetMethod
-- Returns:
-
getPropNameList
Returns an immutable entity property name List by the specified class.- Parameters:
cls
-- Returns:
-
getPropNames
public static <E extends Exception> List<String> getPropNames(Object entity, Throwables.BiPredicate<Object, com.landawn.abacus.type.Type<Object>, throws EE> propValueFilter) - Type Parameters:
E
-- Parameters:
entity
-propValueFilter
- first parameter is property value, second parameter is property type.- Returns:
- Throws:
E
-
getPropNames
public static <E extends Exception> List<String> getPropNames(Object entity, Throwables.Predicate<Object, E> propValueFilter) throws E- Type Parameters:
E
-- Parameters:
entity
-propValueFilter
-- Returns:
- Throws:
E
-
getPropNamesExclusively
public static List<String> getPropNamesExclusively(Class<?> cls, Collection<String> propNameToExcluded) Gets the prop name list exclusively.- Parameters:
cls
-propNameToExcluded
-- Returns:
-
getPropNamesExclusively
Gets the prop name list exclusively.- Parameters:
cls
-propNameToExcluded
-- Returns:
-
getNonNullPropNames
- Parameters:
entity
-- Returns:
-
getPropField
Gets the prop field.- Parameters:
cls
-propName
-- Returns:
-
getPropFields
-
getPropGetMethod
Returns the property get method declared in the specifiedcls
with the specified property namepropName
.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 specification- Parameters:
cls
-propName
-- Returns:
-
getPropGetMethods
Call registerXMLBindingClassForPropGetSetMethod first to retrieve the property getter/setter method for the class/bean generated/wrote by JAXB specification.- Parameters:
cls
-- Returns:
-
getPropSetMethod
Returns the property set method declared in the specifiedcls
with the specified property namepropName
.null
is returned if no method is found.- Parameters:
cls
-propName
-- Returns:
-
getPropSetMethods
Gets the prop set method list.- Parameters:
cls
-- Returns:
-
getPropValue
Return the specifiedpropValue
got by the specified methodpropSetMethod
in the specifiedentity
.- Type Parameters:
T
-- Parameters:
entity
- MapEntity is not supportedpropGetMethod
-- Returns:
-
getPropValue
Refer to getPropValue(Method, Object).- Type Parameters:
T
-- Parameters:
entity
-propName
- is case insensitive- Returns:
getPropValue(Object, Method)
-
getPropValue
Gets the prop value.- Type Parameters:
T
-- Parameters:
entity
-propName
-ignoreUnmatchedProperty
-- Returns:
- Throws:
IllegalArgumentException
- if the specified property can't be gotten and ignoreUnmatchedProperty is false.
-
setPropValue
Set the specifiedpropValue
toentity
by the specified methodpropSetMethod
. This method will try to convertpropValue
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 inputpropValue
or converted property value, otherwise, exception will be threw if the property value is set unsuccessfully.- Parameters:
entity
- MapEntity is not supportedpropSetMethod
-propValue
-
-
setPropValue
Deprecated.Refer to setPropValue(Method, Object, Object).- Parameters:
entity
-propName
- is case insensitivepropValue
-
-
setPropValue
@Deprecated public static boolean setPropValue(Object entity, String propName, Object propValue, boolean ignoreUnmatchedProperty) Deprecated.Sets the prop value.- Parameters:
entity
-propName
-propValue
-ignoreUnmatchedProperty
-- Returns:
- true if the property value has been set.
- Throws:
IllegalArgumentException
- if the specified property can't be set and ignoreUnmatchedProperty is false.
-
setPropValueByGet
Sets the prop value by get.- Parameters:
entity
-propGetMethod
-propValue
-
-
getBuilderInfo
-
getTypeName
-
hierarchy
Gets anIterator
that can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.- Parameters:
type
- the type to get the class hierarchy from- Returns:
- Iterator an Iterator over the class hierarchy of the given class
- Since:
- 3.2
-
hierarchy
Gets anIterator
that can iterate over a class hierarchy in ascending (subclass to superclass) order.- Parameters:
type
- the type to get the class hierarchy fromincludeInterface
- switch indicating whether to include or exclude interfaces- Returns:
- Iterator an Iterator over the class hierarchy of the given class
- Since:
- 3.2
-
invokeConstructor
- Type Parameters:
T
-- Parameters:
constructor
-args
-- Returns:
-
invokeMethod
- Type Parameters:
T
-- Parameters:
method
-args
-- Returns:
-
invokeMethod
- Type Parameters:
T
-- Parameters:
instance
-method
-args
-- Returns:
-
isEntity
Checks if is entity.- Parameters:
cls
-- Returns:
- true, if is entity
-
setAccessible
- Parameters:
accessibleObject
-flag
-
-
setAccessibleQuietly
- Parameters:
accessibleObject
-flag
-- Returns:
true
if no error happens, otherwisefalse
is returned.
-
toCamelCase
To camel case.- Parameters:
props
-
-
toCamelCase
It's designed for field/method/class/column/table names. and source and target Strings will be cached.- Parameters:
propName
-- Returns:
-
toLowerCaseKeyWithUnderscore
To lower case key with underscore.- Parameters:
props
-
-
toLowerCaseWithUnderscore
It's designed for field/method/class/column/table names. and source and target Strings will be cached.- Parameters:
str
-- Returns:
-
toUpperCaseWithUnderscore
It's designed for field/method/class/column/table names. and source and target Strings will be cached.- Parameters:
str
-- Returns:
-
toUpperCaseKeyWithUnderscore
To upper case key with underscore.- Parameters:
props
-
-
isRecordClass
-
isAnonymousClass
-
isMemberClass
-
isAnonymousOrMemeberClass
-