public class ClassUtils extends Object
限定符和类型 | 字段和说明 |
---|---|
static String |
ARRAY_SUFFIX
Suffix for array class names: "[]"
|
static String |
CLASS_FILE_SUFFIX
The ".class" file suffix
|
构造器和说明 |
---|
ClassUtils() |
限定符和类型 | 方法和说明 |
---|---|
static String |
addResourcePathToPackagePath(Class<?> clazz,
String resourceName)
Return a path suitable for use with
ClassLoader.getResource
(also suitable for use with Class.getResource by prepending a
slash ('/') to the return value). |
static String |
classPackageAsResourcePath(Class<?> clazz)
Given an input class object, return a string which consists of the
class's package name as a pathname, i.e., all dots ('.') are replaced by
slashes ('/').
|
static Set<Class> |
findAllAssignableClass(Class<?> clazz) |
static Set<Class> |
findAllAssignableClass(Class<?> clazz,
ClassLoader classLoader)
Find all the classes which is either the same as, or is a superclass or superinterface of.
|
static Class<?> |
forName(String name,
ClassLoader classLoader)
Replacement for
Class.forName() that also returns Class instances
for primitives (e.g." |
static Set<Class> |
getAllInterfaces(Class<?> c) |
static Set<Class> |
getAllInterfaces(Class<?> c,
ClassLoader classLoader)
get all interfaces of the class. include all ancestors of all the interfaces of the class,
but not include interface from the parent or ancestors of the class.
|
static Set<Class> |
getAllInterfacesForClassAsSet(Class clazz)
Return all interfaces that the given class implements as Set,
including ones implemented by superclasses.
|
static Set<Class> |
getAllInterfacesForClassAsSet(Class clazz,
ClassLoader classLoader)
Return all interfaces that the given class implements as Set,
including ones implemented by superclasses.
|
static Set<Class> |
getAllSuperClass(Class<?> clazz)
Get all super classes of the class.
|
static String |
getClassFileName(Class<?> clazz)
Determine the name of the class file, relative to the containing
package: e.g.
|
static ClassLoader |
getDefaultClassLoader()
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 String |
getDescriptiveType(Object value)
Return a descriptive name for the given object's type: usually simply
the class name, but component type class name + "[]" for arrays,
and an appended list of implemented interfaces for JDK proxies.
|
static String |
getPackageName(Class<?> clazz)
Determine the name of the package of the given class:
e.g.
|
static String |
getQualifiedName(Class<?> clazz)
Return the qualified name of the given class: usually simply
the class name, but component type class name + "[]" for arrays.
|
static String |
getShortName(Class<?> clazz)
Get the class name without the qualified package name.
|
static String |
getShortName(String className)
Get the class name without the qualified package name.
|
static String |
getShortNameAsProperty(Class<?> clazz)
Return the short string name of a Java class in uncapitalized JavaBeans
property format.
|
static boolean |
isAssignable(Class<?> lhsType,
Class<?> rhsType)
Check if the right-hand side type may be assigned to the left-hand side
type, assuming setting by reflection.
|
static boolean |
isAssignableValue(Class<?> type,
Object value)
Determine if the given type is assignable from the given value,
assuming setting by reflection.
|
static boolean |
isAssignableWithNum(Class<?> lhsType,
Class<?> rhsType) |
static boolean |
isCacheSafe(Class<?> clazz,
ClassLoader classLoader)
Check whether the given class is cache-safe in the given context,
i.e. whether it is loaded by the given ClassLoader or a parent of it.
|
static boolean |
isNumAssignableFrom(Class<?> target,
Class<?> source)
判断 数值类型source 是否能赋值给 数值类型target, 赋值顺序: byte -> short -> int -> long -> float -> double
|
static boolean |
isNumber(Class<?> target) |
static boolean |
isPresent(String className,
ClassLoader classLoader)
Determine whether the
Class identified by the supplied name is present
and can be loaded. |
static boolean |
isPrimitiveArray(Class<?> clazz)
Check if the given class represents an array of primitives,
i.e. boolean, byte, char, short, int, long, float, or double.
|
static boolean |
isPrimitiveOrWrapper(Class<?> clazz)
Check if the given class represents a primitive (i.e. boolean, byte,
char, short, int, long, float, or double) or a primitive wrapper
(i.e.
|
static boolean |
isPrimitiveWrapper(Class<?> clazz)
Check if the given class represents a primitive wrapper,
i.e.
|
static boolean |
isPrimitiveWrapperArray(Class<?> clazz)
Check if the given class represents an array of primitive wrappers,
i.e.
|
static boolean |
isVisible(Class<?> clazz,
ClassLoader classLoader)
Check whether the given class is visible in the given ClassLoader.
|
static boolean |
matchesTypeName(Class<?> clazz,
String typeName)
Check whether the given class matches the user-specified type name.
|
static Object |
minType(Number value) |
static <T> T |
newInstance(String name,
ClassLoader classLoader) |
static Class<?> |
resolveClassName(String className,
ClassLoader classLoader)
Resolve the given class name into a Class instance.
|
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 Class<?> |
resolvePrimitiveIfNecessary(Class<?> clazz)
Resolve the given class if it is a primitive class,
returning the corresponding primitive wrapper type instead.
|
public static ClassLoader getDefaultClassLoader()
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 <T> T newInstance(String name, ClassLoader classLoader)
public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError
Class.forName()
that also returns Class instances
for primitives (e.g."int") and array class names (e.g. "String[]").
Furthermore, it is also capable of resolving inner class names in Java source
style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State").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<?> resolveClassName(String className, ClassLoader classLoader) throws IllegalArgumentException
This is effectively equivalent to the forName
method with the same arguments, with the only difference being
the exceptions thrown in case of class loading failure.
className
- the name of the ClassclassLoader
- the class loader to use
(may be null
, which indicates the default class loader)IllegalArgumentException
- if the class name was not resolvable
(that is, the class could not be found or the class file could not be loaded)forName(String, 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(String, ClassLoader)
.
name
- the name of the potentially primitive classnull
if the name does not denote
a primitive class or primitive array classpublic static boolean isPresent(String className, ClassLoader classLoader)
Class
identified by the supplied name is present
and can be loaded. Will return false
if either the class or
one of its dependencies is not present or cannot be loaded.className
- the name of the class to checkclassLoader
- the class loader to use
(may be null
, which indicates the default class loader)public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader)
clazz
- the class to analyzeclassLoader
- the ClassLoader to potentially cache metadata inpublic static String getShortName(String className)
className
- the className to get the short name forIllegalArgumentException
- if the className is emptypublic static String getShortName(Class<?> clazz)
clazz
- the class to get the short name forpublic static String getShortNameAsProperty(Class<?> clazz)
clazz
- the classIntrospector.decapitalize(String)
public static String getClassFileName(Class<?> clazz)
clazz
- the classpublic static String getPackageName(Class<?> clazz)
java.lang.String
class.clazz
- the classpublic static String getQualifiedName(Class<?> clazz)
clazz
- the classpublic static String getDescriptiveType(Object value)
value
- the value to introspectpublic static boolean matchesTypeName(Class<?> clazz, String typeName)
clazz
- the class to checktypeName
- the type name to matchpublic static boolean isPrimitiveWrapper(Class<?> clazz)
clazz
- the class to checkpublic static boolean isPrimitiveOrWrapper(Class<?> clazz)
clazz
- the class to checkpublic static boolean isPrimitiveArray(Class<?> clazz)
clazz
- the class to checkpublic static boolean isPrimitiveWrapperArray(Class<?> clazz)
clazz
- the class to checkpublic static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz)
clazz
- the class to checkpublic static boolean isAssignable(Class<?> lhsType, Class<?> rhsType)
lhsType
- the target typerhsType
- the value type that should be assigned to the target typepublic static boolean isAssignableValue(Class<?> type, Object value)
type
- the target typevalue
- the value that should be assigned to the typepublic static String addResourcePathToPackagePath(Class<?> clazz, String resourceName)
ClassLoader.getResource
(also suitable for use with Class.getResource
by prepending a
slash ('/') to the return value). Built by taking the package of the specified
class file, converting all dots ('.') to slashes ('/'), adding a trailing slash
if necessary, and concatenating the specified resource name to this.
org.springframework.core.io.ClassPathResource
is usually
even more convenient.clazz
- the Class whose package will be used as the baseresourceName
- the resource name to append. A leading slash is optional.ClassLoader.getResource(java.lang.String)
,
Class.getResource(java.lang.String)
public static String classPackageAsResourcePath(Class<?> clazz)
ClassLoader.getResource()
. For it to be fed to
Class.getResource
instead, a leading slash would also have
to be prepended to the returned value.clazz
- the input class. A null
value or the default
(empty) package will result in an empty string ("") being returned.ClassLoader.getResource(java.lang.String)
,
Class.getResource(java.lang.String)
public static Set<Class> getAllInterfacesForClassAsSet(Class clazz)
注意:If the class itself is an interface, it gets returned as sole interface.
clazz
- the class to analyze for interfaces#getAllInterfaces(Class)}
public static Set<Class> getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader)
注意:If the class itself is an interface, it gets returned as sole interface.
clazz
- the class to analyze for interfacesclassLoader
- the ClassLoader that the interfaces need to be visible in
(may be null
when accepting all declared interfaces)#getAllInterfaces(Class)}
public static boolean isVisible(Class<?> clazz, ClassLoader classLoader)
clazz
- the class to check (typically an interface)classLoader
- the ClassLoader to check against (may be null
,
in which case this method will always return true
)public static Set<Class> getAllSuperClass(Class<?> clazz)
clazz
- public static Set<Class> findAllAssignableClass(Class<?> clazz)
clazz
- #findAllAssignableClass(Class, ClassLoader)}
public static Set<Class> findAllAssignableClass(Class<?> clazz, ClassLoader classLoader)
clazz
- classLoader
- ClassLoader for check isVisible.public static Set<Class> getAllInterfaces(Class<?> c, ClassLoader classLoader)
c
- classLoader
- ClassLoader for check isVisible.#isVisible(Class, ClassLoader)}
public static boolean isNumAssignableFrom(Class<?> target, Class<?> source)
public static boolean isNumber(Class<?> target)
Copyright © 2018 jretty-org. All rights reserved.