org.apache.commons.lang

Class ClassUtils


public class ClassUtils
extends 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.

Since:
2.0

Field Summary

static String
INNER_CLASS_SEPARATOR
The inner class separator String: "$".
static char
INNER_CLASS_SEPARATOR_CHAR
The inner class separator character: '$' == .
static String
PACKAGE_SEPARATOR
The package separator String: ".".
static char
PACKAGE_SEPARATOR_CHAR
The package separator character: '.' == .

Constructor Summary

ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.

Method Summary

static List<E>
convertClassNamesToClasses(List<E> classNames)
Given a List of class names, this method converts them into classes.
static List<E>
convertClassesToClassNames(List<E> classes)
Given a List of Class objects, this method converts them into class names.
static List<E>
getAllInterfaces(Class<T> cls)
Gets a List of all interfaces implemented by the given class and its superclasses.
static List<E>
getAllSuperclasses(Class<T> cls)
Gets a List of superclasses for the given class.
static Class<T>
getClass(ClassLoader classLoader, String className)
Returns the (initialized) class represented by className using the classLoader.
static Class<T>
getClass(ClassLoader classLoader, String className, boolean initialize)
Returns the class represented by className using the classLoader.
static Class<T>
getClass(String className)
Returns the (initialized )class represented by className using the current thread's context class loader.
static Class<T>
getClass(String className, boolean initialize)
Returns the class represented by className using the current thread's context class loader.
static String
getPackageCanonicalName(Class<T> cls)
Gets the package name from the canonical name of a Class.
static String
getPackageCanonicalName(Object object, String valueIfNull)
Gets the package name from the canonical name of an Object.
static String
getPackageCanonicalName(String canonicalName)
Gets the package name from the canonical name.
static String
getPackageName(Class<T> cls)
Gets the package name of a Class.
static String
getPackageName(Object object, String valueIfNull)
Gets the package name of an Object.
static String
getPackageName(String className)
Gets the package name from a String.
static Method
getPublicMethod(Class<T> cls, String methodName, 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.
static String
getShortCanonicalName(Class<T> cls)
Gets the canonical name minus the package name from a Class.
static String
getShortCanonicalName(Object object, String valueIfNull)
Gets the canonical name minus the package name for an Object.
static String
getShortCanonicalName(String canonicalName)
Gets the canonical name minus the package name from a String.
static String
getShortClassName(Class<T> cls)
Gets the class name minus the package name from a Class.
static String
getShortClassName(Object object, String valueIfNull)
Gets the class name minus the package name for an Object.
static String
getShortClassName(String className)
Gets the class name minus the package name from a String.
static boolean
isAssignable(Class<T> cls, Class<T> toClass)
Checks if one Class can be assigned to a variable of another Class.
static boolean
isAssignable(Class<T>[] classArray, Class<T>[] toClassArray)
Checks if an array of Classes can be assigned to another array of Classes.
static boolean
isInnerClass(Class<T> cls)
Is the specified class an inner class or static nested class.
static Class<T>
primitiveToWrapper(Class<T> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.
static Class<T>[]
primitivesToWrappers(Class<T>[] classes)
Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class objects.
static Class<T>[]
toClass(Object[] array)
Converts an array of Object in to an array of Class objects.
static Class<T>
wrapperToPrimitive(Class<T> cls)
Converts the specified wrapper class to its corresponding primitive class.
static Class<T>[]
wrappersToPrimitives(Class<T>[] classes)
Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

INNER_CLASS_SEPARATOR

public static final String INNER_CLASS_SEPARATOR
The inner class separator String: "$".

INNER_CLASS_SEPARATOR_CHAR

public static final char INNER_CLASS_SEPARATOR_CHAR
The inner class separator character: '$' == .
Field Value:
'$'

PACKAGE_SEPARATOR

public static final String PACKAGE_SEPARATOR
The package separator String: ".".

PACKAGE_SEPARATOR_CHAR

public static final char PACKAGE_SEPARATOR_CHAR
The package separator character: '.' == .
Field Value:
'.'

Constructor Details

ClassUtils

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.

Method Details

convertClassNamesToClasses

public static List<E> convertClassNamesToClasses(List<E> 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:
classNames - the classNames to change
Returns:
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<E> convertClassesToClassNames(List<E> 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:
classes - the classes to change
Returns:
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<E> getAllInterfaces(Class<T> 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.

Parameters:
cls - the class to look up, may be null
Returns:
the List of interfaces in order, null if null input

getAllSuperclasses

public static List<E> getAllSuperclasses(Class<T> cls)
Gets a List of superclasses for the given class.
Parameters:
cls - the class to look up, may be null
Returns:
the List of superclasses in order going up from this one null if null input

getClass

public static Class<T> getClass(ClassLoader classLoader,
                             String className)
            throws ClassNotFoundException
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
className - the class name
Returns:
the class represented by className using the classLoader
Throws:
ClassNotFoundException - if the class is not found

getClass

public static Class<T> getClass(ClassLoader classLoader,
                             String className,
                             boolean initialize)
            throws ClassNotFoundException
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
className - the class name
initialize - whether the class must be initialized
Returns:
the class represented by className using the classLoader
Throws:
ClassNotFoundException - if the class is not found

getClass

public static Class<T> getClass(String className)
            throws ClassNotFoundException
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
Returns:
the class represented by className using the current thread's context class loader
Throws:
ClassNotFoundException - if the class is not found
Usages and Demos :

View More Examples of getClass(String className)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.apache.commons.lang.text.StrLookup;
   4:         ...
   5:     {
   6:         return ClassUtils.getClass(className);
   7:     }
   8: }

View Full Code Here
   1: import org.apache.commons.configuration.ConfigurationRuntimeException;
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:     {
   6:         return ClassUtils.getClass(name);
   7:     }
   8: 
   9:     private static Class fetchBeanClass(BeanDeclaration data,

View Full Code Here

getClass

public static Class<T> getClass(String className,
                             boolean initialize)
            throws ClassNotFoundException
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
initialize - whether the class must be initialized
Returns:
the class represented by className using the current thread's context class loader
Throws:
ClassNotFoundException - if the class is not found

getPackageCanonicalName

public static String getPackageCanonicalName(Class<T> cls)
Gets the package name from the canonical name of a Class.
Parameters:
cls - the class to get the package name for, may be null.
Returns:
the package name or an empty string
Since:
2.4

getPackageCanonicalName

public static String getPackageCanonicalName(Object object,
                                             String valueIfNull)
Gets the package name from the canonical name of an Object.
Parameters:
object - the class to get the package name for, may be null
valueIfNull - the value to return if null
Returns:
the package name of the object, or the null value
Since:
2.4

getPackageCanonicalName

public static String getPackageCanonicalName(String canonicalName)
Gets the package name from the canonical name.

The string passed in is assumed to be a canonical name - it is not checked.

If the class is unpackaged, return an empty string.

Parameters:
canonicalName - the canonical name to get the package name for, may be null
Returns:
the package name or an empty string
Since:
2.4

getPackageName

public static String getPackageName(Class<T> cls)
Gets the package name of a Class.
Parameters:
cls - the class to get the package name for, may be null.
Returns:
the package name or an empty string
Usages and Demos :

View More Examples of getPackageName(Class cls)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:         this.fullName = clazz.getName();
   6:         this.packageName = ClassUtils.getPackageName(clazz);
   7:         this.naturalName = GrailsClassUtils.getNaturalName(clazz.getName());
   8:         ...
   9:     private static String getShortClassname(Class clazz) {
  10:         return ClassUtils.getShortClassName(clazz);
  11:     }

View Full Code Here

getPackageName

public static String getPackageName(Object object,
                                    String valueIfNull)
Gets the package name of an Object.
Parameters:
object - the class to get the package name for, may be null
valueIfNull - the value to return if null
Returns:
the package name of the object, or the null value

getPackageName

public static String getPackageName(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.

Parameters:
className - the className to get the package name for, may be null
Returns:
the package name or an empty string
Usages and Demos :

View More Examples of getPackageName(String className)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:         this.fullName = clazz.getName();
   6:         this.packageName = ClassUtils.getPackageName(clazz);
   7:         this.naturalName = GrailsClassUtils.getNaturalName(clazz.getName());
   8:         ...
   9:     private static String getShortClassname(Class clazz) {
  10:         return ClassUtils.getShortClassName(clazz);
  11:     }

View Full Code Here

getPublicMethod

public static Method getPublicMethod(Class<T> cls,
                                     String methodName,
                                     parameterTypes[] )
            throws SecurityException,
                   NoSuchMethodException
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
methodName - the name of the method
Returns:
the method
Throws:
NullPointerException - if the class is null
SecurityException - if a a security violation occured
NoSuchMethodException - if the method is not found in the given class or if the metothod doen't conform with the requirements

getShortCanonicalName

public static String getShortCanonicalName(Class<T> cls)
Gets the canonical name minus the package name from a Class.
Parameters:
cls - the class to get the short name for.
Returns:
the canonical name without the package name or an empty string
Since:
2.4

getShortCanonicalName

public static String getShortCanonicalName(Object object,
                                           String valueIfNull)
Gets the canonical name minus the package name for an Object.
Parameters:
object - the class to get the short name for, may be null
valueIfNull - the value to return if null
Returns:
the canonical name of the object without the package name, or the null value
Since:
2.4

getShortCanonicalName

public static String getShortCanonicalName(String canonicalName)
Gets the canonical name minus the package name from a String.

The string passed in is assumed to be a canonical name - it is not checked.

Parameters:
canonicalName - the class name to get the short name for
Returns:
the canonical name of the class without the package name or an empty string
Since:
2.4

getShortClassName

public static String getShortClassName(Class<T> cls)
Gets the class name minus the package name from a Class.
Parameters:
cls - the class to get the short name for.
Returns:
the class name without the package name or an empty string
Usages and Demos :

View More Examples of getShortClassName(Class cls)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: 
   9:   public void execute(Result st) {

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.sblim.wbemsmt.tools.converter.StringArrayConverter;
   4:         ...
   5:     public String getTypeForModel() {
   6:         return ClassUtils.getShortClassName(Integer.class);
   7:     }    
   8:     
   9: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.sblim.wbem.cim.UnsignedInt16;
   4:         ...
   5:     public String getTypeForModel() {
   6:         return ClassUtils.getShortClassName(UnsignedInt16.class);
   7:     }    
   8:     
   9: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: 
   9:   public void setType(String ct) {

View Full Code Here

getShortClassName

public static String getShortClassName(Object object,
                                       String valueIfNull)
Gets the class name minus the package name for an Object.
Parameters:
object - the class to get the short name for, may be null
valueIfNull - the value to return if null
Returns:
the class name of the object without the package name, or the null value
Usages and Demos :

View More Examples of getShortClassName(Object object,String valueIfNull)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.crosswire.common.util.Logger;
   4: 
   5: public class DebugContainerListener implements ContainerListener

View Full Code Here

getShortClassName

public static String getShortClassName(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.

Parameters:
className - the className to get the short name for
Returns:
the class name of the class without the package name or an empty string
Usages and Demos :

View More Examples of getShortClassName(String className)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: 
   9:   public void execute(Result st) {

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.sblim.wbemsmt.tools.converter.StringArrayConverter;
   4:         ...
   5:     public String getTypeForModel() {
   6:         return ClassUtils.getShortClassName(Integer.class);
   7:     }    
   8:     
   9: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.sblim.wbem.cim.UnsignedInt16;
   4:         ...
   5:     public String getTypeForModel() {
   6:         return ClassUtils.getShortClassName(UnsignedInt16.class);
   7:     }    
   8:     
   9: }

View Full Code Here
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:   public String getName() {
   6:     return ClassUtils.getShortClassName(getClass());
   7:   }
   8: 
   9:   public void setType(String ct) {

View Full Code Here

isAssignable

public static boolean isAssignable(Class<T> cls,
                                   Class<T> 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:
cls - the Class to check, may be null
toClass - the Class to try to assign into, returns false if null
Returns:
true if assignment possible
Usages and Demos :

View More Examples of isAssignable(Class cls,Class toClass)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:         boolean result = false;
   6:         result = ClassUtils.isAssignable(cls, toClass);
   7:         return result;
   8:     }

View Full Code Here

isAssignable

public static boolean isAssignable(Class<T>[] classArray,
                                   Class<T>[] toClassArray)
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:
classArray - the array of Classes to check, may be null
toClassArray - the array of Classes to try to assign into, may be null
Returns:
true if assignment possible
Usages and Demos :

View More Examples of isAssignable(Class[] classArray,Class[] toClassArray)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: 
   4:         ...
   5:         boolean result = false;
   6:         result = ClassUtils.isAssignable(cls, toClass);
   7:         return result;
   8:     }

View Full Code Here

isInnerClass

public static boolean isInnerClass(Class<T> cls)
Is the specified class an inner class or static nested class.
Parameters:
cls - the class to check, may be null
Returns:
true if the class is an inner or static nested class, false if not or null

primitiveToWrapper

public static Class<T> primitiveToWrapper(Class<T> 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
Returns:
the wrapper class for cls or cls if cls is not a primitive. null if null input.
Since:
2.1
Usages and Demos :

View More Examples of primitiveToWrapper(Class cls)
   1: 
   2: import org.apache.commons.lang.ClassUtils;
   3: import org.apache.commons.lang.StringUtils;
   4:         ...
   5:                     if (arrayType.equals(cls)
   6:                             || (arrayType.isPrimitive() && cls.equals(ClassUtils.primitiveToWrapper(arrayType))))
   7:                     {
   8:         ...
   9:                 }
  10:                 else if (arrayType.equals(ClassUtils.primitiveToWrapper(cls)))
  11:                 {

View Full Code Here

primitivesToWrappers

public static Class<T>[] primitivesToWrappers(Class<T>[] 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
Returns:
an array which contains for each given class, the wrapper class or the original class if class is not a primitive. null if null input. Empty array if an empty array passed in.
Since:
2.1

toClass

public static Class<T>[] toClass(Object[] array)
Converts an array of Object in to an array of Class objects.

This method returns null for a null input array.

Parameters:
array - an Object array
Returns:
a Class array, null if null array input
Since:
2.4

wrapperToPrimitive

public static Class<T> wrapperToPrimitive(Class<T> cls)
Converts the specified wrapper class to its corresponding primitive class.

This method is the counter part of primitiveToWrapper(). If the passed in class is a wrapper class for a primitive type, this primitive type will be returned (e.g. Integer.TYPE for Integer.class). For other classes, or if the parameter is null, the return value is null.

Parameters:
cls - the class to convert, may be null
Returns:
the corresponding primitive type if cls is a wrapper class, null otherwise
Since:
2.4

wrappersToPrimitives

public static Class<T>[] wrappersToPrimitives(Class<T>[] classes)
Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.

This method invokes wrapperToPrimitive() for each element of the passed in array.

Parameters:
classes - the class array to convert, may be null or empty
Returns:
an array which contains for each given class, the primitive class or null if the original class is not a wrapper class. null if null input. Empty array if an empty array passed in.
Since:
2.4