|
Operates on classes without using reflection. This class handles invalid null inputs as best it can. Each method documents its behavior in more detail. Field Detailpublic static final char PACKAGE_SEPARATOR_CHAR The package separator character: .. See Also: Constant Field Values 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: $. See Also: Constant Field Values public static final java.lang.String INNER_CLASS_SEPARATOR The inner class separator String: $. Constructor Detailpublic 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. Method Detail
Gets the class name minus the package name for an Object. Parameters: objectthe class to get the short name for, may be null valueIfNullthe value to return if null Returns: the class name of the object without the package name, or the null value public static java.lang.String getShortClassName(java.lang.Class cls) Gets the class name minus the package name from a Class. Parameters: clsthe class to get the short name for, must not be null Returns: the class name without the package name Throws: java.lang.IllegalArgumentExceptionif the class is null 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 nameit is not checked. Parameters: classNamethe className to get the short name for, must not be empty or null Returns: the class name of the class without the package name Throws: java.lang.IllegalArgumentExceptionif the className is empty
Gets the package name of an Object. Parameters: objectthe class to get the package name for, may be null valueIfNullthe value to return if null Returns: the package name of the object, or the null value public static java.lang.String getPackageName(java.lang.Class cls) Gets the package name of a Class. Parameters: clsthe class to get the package name for, must not be null Returns: the package name Throws: java.lang.IllegalArgumentExceptionif the class is 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 nameit is not checked. Parameters: classNamethe className to get the package name for, must not be empty or null Returns: the package name Throws: java.lang.IllegalArgumentExceptionif the className is empty public static java.util.List getAllSuperclasses(java.lang.Class cls) Gets a List of superclasses for the given class. Parameters: clsthe class to look up, must not be null Returns: the List of superclasses in order going up from this one, null if null input public static java.util.List 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 hieracrchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained. Parameters: clsthe class to look up, must not be null Returns: the List of interfaces in order, null if null input public static java.util.List convertClassNamesToClasses(java.util.List 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. Parameters: classNamesthe classNames to change Returns: a List of Class objects corresponding to the class names, null if null input Throws: java.lang.ClassCastExceptionif classNames contains a non String entry public static java.util.List convertClassesToClassNames(java.util.List 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. Parameters: classesthe classes to change Returns: a List of Class objects corresponding to the class names, null if null input Throws: java.lang.ClassCastExceptionif classNames contains a non Class or null enTRy
Checks if an array of Classes can be assigned to another array of Classes. This method calls 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 nulls. 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: classArraythe array of Classes to check, may be null toClassArraythe array of Classes to try to assign into, may be null Returns: true if assignment possible public static boolean isAssignable(java.lang.Class cls, java.lang.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 nulls. 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: clsthe Class to check, may be null toClassthe Class to try to assign into, returns false if null Returns: true if assignment possible public static boolean isInnerClass(java.lang.Class cls) Is the specified class an inner class or static nested class. Parameters: clsthe class to check Returns: true if the class is an inner or static nested class, false if not or null |
|