public class ClassUtils
extends java.lang.Object
Operates on classes without using reflection.
This class handles invalid null inputs as best it can.
Each method documents its behaviour in more detail.
The notion of a canonical name includes the human
readable name for the type, for example int[]. The
non-canonical method variants work with the JVM names, such as
[I.
| 限定符和类型 | 字段和说明 |
|---|---|
static java.lang.String |
INNER_CLASS_SEPARATOR
The inner class separator String:
"$". |
static char |
INNER_CLASS_SEPARATOR_CHAR
The inner class separator character:
'$' == 36. |
static java.lang.String |
PACKAGE_SEPARATOR
The package separator String:
".". |
static char |
PACKAGE_SEPARATOR_CHAR
The package separator character:
'.' == 46. |
static java.lang.String |
STRING_EMPTY
Empty string.
|
| 构造器和说明 |
|---|
ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static java.util.List<java.lang.String> |
convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)
Given a
List of Class objects, this method converts
them into class names. |
static java.util.List<java.lang.Class<?>> |
convertClassNamesToClasses(java.util.List<java.lang.String> classNames)
Given a
List of class names, this method converts them into classes. |
static java.lang.String |
deleteWhitespace(java.lang.String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char). |
static java.util.List<java.lang.Class<?>> |
getAllInterfaces(java.lang.Class<?> cls)
Gets a
List of all interfaces implemented by the given
class and its superclasses. |
static java.util.List<java.lang.Class<?>> |
getAllSuperclasses(java.lang.Class<?> cls)
Gets a
List of superclasses for the given class. |
static java.lang.Class<?> |
getClass(java.lang.ClassLoader classLoader,
java.lang.String className)
Returns the (initialized) class represented by
className
using the classLoader. |
static java.lang.Class<?> |
getClass(java.lang.ClassLoader classLoader,
java.lang.String className,
boolean initialize)
Returns the class represented by
className using the
classLoader. |
static java.lang.Class<?> |
getClass(java.lang.String className)
Returns the (initialized) class represented by
className
using the current thread's context class loader. |
static java.lang.Class<?> |
getClass(java.lang.String className,
boolean initialize)
Returns the class represented by
className using the
current thread's context class loader. |
static java.lang.String |
getPackageName(java.lang.Class<?> cls)
Gets the package name of a
Class. |
static java.lang.String |
getPackageName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the package name of an
Object. |
static java.lang.String |
getPackageName(java.lang.String className)
Gets the package name from a
String. |
static java.lang.String |
getShortClassName(java.lang.Class<?> cls)
Gets the class name minus the package name from a
Class. |
static java.lang.String |
getShortClassName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the class name minus the package name for an
Object. |
static java.lang.String |
getShortClassName(java.lang.String className)
Gets the class name minus the package name from a String.
|
static java.lang.String |
getSimpleName(java.lang.Class<?> cls)
Null-safe version of
aClass.getSimpleName() |
static java.lang.String |
getSimpleName(java.lang.Object object,
java.lang.String valueIfNull)
Null-safe version of
aClass.getSimpleName() |
static boolean |
isEmpty(java.lang.CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static java.lang.Class<?>[] |
toClass(java.lang.Object... array)
Converts an array of
Object in to an array of Class objects. |
public static final char PACKAGE_SEPARATOR_CHAR
The package separator character: '.' == 46.
public static final java.lang.String PACKAGE_SEPARATOR
The package separator String: ".".
public static final char INNER_CLASS_SEPARATOR_CHAR
The inner class separator character: '$' == 36.
public static final java.lang.String INNER_CLASS_SEPARATOR
The inner class separator String: "$".
public static final java.lang.String STRING_EMPTY
Empty string.
public ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as
ClassUtils.getShortClassName(cls).
This constructor is public to permit tools that require a JavaBean instance to operate.
public static java.lang.String getShortClassName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the class name minus the package name for an Object.
object - the class to get the short name for, may be nullvalueIfNull - the value to return if nullpublic static java.lang.String getShortClassName(java.lang.Class<?> cls)
Gets the class name minus the package name from a Class.
Consider using the Java 5 API Class.getSimpleName() instead.
The one known difference is that this code will return "Map.Entry" while
the java.lang.Class variant will simply return "Entry".
cls - the class to get the short name for.public static java.lang.String getShortClassName(java.lang.String className)
Gets the class name minus the package name from a String.
The string passed in is assumed to be a class name - it is not checked.
Note that this method differs from Class.getSimpleName() in that this will
return "Map.Entry" whilst the java.lang.Class variant will simply
return "Entry".
className - the className to get the short name forpublic static java.lang.String getSimpleName(java.lang.Class<?> cls)
Null-safe version of aClass.getSimpleName()
cls - the class for which to get the simple name.Class.getSimpleName()public static java.lang.String getSimpleName(java.lang.Object object,
java.lang.String valueIfNull)
Null-safe version of aClass.getSimpleName()
object - the object for which to get the simple class name.valueIfNull - the value to return if object is nullClass.getSimpleName()public static java.lang.String getPackageName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the package name of an Object.
object - the class to get the package name for, may be nullvalueIfNull - the value to return if nullpublic static java.lang.String getPackageName(java.lang.Class<?> cls)
Gets the package name of a Class.
cls - the class to get the package name for, may be null.public static java.lang.String getPackageName(java.lang.String className)
Gets the package name from a String.
The string passed in is assumed to be a class name - it is not checked.
If the class is unpackaged, return an empty string.
className - the className to get the package name for, may be nullpublic static java.util.List<java.lang.Class<?>> getAllSuperclasses(java.lang.Class<?> cls)
Gets a List of superclasses for the given class.
cls - the class to look up, may be nullList of superclasses in order going up from this one
null if null inputpublic static java.util.List<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> cls)
Gets a List of all interfaces implemented by the given
class and its superclasses.
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.
cls - the class to look up, may be nullList of interfaces in order,
null if null inputpublic static java.util.List<java.lang.Class<?>> convertClassNamesToClasses(java.util.List<java.lang.String> classNames)
Given a List of class names, this method converts them into classes.
A new List is returned. If the class name cannot be found, null
is stored in the List. If the class name in the List is
null, null is stored in the output List.
classNames - the classNames to changeList of Class objects corresponding to the class names,
null if null inputjava.lang.ClassCastException - if classNames contains a non String entrypublic static java.util.List<java.lang.String> convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)
Given a List of Class objects, this method converts
them into class names.
A new List is returned. null objects will be copied into
the returned list as null.
classes - the classes to changeList of class names corresponding to the Class objects,
null if null inputjava.lang.ClassCastException - if classes contains a non-Class entrypublic static java.lang.Class<?> getClass(java.lang.ClassLoader classLoader,
java.lang.String className,
boolean initialize)
throws java.lang.ClassNotFoundException
className using the
classLoader. This implementation supports the syntaxes
"java.util.Map.Entry[]", "java.util.Map$Entry[]",
"[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".classLoader - the class loader to use to load the classclassName - the class nameinitialize - whether the class must be initializedclassName using the classLoaderjava.lang.ClassNotFoundException - if the class is not foundpublic static java.lang.Class<?> getClass(java.lang.ClassLoader classLoader,
java.lang.String className)
throws java.lang.ClassNotFoundException
className
using the classLoader. This implementation supports
the syntaxes "java.util.Map.Entry[]",
"java.util.Map$Entry[]", "[Ljava.util.Map.Entry;",
and "[Ljava.util.Map$Entry;".classLoader - the class loader to use to load the classclassName - the class nameclassName using the classLoaderjava.lang.ClassNotFoundException - if the class is not foundpublic static java.lang.Class<?> getClass(java.lang.String className)
throws java.lang.ClassNotFoundException
className
using the current thread's context class loader. This implementation
supports the syntaxes "java.util.Map.Entry[]",
"java.util.Map$Entry[]", "[Ljava.util.Map.Entry;",
and "[Ljava.util.Map$Entry;".className - the class nameclassName using the current thread's context class loaderjava.lang.ClassNotFoundException - if the class is not foundpublic static java.lang.Class<?> getClass(java.lang.String className,
boolean initialize)
throws java.lang.ClassNotFoundException
className using the
current thread's context class loader. This implementation supports the
syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]",
"[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".className - the class nameinitialize - whether the class must be initializedclassName using the current thread's context class loaderjava.lang.ClassNotFoundException - if the class is not foundpublic static java.lang.Class<?>[] toClass(java.lang.Object... array)
Converts an array of Object in to an array of Class objects.
If any of these objects is null, a null element will be inserted into the array.
This method returns null for a null input array.
array - an Object arrayClass array, null if null array inputpublic static java.lang.String deleteWhitespace(java.lang.String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char).
StringUtils.deleteWhitespace(null) = null
StringUtils.deleteWhitespace("") = ""
StringUtils.deleteWhitespace("abc") = "abc"
StringUtils.deleteWhitespace(" ab c ") = "abc"
str - the String to delete whitespace from, may be nullnull if null String inputpublic static boolean isEmpty(java.lang.CharSequence cs)
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or null