public class ClassUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
ARRAY_SUFFIX
Suffix for array class names: "[]"
|
static Set<Class<?>> |
SIMPLE_TYPES
|
| Constructor and Description |
|---|
ClassUtils() |
| Modifier and Type | Method and Description |
|---|---|
static Object |
convertPrimitive(Class<?> type,
String value) |
static Class<?> |
forName(String name)
Same as
Class.forName(), except that it works for primitive
types. |
static Class<?> |
forName(String name,
ClassLoader classLoader)
Replacement for
Class.forName() that also returns Class
instances for primitives (like "int") and array class names (like
"String[]"). |
static Class<?> |
forNameWithCallerClassLoader(String name,
Class<?> caller) |
static Class<?> |
forNameWithThreadContextClassLoader(String name) |
static Set<Class<?>> |
getAllInheritedTypes(Class<?> type,
Predicate<Class<?>>... typeFilters)
Get all inherited types from the specified type
|
static Set<Class<?>> |
getAllInterfaces(Class<?> type,
Predicate<Class<?>>... interfaceFilters)
Get all interfaces from the specified type
|
static Set<Class<?>> |
getAllSuperClasses(Class<?> type,
Predicate<Class<?>>... classFilters)
Get all super classes from the specified type
|
static ClassLoader |
getCallerClassLoader(Class<?> caller) |
static ClassLoader |
getClassLoader()
Return the default ClassLoader to use: typically the thread context
ClassLoader, if available; the ClassLoader that loaded the ClassUtils
class will be used as fallback.
|
static ClassLoader |
getClassLoader(Class<?> clazz)
get class loader
|
static boolean |
isAssignableFrom(Class<?> superType,
Class<?> targetType)
the semantics is same as
Class.isAssignableFrom(Class) |
static boolean |
isGenericClass(Class<?> type)
Is generic class or not?
|
static boolean |
isPresent(String className,
ClassLoader classLoader)
Test the specified class name is present in the
ClassLoader |
static boolean |
isPrimitive(Class<?> type)
Deprecated.
as 2.7.6, use
Class.isPrimitive() plus isSimpleType(Class) instead |
static boolean |
isSimpleType(Class<?> type)
The specified type is simple type or not
|
static boolean |
isTypeMatch(Class<?> type,
String value)
We only check boolean value at this moment.
|
static Class<?> |
resolveClass(String className,
ClassLoader classLoader)
Resolve the
Class by the specified name and ClassLoader |
static Class<?> |
resolvePrimitiveClassName(String name)
Resolve the given class name as primitive class, if appropriate,
according to the JVM's naming rules for primitive classes.
|
static String |
simpleClassName(Class<?> clazz) |
static String |
toShortString(Object obj) |
public static final String ARRAY_SUFFIX
public static final Set<Class<?>> SIMPLE_TYPES
SimpleTypepublic static Class<?> forNameWithThreadContextClassLoader(String name) throws ClassNotFoundException
ClassNotFoundExceptionpublic static Class<?> forNameWithCallerClassLoader(String name, Class<?> caller) throws ClassNotFoundException
ClassNotFoundExceptionpublic static ClassLoader getCallerClassLoader(Class<?> caller)
public static ClassLoader getClassLoader(Class<?> clazz)
clazz - public static ClassLoader getClassLoader()
Call this method if you intend to use the thread context ClassLoader in a
scenario where you absolutely need a non-null ClassLoader reference: for
example, for class path resource loading (but not necessarily for
Class.forName, which accepts a null ClassLoader
reference as well).
null)Thread.getContextClassLoader()public static Class<?> forName(String name) throws ClassNotFoundException
Class.forName(), except that it works for primitive
types.ClassNotFoundExceptionpublic static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError
Class.forName() that also returns Class
instances for primitives (like "int") and array class names (like
"String[]").name - the name of the ClassclassLoader - the class loader to use (may be null,
which indicates the default class loader)ClassNotFoundException - if the class was not foundLinkageError - if the class file could not be loadedClass.forName(String, boolean, ClassLoader)public static Class<?> resolvePrimitiveClassName(String name)
Also supports the JVM's internal class names for primitive arrays. Does
not support the "[]" suffix notation for primitive arrays; this is
only supported by forName(java.lang.String).
name - the name of the potentially primitive classnull if the name does not
denote a primitive class or primitive array classpublic static boolean isPrimitive(Class<?> type)
Class.isPrimitive() plus isSimpleType(Class) insteadtype - the type to testpublic static boolean isSimpleType(Class<?> type)
type - the type to testtype is one element of SIMPLE_TYPES, return true, or falseSIMPLE_TYPESpublic static boolean isTypeMatch(Class<?> type, String value)
type - value - public static Set<Class<?>> getAllSuperClasses(Class<?> type, Predicate<Class<?>>... classFilters)
type - the specified typeclassFilters - the filters for classesSetpublic static Set<Class<?>> getAllInterfaces(Class<?> type, Predicate<Class<?>>... interfaceFilters)
type - the specified typeinterfaceFilters - the filters for interfacesSetpublic static Set<Class<?>> getAllInheritedTypes(Class<?> type, Predicate<Class<?>>... typeFilters)
type - the specified typetypeFilters - the filters for typesSetpublic static boolean isAssignableFrom(Class<?> superType, Class<?> targetType)
Class.isAssignableFrom(Class)superType - the super typetargetType - the target typeClass.isAssignableFrom(Class)public static boolean isPresent(String className, ClassLoader classLoader)
ClassLoaderclassName - the name of ClassclassLoader - ClassLoadertruepublic static Class<?> resolveClass(String className, ClassLoader classLoader)
Class by the specified name and ClassLoaderclassName - the name of ClassclassLoader - ClassLoadernullpublic static boolean isGenericClass(Class<?> type)
type - the target typevoid or Void.class, return true, or falseCopyright © 2011–2022 The Apache Software Foundation. All rights reserved.