| java.lang.Object org.apache.commons.lang.ClassUtils
ClassUtils | public class ClassUtils (Code) | | Operates on classes without using reflection.
This class handles invalid null inputs as best it can.
Each method documents its behaviour in more detail.
author: Stephen Colebourne author: Gary Gregory author: Norm Deane author: Alban Peignier since: 2.0 version: $Id: ClassUtils.java 437554 2006-08-28 06:21:41Z bayard $ |
Constructor Summary | |
public | ClassUtils() ClassUtils instances should NOT be constructed in standard programming. |
Method Summary | |
public static List | convertClassNamesToClasses(List classNames) Given a List of class names, this method converts them into classes.
A new List is returned. | public static List | convertClassesToClassNames(List classes) Given a List of Class objects, this method converts
them into class names.
A new List is returned. | public static List | getAllInterfaces(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. | public static List | getAllSuperclasses(Class cls) | public static Class | getClass(ClassLoader classLoader, String className, boolean initialize) Returns the class represented by className using the
classLoader . | public static Class | getClass(ClassLoader classLoader, String className) Returns the (initialized) class represented by className
using the classLoader . | public static Class | getClass(String className) Returns the (initialized )class represented by className
using the current thread's context class loader. | public static Class | getClass(String className, boolean initialize) Returns the class represented by className using the
current thread's context class loader. | public static String | getPackageName(Object object, String valueIfNull) | public static String | getPackageName(Class cls) Gets the package name of a Class .
Parameters: cls - the class to get the package name for, may be null . | public static String | getPackageName(String className) | public static Method | getPublicMethod(Class cls, String methodName, Class parameterTypes) Returns the desired Method much like Class.getMethod , however
it ensures that the returned Method is from a public class or interface and not
from an anonymous inner class. | public static String | getShortClassName(Object object, String valueIfNull) | public static String | getShortClassName(Class cls) Gets the class name minus the package name from a Class .
Parameters: cls - the class to get the short name for. | public static String | getShortClassName(String className) | public static boolean | isAssignable(Class[] classArray, Class[] toClassArray) Checks if an array of Classes can be assigned to another array of Classes.
This method calls
ClassUtils.isAssignable(Class,Class) isAssignable for each
Class pair in the input arrays. | public static boolean | isAssignable(Class cls, Class toClass) Checks if one Class can be assigned to a variable of
another Class .
Unlike the
Class.isAssignableFrom(java.lang.Class) method,
this method takes into account widenings of primitive classes and
null s.
Primitive widenings allow an int to be assigned to a long, float or
double. | public static boolean | isInnerClass(Class cls) | public static Class | primitiveToWrapper(Class cls) Converts the specified primitive Class object to its corresponding
wrapper Class object.
NOTE: From v2.2, this method handles Void.TYPE ,
returning Void.TYPE .
Parameters: cls - the class to convert, may be null the wrapper class for cls or cls ifcls is not a primitive. | public static Class[] | primitivesToWrappers(Class[] classes) Converts the specified array of primitive Class objects to an array of
its corresponding wrapper Class objects.
Parameters: classes - the class array to convert, may be null or empty an array which contains for each given class, the wrapper class orthe original class if class is not a primitive. |
INNER_CLASS_SEPARATOR | final public static String INNER_CLASS_SEPARATOR(Code) | | The inner class separator String: "$" .
|
INNER_CLASS_SEPARATOR_CHAR | final public static char INNER_CLASS_SEPARATOR_CHAR(Code) | | The inner class separator character: '$' ==
.
|
PACKAGE_SEPARATOR | final public static String PACKAGE_SEPARATOR(Code) | | The package separator String: "." .
|
PACKAGE_SEPARATOR_CHAR | final public static char PACKAGE_SEPARATOR_CHAR(Code) | | The package separator character: '.' ==
.
|
ClassUtils | public ClassUtils()(Code) | | 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.
|
convertClassNamesToClasses | public static List convertClassNamesToClasses(List classNames)(Code) | | 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 .
Parameters: classNames - the classNames to change a List of Class objects corresponding to the class names,null if null input throws: ClassCastException - if classNames contains a non String entry |
convertClassesToClassNames | public static List convertClassesToClassNames(List classes)(Code) | | 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 .
Parameters: classes - the classes to change a List of class names corresponding to the Class objects,null if null input throws: ClassCastException - if classes contains a non-Class entry |
getAllInterfaces | public static List getAllInterfaces(Class cls)(Code) | | 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.
Parameters: cls - the class to look up, may be null the List of interfaces in order,null if null input |
getAllSuperclasses | public static List getAllSuperclasses(Class cls)(Code) | | Gets a List of superclasses for the given class.
Parameters: cls - the class to look up, may be null the List of superclasses in order going up from this onenull if null input |
getClass | public static Class getClass(ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException(Code) | | Returns the class represented by className using the
classLoader . This implementation supports names like
"java.lang.String[] " as well as "[Ljava.lang.String; ".
Parameters: classLoader - the class loader to use to load the class Parameters: className - the class name Parameters: initialize - whether the class must be initialized the class represented by className using the classLoader throws: ClassNotFoundException - if the class is not found |
getClass | public static Class getClass(ClassLoader classLoader, String className) throws ClassNotFoundException(Code) | | Returns the (initialized) class represented by className
using the classLoader . This implementation supports names
like "java.lang.String[] " as well as
"[Ljava.lang.String; ".
Parameters: classLoader - the class loader to use to load the class Parameters: className - the class name the class represented by className using the classLoader throws: ClassNotFoundException - if the class is not found |
getClass | public static Class getClass(String className) throws ClassNotFoundException(Code) | | Returns the (initialized )class represented by className
using the current thread's context class loader. This implementation
supports names like "java.lang.String[] " as well as
"[Ljava.lang.String; ".
Parameters: className - the class name the class represented by className using the current thread's context class loader throws: ClassNotFoundException - if the class is not found |
getClass | public static Class getClass(String className, boolean initialize) throws ClassNotFoundException(Code) | | Returns the class represented by className using the
current thread's context class loader. This implementation supports
names like "java.lang.String[] " as well as
"[Ljava.lang.String; ".
Parameters: className - the class name Parameters: initialize - whether the class must be initialized the class represented by className using the current thread's context class loader throws: ClassNotFoundException - if the class is not found |
getPackageName | public static String getPackageName(Object object, String valueIfNull)(Code) | | Gets the package name of an Object .
Parameters: object - the class to get the package name for, may be null Parameters: valueIfNull - the value to return if null the package name of the object, or the null value |
getPackageName | public static String getPackageName(Class cls)(Code) | | Gets the package name of a Class .
Parameters: cls - the class to get the package name for, may be null . the package name or an empty string |
getPackageName | public static String getPackageName(String className)(Code) | | 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.
Parameters: className - the className to get the package name for, may be null the package name or an empty string |
getPublicMethod | public static Method getPublicMethod(Class cls, String methodName, Class parameterTypes) throws SecurityException, NoSuchMethodException(Code) | | Returns the desired Method much like Class.getMethod , however
it ensures that the returned Method is from a public class or interface and not
from an anonymous inner class. This means that the Method is invokable and
doesn't fall foul of Java bug
4071957).
Set set = Collections.unmodifiableSet(...);
Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty", new Class[0]);
Object result = method.invoke(set, new Object[]);
Parameters: cls - the class to check, not null Parameters: methodName - the name of the method Parameters: parameterTypes - the list of parameters the method throws: NullPointerException - if the class is null throws: SecurityException - if a a security violation occured throws: NoSuchMethodException - if the method is not found in the given classor if the metothod doen't conform with the requirements |
getShortClassName | public static String getShortClassName(Object object, String valueIfNull)(Code) | | Gets the class name minus the package name for an Object .
Parameters: object - the class to get the short name for, may be null Parameters: valueIfNull - the value to return if null the class name of the object without the package name, or the null value |
getShortClassName | public static String getShortClassName(Class cls)(Code) | | Gets the class name minus the package name from a Class .
Parameters: cls - the class to get the short name for. the class name without the package name or an empty string |
getShortClassName | public static String getShortClassName(String className)(Code) | | 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.
Parameters: className - the className to get the short name for the class name of the class without the package name or an empty string |
isAssignable | public static boolean isAssignable(Class[] classArray, Class[] toClassArray)(Code) | | Checks if an array of Classes can be assigned to another array of Classes.
This method calls
ClassUtils.isAssignable(Class,Class) isAssignable for each
Class pair in the input arrays. It can be used to check if a set of arguments
(the first parameter) are suitably compatible with a set of method parameter types
(the second parameter).
Unlike the
Class.isAssignableFrom(java.lang.Class) method, this
method takes into account widenings of primitive classes and
null s.
Primitive widenings allow an int to be assigned to a long ,
float or double . This method returns the correct
result for these cases.
Null may be assigned to any reference type. This method will
return true if null is passed in and the toClass is
non-primitive.
Specifically, this method tests whether the type represented by the
specified Class parameter can be converted to the type
represented by this Class object via an identity conversion
widening primitive or widening reference conversion. See
The Java Language Specification,
sections 5.1.1, 5.1.2 and 5.1.4 for details.
Parameters: classArray - the array of Classes to check, may be null Parameters: toClassArray - the array of Classes to try to assign into, may be null true if assignment possible |
isAssignable | public static boolean isAssignable(Class cls, Class toClass)(Code) | | Checks if one Class can be assigned to a variable of
another Class .
Unlike the
Class.isAssignableFrom(java.lang.Class) method,
this method takes into account widenings of primitive classes and
null s.
Primitive widenings allow an int to be assigned to a long, float or
double. This method returns the correct result for these cases.
Null may be assigned to any reference type. This method
will return true if null is passed in and the
toClass is non-primitive.
Specifically, this method tests whether the type represented by the
specified Class parameter can be converted to the type
represented by this Class object via an identity conversion
widening primitive or widening reference conversion. See
The Java Language Specification,
sections 5.1.1, 5.1.2 and 5.1.4 for details.
Parameters: cls - the Class to check, may be null Parameters: toClass - the Class to try to assign into, returns false if null true if assignment possible |
isInnerClass | public static boolean isInnerClass(Class cls)(Code) | | Is the specified class an inner class or static nested class.
Parameters: cls - the class to check, may be null true if the class is an inner or static nested class,false if not or null |
primitiveToWrapper | public static Class primitiveToWrapper(Class cls)(Code) | | Converts the specified primitive Class object to its corresponding
wrapper Class object.
NOTE: From v2.2, this method handles Void.TYPE ,
returning Void.TYPE .
Parameters: cls - the class to convert, may be null the wrapper class for cls or cls ifcls is not a primitive. null if null input. since: 2.1 |
primitivesToWrappers | public static Class[] primitivesToWrappers(Class[] classes)(Code) | | Converts the specified array of primitive Class objects to an array of
its corresponding wrapper Class objects.
Parameters: classes - the class array to convert, may be null or empty an array which contains for each given class, the wrapper class orthe original class if class is not a primitive. null if null input.Empty array if an empty array passed in. since: 2.1 |
|
|