public abstract class ClassUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ARRAY_SUFFIX
Suffix for array class names
|
Constructor and Description |
---|
ClassUtils() |
Modifier and Type | Method and Description |
---|---|
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 Class |
forName(String name)
Replacement for
Class.forName() that also returns Class instances for primitives
(like "int") and array class names (like "String[]"). |
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[] |
getAllInterfaces(Object object)
Return all interfaces that the given object implements as array, including ones implemented
by superclasses.
|
static Set<Class> |
getAllInterfacesAsSet(Object object)
Return all interfaces that the given object implements as List, including ones implemented by
superclasses.
|
static Class[] |
getAllInterfacesForClass(Class clazz)
Return all interfaces that the given class implements as array, including ones implemented by
superclasses.
|
static Set<Class> |
getAllInterfacesForClassAsSet(Class clazz)
Return all interfaces that the given class implements as Set, including ones implemented by
superclasses.
|
static Class |
getClass(Object obj) |
static ClassLoader |
getDefaultClassLoader()
Return a default ClassLoader to use (never
null ). |
static int |
getMethodCountForName(Class clazz,
String methodName)
Return the number of methods with a given name (with any argument types), for the given class
and/or its superclasses.
|
static Method |
getMethodIfAvailable(Class clazz,
String methodName,
Class[] paramTypes)
Determine whether the given class has a method with the given signature, and return it if
available (else return
null ). |
static String |
getPackage(String className) |
static String |
getQualifiedMethodName(Method method)
Return the qualified name of the given method, consisting of fully qualified interface/class
name + "." + method name.
|
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 decapitalized JavaBeans property format.
|
static Method |
getStaticMethod(Class clazz,
String methodName,
Class[] args)
Return a static method of a class.
|
static String |
getTypeDisplayName(String typeName) |
static boolean |
hasAtLeastOneMethodWithName(Class clazz,
String methodName)
Does the given class and/or its superclasses at least have one or more methods (with any
argument types)? Includes non-public methods.
|
static boolean |
hasMethod(Class clazz,
String methodName,
Class[] paramTypes)
Determine whether the given class has a method with the given signature.
|
static boolean |
isAssignable(Class targetType,
Class valueType)
Determine if the given target type is assignable from the given value 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 |
isPresent(String className)
Return 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.
|
static boolean |
isPrimitiveOrWrapper(Class clazz)
Check if the given class represents a primitive (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 Class |
resolvePrimitiveClassName(String name)
Resolve the given class name as primitive class, if appropriate.
|
public static final String ARRAY_SUFFIX
public static ClassLoader getDefaultClassLoader()
null
). Returns the thread context
ClassLoader, if available. The ClassLoader that loaded the ClassUtils class will be used as
fallback. 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).
Thread.getContextClassLoader()
public static boolean isPresent(String className)
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.public static Class forName(String name) throws ClassNotFoundException
Class.forName()
that also returns Class instances for primitives
(like "int") and array class names (like "String[]"). Always uses the default class loader: that is, preferably the thread context class loader, or the ClassLoader that loaded the ClassUtils class as fallback.
name
- the name of the ClassClassNotFoundException
Class.forName(String, boolean, ClassLoader)
,
getDefaultClassLoader()
public static Class forName(String name, ClassLoader classLoader) throws ClassNotFoundException
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 useClassNotFoundException
Class.forName(String, boolean, ClassLoader)
public static Class resolvePrimitiveClassName(String name)
name
- the name of the potentially primitive classnull
if the name does not denote a primitive
classpublic 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 getQualifiedName(Class clazz)
clazz
- the classpublic static String getQualifiedMethodName(Method method)
method
- the methodpublic static boolean hasMethod(Class clazz, String methodName, Class[] paramTypes)
Essentially
translates NoSuchMethodException
to "false".
clazz
- the clazz to analyzemethodName
- the name of the methodparamTypes
- the parameter types of the methodClass.getMethod(java.lang.String, java.lang.Class<?>...)
public static Method getMethodIfAvailable(Class clazz, String methodName, Class[] paramTypes)
null
). Essentially translates
NoSuchMethodException
to null
.
clazz
- the clazz to analyzemethodName
- the name of the methodparamTypes
- the parameter types of the methodnull
if not foundClass.getMethod(java.lang.String, java.lang.Class<?>...)
public static int getMethodCountForName(Class clazz, String methodName)
clazz
- the clazz to checkmethodName
- the name of the methodpublic static boolean hasAtLeastOneMethodWithName(Class clazz, String methodName)
clazz
- the clazz to checkmethodName
- the name of the methodpublic static Method getStaticMethod(Class clazz, String methodName, Class[] args)
methodName
- the static method nameclazz
- the class which defines the methodargs
- the parameter types to the methodnull
if no static method was foundIllegalArgumentException
- if the method name is blank or the clazz is nullpublic static boolean isPrimitiveWrapper(Class clazz)
public static boolean isPrimitiveOrWrapper(Class clazz)
public static boolean isPrimitiveArray(Class clazz)
public static boolean isPrimitiveWrapperArray(Class clazz)
public static boolean isAssignable(Class targetType, Class valueType)
targetType
- the target typevalueType
- 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. 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)
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 Class[] getAllInterfaces(Object object)
object
- the object to analyze for interfacespublic static Class[] getAllInterfacesForClass(Class clazz)
If the class itself is an interface, it gets returned as sole interface.
clazz
- the class to analyze for interfacespublic static Set<Class> getAllInterfacesAsSet(Object object)
object
- the object to analyze for interfacespublic 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 interfacespublic static Class getClass(Object obj)
null
if the obj is null
public static String getPackage(String className)
Copyright © GigaSpaces.