Package io.microsphere.util
Class ClassUtils
- java.lang.Object
-
- io.microsphere.util.ClassUtils
-
- All Implemented Interfaces:
Utils
public abstract class ClassUtils extends java.lang.Object implements Utils
Class
utility class- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
ClassUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ARRAY_SUFFIX
Suffix for array class names: "[]"static java.util.Set<java.lang.Class<?>>
PRIMITIVE_ARRAY_TYPES
static java.util.Set<java.lang.Class<?>>
PRIMITIVE_TYPES
static java.util.Set<java.lang.Class<?>>
SIMPLE_TYPES
Simple Types including:Void
Boolean
Character
Byte
Integer
Float
Double
String
BigDecimal
BigInteger
Date
Object
static java.util.Set<java.lang.Class<?>>
WRAPPER_TYPES
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
arrayTypeEquals(java.lang.Class<?> oneArrayType, java.lang.Class<?> anotherArrayType)
static <T> T
cast(java.lang.Object object, java.lang.Class<T> castType)
Cast the given object to the specified typestatic java.util.List<java.lang.Class<?>>
findAllClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all classes from the specified type with filtersstatic java.util.List<java.lang.Class<?>>
findAllInheritedClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all inherited classes from the specified typestatic java.util.List<java.lang.Class<?>>
findAllInterfaces(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... interfaceFilters)
find all interfaces from the specified typestatic java.util.List<java.lang.Class<?>>
findAllSuperClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all super classes from the specified typestatic java.util.Set<java.lang.String>
findClassNamesInClassPath(java.io.File classPath, boolean recursive)
Find all class names in class pathstatic java.util.Set<java.lang.String>
findClassNamesInClassPath(java.lang.String classPath, boolean recursive)
Find all class names in class pathstatic java.util.Set<java.lang.String>
findClassNamesInDirectory(java.io.File classesDirectory, boolean recursive)
Find all class names in directorystatic java.util.Set<java.lang.String>
findClassNamesInJarFile(java.io.File jarFile, boolean recursive)
Find all class names in jar fileprotected static java.util.List<java.lang.Class<?>>
findTypes(java.lang.Class<?> type, boolean includeSelf, boolean includeHierarchicalTypes, boolean includeGenericSuperclass, boolean includeGenericInterfaces, java.util.function.Predicate<? super java.lang.Class<?>>... typeFilters)
static java.util.List<java.lang.Class<?>>
getAllClasses(java.lang.Class<?> type)
Get all classes from the specified typestatic java.util.List<java.lang.Class<?>>
getAllInheritedClasses(java.lang.Class<?> type)
Get all inherited classes from the specified typestatic java.util.List<java.lang.Class<?>>
getAllInheritedTypes(java.lang.Class<?> type)
Get all inherited types from the specified typestatic java.util.List<java.lang.Class<?>>
getAllInterfaces(java.lang.Class<?> type)
Get all interfaces from the specified typestatic java.util.List<java.lang.Class<?>>
getAllSuperClasses(java.lang.Class<?> type)
Get all super classes from the specified typestatic java.lang.String
getSimpleName(java.lang.Class<?> type)
Get the simple name of the specified typestatic java.lang.Class<?>
getTopComponentType(java.lang.Class<?> arrayType)
static java.lang.Class<?>
getTopComponentType(java.lang.Object array)
static java.lang.String
getTypeName(java.lang.Class<?> type)
Get the name of the specified typestatic java.lang.Class[]
getTypes(java.lang.Object... values)
Resolve the types of the specified valuesstatic boolean
isAbstractClass(java.lang.Class<?> type)
Is the specified type a abstract class or not?static boolean
isArray(java.lang.Class<?> type)
The specified type is array or not?static boolean
isAssignableFrom(java.lang.Class<?> superType, java.lang.Class<?> targetType)
the semantics is same asClass.isAssignableFrom(Class)
static boolean
isCharSequence(java.lang.Class<?> type)
Checks if the given type is a subtype ofCharSequence
.static boolean
isClass(java.lang.Object object)
Checks if the given object is an instance ofClass
.static boolean
isConcreteClass(java.lang.Class<?> type)
Is the specified type a concrete class or not?static boolean
isDerived(java.lang.Class<?> targetType, java.lang.Class<?>... superTypes)
the semantics is same asClass.isAssignableFrom(Class)
static boolean
isFinal(java.lang.Class<?> type)
static boolean
isGeneralClass(java.lang.Class<?> type)
Is the specified type a general class or not?protected static boolean
isGeneralClass(java.lang.Class<?> type, java.lang.Boolean isAbstract)
Is the specified type a general class or not?static boolean
isNumber(java.lang.Class<?> type)
Checks if the given type is a subtype ofNumber
.static boolean
isPrimitive(java.lang.Class<?> type)
The specified type is primitive type or simple typestatic boolean
isSimpleType(java.lang.Class<?> type)
The specified type is simple type or notstatic boolean
isTopLevelClass(java.lang.Class<?> type)
static boolean
isWrapperType(java.lang.Class<?> type)
static <T> T
newInstance(java.lang.Class<T> type, java.lang.Object... args)
protected static java.lang.String
resolveClassName(java.io.File classesDirectory, java.io.File classFile)
static java.lang.String
resolveClassName(java.lang.String resourceName)
Resolve resource name to class namestatic java.lang.String
resolvePackageName(java.lang.Class<?> targetClass)
Resolve package name under specified classstatic java.lang.String
resolvePackageName(java.lang.String className)
Resolve package name under specified class namestatic java.lang.Class<?>
resolvePrimitiveClassForName(java.lang.String name)
Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.static java.lang.Class<?>
resolvePrimitiveType(java.lang.Class<?> type)
Resolve the primitive class from the specified typestatic java.lang.Class[]
resolveTypes(java.lang.Object... values)
Resolve the types of the specified valuesstatic java.lang.Class<?>
resolveWrapperType(java.lang.Class<?> primitiveType)
Resolve the wrapper class from the primitive type
-
-
-
Field Detail
-
ARRAY_SUFFIX
public static final java.lang.String ARRAY_SUFFIX
Suffix for array class names: "[]"- See Also:
- Constant Field Values
-
PRIMITIVE_TYPES
@Nonnull @Immutable public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_TYPES
-
WRAPPER_TYPES
@Nonnull @Immutable public static final java.util.Set<java.lang.Class<?>> WRAPPER_TYPES
-
PRIMITIVE_ARRAY_TYPES
@Nonnull @Immutable public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_ARRAY_TYPES
-
SIMPLE_TYPES
@Immutable public static final java.util.Set<java.lang.Class<?>> SIMPLE_TYPES
Simple Types including:Void
Boolean
Character
Byte
Integer
Float
Double
String
BigDecimal
BigInteger
Date
Object
- See Also:
SimpleType
-
-
Method Detail
-
isArray
public static boolean isArray(java.lang.Class<?> type)
The specified type is array or not?*- Parameters:
type
- the type to test- Returns:
true
if the specified type is an array class,false
otherwise- See Also:
Class.isArray()
-
isConcreteClass
public static boolean isConcreteClass(java.lang.Class<?> type)
Is the specified type a concrete class or not?- Parameters:
type
- type to check- Returns:
true
if concrete class,false
otherwise.
-
isAbstractClass
public static boolean isAbstractClass(java.lang.Class<?> type)
Is the specified type a abstract class or not?- Parameters:
type
- the type- Returns:
- true if type is a abstract class, false otherwise.
-
isGeneralClass
public static boolean isGeneralClass(java.lang.Class<?> type)
Is the specified type a general class or not?- Parameters:
type
- the type- Returns:
- true if type is a general class, false otherwise.
-
isGeneralClass
protected static boolean isGeneralClass(java.lang.Class<?> type, java.lang.Boolean isAbstract)
Is the specified type a general class or not?If
isAbstract
==null
, it will not checktype
is abstract or not.- Parameters:
type
- the typeisAbstract
- optional abstract flag- Returns:
- true if type is a general (abstract) class, false otherwise.
-
isTopLevelClass
public static boolean isTopLevelClass(java.lang.Class<?> type)
-
isPrimitive
public static boolean isPrimitive(java.lang.Class<?> type)
The specified type is primitive type or simple typeIt's an optimized implementation for
Class.isPrimitive()
.- Parameters:
type
- the type to test- Returns:
- See Also:
Class.isPrimitive()
-
isFinal
public static boolean isFinal(java.lang.Class<?> type)
-
isSimpleType
public static boolean isSimpleType(java.lang.Class<?> type)
The specified type is simple type or not- Parameters:
type
- the type to test- Returns:
- if
type
is one element ofSIMPLE_TYPES
, returntrue
, orfalse
- See Also:
SIMPLE_TYPES
-
isCharSequence
public static boolean isCharSequence(java.lang.Class<?> type)
Checks if the given type is a subtype ofCharSequence
.Example Usage
boolean result = ClassUtils.isCharSequence(String.class); // returns true boolean result2 = ClassUtils.isCharSequence(Integer.class); // returns false
- Parameters:
type
- the class to check, may benull
- Returns:
true
if the type is a subtype ofCharSequence
,false
otherwise
-
isNumber
public static boolean isNumber(java.lang.Class<?> type)
Checks if the given type is a subtype ofNumber
.Example Usage
boolean result = ClassUtils.isNumber(Integer.class); // returns true boolean result2 = ClassUtils.isNumber(String.class); // returns false
- Parameters:
type
- the class to check, may benull
- Returns:
true
if the type is a subtype ofNumber
,false
otherwise
-
isClass
public static boolean isClass(java.lang.Object object)
Checks if the given object is an instance ofClass
.Example Usage
boolean result1 = ClassUtils.isClass(String.class); // returns true boolean result2 = ClassUtils.isClass("Hello"); // returns false boolean result3 = ClassUtils.isClass(null); // returns false
- Parameters:
object
- the object to check, may benull
- Returns:
true
if the object is an instance ofClass
,false
otherwise
-
resolvePrimitiveType
public static java.lang.Class<?> resolvePrimitiveType(java.lang.Class<?> type)
Resolve the primitive class from the specified type- Parameters:
type
- the specified type- Returns:
null
if not found
-
resolveWrapperType
public static java.lang.Class<?> resolveWrapperType(java.lang.Class<?> primitiveType)
Resolve the wrapper class from the primitive type- Parameters:
primitiveType
- the primitive type- Returns:
null
if not found
-
isWrapperType
public static boolean isWrapperType(java.lang.Class<?> type)
-
arrayTypeEquals
public static boolean arrayTypeEquals(java.lang.Class<?> oneArrayType, java.lang.Class<?> anotherArrayType)
-
resolvePrimitiveClassForName
public static java.lang.Class<?> resolvePrimitiveClassForName(java.lang.String name)
Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.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
.- Parameters:
name
- the name of the potentially primitive class- Returns:
- the primitive class, or
null
if the name does not denote a primitive class or primitive array class
-
resolvePackageName
@Nullable public static java.lang.String resolvePackageName(java.lang.Class<?> targetClass)
Resolve package name under specified class- Parameters:
targetClass
- target class- Returns:
- package name
-
resolvePackageName
@Nullable public static java.lang.String resolvePackageName(java.lang.String className)
Resolve package name under specified class name- Parameters:
className
- class name- Returns:
- package name
-
findClassNamesInClassPath
@Nonnull @Immutable public static java.util.Set<java.lang.String> findClassNamesInClassPath(java.lang.String classPath, boolean recursive)
Find all class names in class path- Parameters:
classPath
- class pathrecursive
- is recursive on sub directories- Returns:
- all class names in class path
-
findClassNamesInClassPath
@Nonnull @Immutable public static java.util.Set<java.lang.String> findClassNamesInClassPath(java.io.File classPath, boolean recursive)
Find all class names in class path- Parameters:
classPath
- JarFile or class patch directoryrecursive
- is recursive on sub directories- Returns:
- all class names in class path
-
findClassNamesInDirectory
@Nonnull @Immutable public static java.util.Set<java.lang.String> findClassNamesInDirectory(java.io.File classesDirectory, boolean recursive)
Find all class names in directory- Parameters:
classesDirectory
- a directory to be foundrecursive
- is recursive on sub directories- Returns:
- all class names in directory
-
findClassNamesInJarFile
@Nonnull @Immutable public static java.util.Set<java.lang.String> findClassNamesInJarFile(java.io.File jarFile, boolean recursive)
Find all class names in jar file- Parameters:
jarFile
- jar filerecursive
- is recursive on sub directories- Returns:
- all class names in jar file
-
resolveClassName
protected static java.lang.String resolveClassName(java.io.File classesDirectory, java.io.File classFile)
-
resolveClassName
public static java.lang.String resolveClassName(java.lang.String resourceName)
Resolve resource name to class name- Parameters:
resourceName
- resource name- Returns:
- class name
-
getAllSuperClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> getAllSuperClasses(java.lang.Class<?> type)
Get all super classes from the specified type- Parameters:
type
- the specified type- Returns:
- non-null read-only
List
-
getAllInterfaces
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> type)
Get all interfaces from the specified type- Parameters:
type
- the specified type- Returns:
- non-null read-only
List
-
getAllInheritedTypes
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> getAllInheritedTypes(java.lang.Class<?> type)
Get all inherited types from the specified type- Parameters:
type
- the specified type- Returns:
- non-null read-only
List
-
getAllInheritedClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> getAllInheritedClasses(java.lang.Class<?> type)
Get all inherited classes from the specified type- Parameters:
type
- the specified type- Returns:
- non-null read-only
List
-
getAllClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> getAllClasses(java.lang.Class<?> type)
Get all classes from the specified type- Parameters:
type
- the specified type- Returns:
- non-null read-only
List
-
findAllSuperClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> findAllSuperClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all super classes from the specified type- Parameters:
type
- the specified typeclassFilters
- the filters for classes- Returns:
- non-null read-only
List
-
findAllInterfaces
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> findAllInterfaces(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... interfaceFilters)
find all interfaces from the specified type- Parameters:
type
- the specified typeinterfaceFilters
- the filters for interfaces- Returns:
- non-null read-only
List
-
findAllInheritedClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> findAllInheritedClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all inherited classes from the specified type- Parameters:
type
- the specified typeclassFilters
- the filters for types- Returns:
- non-null read-only
List
-
findAllClasses
@Nonnull @Immutable public static java.util.List<java.lang.Class<?>> findAllClasses(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
Find all classes from the specified type with filters- Parameters:
type
- the specified typeclassFilters
- class filters- Returns:
- non-null read-only
List
-
findTypes
@Nonnull @Immutable protected static java.util.List<java.lang.Class<?>> findTypes(java.lang.Class<?> type, boolean includeSelf, boolean includeHierarchicalTypes, boolean includeGenericSuperclass, boolean includeGenericInterfaces, java.util.function.Predicate<? super java.lang.Class<?>>... typeFilters)
-
isAssignableFrom
public static boolean isAssignableFrom(java.lang.Class<?> superType, java.lang.Class<?> targetType)
the semantics is same asClass.isAssignableFrom(Class)
- Parameters:
superType
- the super typetargetType
- the target type- Returns:
- see
Class.isAssignableFrom(Class)
-
getTypes
public static java.lang.Class[] getTypes(java.lang.Object... values)
Resolve the types of the specified values- Parameters:
values
- the values- Returns:
- If can't be resolved, return
empty class array
-
resolveTypes
public static java.lang.Class[] resolveTypes(java.lang.Object... values)
Resolve the types of the specified values- Parameters:
values
- the values- Returns:
- If can't be resolved, return
empty class array
-
getTypeName
public static java.lang.String getTypeName(java.lang.Class<?> type)
Get the name of the specified type- Parameters:
type
- the specified type- Returns:
- non-null
-
getSimpleName
public static java.lang.String getSimpleName(java.lang.Class<?> type)
Get the simple name of the specified type- Parameters:
type
- the specified type- Returns:
- non-null
-
isDerived
public static boolean isDerived(java.lang.Class<?> targetType, java.lang.Class<?>... superTypes)
the semantics is same asClass.isAssignableFrom(Class)
- Parameters:
targetType
- the target typesuperTypes
- the super types- Returns:
- see
Class.isAssignableFrom(Class)
-
newInstance
public static <T> T newInstance(java.lang.Class<T> type, java.lang.Object... args)
-
getTopComponentType
public static java.lang.Class<?> getTopComponentType(java.lang.Object array)
-
getTopComponentType
public static java.lang.Class<?> getTopComponentType(java.lang.Class<?> arrayType)
-
cast
public static <T> T cast(java.lang.Object object, java.lang.Class<T> castType)
Cast the given object to the specified type- Type Parameters:
T
- the type to cast- Parameters:
object
- the objectcastType
- the type to cast- Returns:
- the casted instance if and only if
object
is an instance ofcastType
,null
otherwise
-
-