org.compass.core.util
Class ClassUtils

java.lang.Object
  extended by org.compass.core.util.ClassUtils

public abstract class ClassUtils
extends Object

Miscellaneous class utility methods. Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of utilities.

Author:
kimchy

Field Summary
static String ARRAY_SUFFIX
          Suffix for array class names
 
Constructor Summary
ClassUtils()
           
 
Method Summary
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 String convertClassNameToResourcePath(String className)
          Convert a "."
static String convertResourcePathToClassName(String resourcePath)
          Convert a "/"-based resource path to a "."
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 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 getAllInterfacesForClassAsSet(Class clazz)
          Return all interfaces that the given class implements as Set, including ones implemented by superclasses.
static Constructor getDefaultConstructor(Class clazz)
          Returns the default constructor for the class.
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 String getQualifiedMethodName(Method method)
          Return the qualified name of the given method, consisting of fully qualified interface/class name + "." + method name.
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 String getShortNameForField(Field field)
           
static String getShortNameForMethod(Method method)
           
static Method getStaticMethod(Class clazz, String methodName, Class[] args)
          Return a static method of a class.
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)?
static boolean hasMethod(Class clazz, String methodName, Class[] paramTypes)
          Determine whether the given class has a method with the given signature.
static boolean isAbstractClass(Class clazz)
          Returns true of the class is an abstract class.
static boolean isPublic(Class clazz, Member member)
          Returns true if the member is public and the class is public.
static Class resolvePrimitiveClassName(String name)
          Resolve the given class name as primitive class, if appropriate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARRAY_SUFFIX

public static final String ARRAY_SUFFIX
Suffix for array class names

See Also:
Constant Field Values
Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

forName

public static Class forName(String name,
                            ClassLoader classLoader)
                     throws ClassNotFoundException
Replacement for Class.forName() that also returns Class instances for primitives (like "int") and array class names (like "String[]").

Parameters:
name - the name of the Class
classLoader - the class loader to use
Returns:
Class instance for the supplied name
Throws:
ClassNotFoundException
See Also:
Class.forName(java.lang.String), Thread.getContextClassLoader()

resolvePrimitiveClassName

public static Class resolvePrimitiveClassName(String name)
Resolve the given class name as primitive class, if appropriate.

Parameters:
name - the name of the potentially primitive class
Returns:
the primitive class, or null if the name does not denote a primitive class

getShortNameAsProperty

public static String getShortNameAsProperty(Class clazz)
Return the short string name of a Java class in decapitalized JavaBeans property format.

Parameters:
clazz - the class
Returns:
the short name rendered in a standard JavaBeans property format
See Also:
Introspector.decapitalize(String)

getShortNameForField

public static String getShortNameForField(Field field)

getShortNameForMethod

public static String getShortNameForMethod(Method method)

getShortName

public static String getShortName(Class clazz)
Get the class name without the qualified package name.

Parameters:
clazz - the class to get the short name for
Returns:
the class name of the class without the package name
Throws:
IllegalArgumentException - if the class is null

getShortName

public static String getShortName(String className)
Get the class name without the qualified package name.

Parameters:
className - the className to get the short name for
Returns:
the class name of the class without the package name
Throws:
IllegalArgumentException - if the className is empty

getQualifiedMethodName

public static String getQualifiedMethodName(Method method)
Return the qualified name of the given method, consisting of fully qualified interface/class name + "." + method name.

Parameters:
method - the method
Returns:
the qualified name of the method

hasMethod

public static boolean hasMethod(Class clazz,
                                String methodName,
                                Class[] paramTypes)
Determine whether the given class has a method with the given signature. Essentially translates NoSuchMethodException to "false".

Parameters:
clazz - the clazz to analyze
methodName - the name of the method
paramTypes - the parameter types of the method

getMethodCountForName

public 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. Includes non-public methods.

Parameters:
clazz - the clazz to check
methodName - the name of the method
Returns:
the number of methods with the given name

hasAtLeastOneMethodWithName

public 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.

Parameters:
clazz - the clazz to check
methodName - the name of the method
Returns:
whether there is at least one method with the given name

getStaticMethod

public static Method getStaticMethod(Class clazz,
                                     String methodName,
                                     Class[] args)
Return a static method of a class.

Parameters:
methodName - the static method name
clazz - the class which defines the method
args - the parameter types to the method
Returns:
the static method, or null if no static method was found
Throws:
IllegalArgumentException - if the method name is blank or the clazz is null

addResourcePathToPackagePath

public 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. Built by taking the package of the specified class file, converting all dots ('.') to slashes ('/'), adding a trailing slash if necesssary, and concatenating the specified resource name to this.
As such, this function may be used to build a path suitable for loading a resource file that is in the same package as a class file, although {link org.springframework.core.io.ClassPathResource} is usually even more convenient.

Parameters:
clazz - the Class whose package will be used as the base
resourceName - the resource name to append. A leading slash is optional.
Returns:
the built-up resource path
See Also:
ClassLoader.getResource(java.lang.String), Class.getResource(java.lang.String)

classPackageAsResourcePath

public 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 ('/'). Neither a leading nor trailing slash is added. The result could be concatenated with a slash and the name of a resource, and fed directly to ClassLoader.getResource(). For it to be fed to Class.getResource, a leading slash would also have to be prepended to the return value.

Parameters:
clazz - the input class. A null value or the default (empty) package will result in an empty string ("") being returned.
Returns:
a path which represents the package name
See Also:
ClassLoader.getResource(java.lang.String), Class.getResource(java.lang.String)

getAllInterfaces

public static Class[] getAllInterfaces(Object object)
Return all interfaces that the given object implements as array, including ones implemented by superclasses.

Parameters:
object - the object to analyse for interfaces
Returns:
all interfaces that the given object implements as array

getAllInterfacesForClass

public static Class[] getAllInterfacesForClass(Class clazz)
Return all interfaces that the given class implements as array, including ones implemented by superclasses.

Parameters:
clazz - the class to analyse for interfaces
Returns:
all interfaces that the given object implements as array

getAllInterfacesAsSet

public static Set getAllInterfacesAsSet(Object object)
Return all interfaces that the given object implements as List, including ones implemented by superclasses.

Parameters:
object - the object to analyse for interfaces
Returns:
all interfaces that the given object implements as List

getAllInterfacesForClassAsSet

public static Set getAllInterfacesForClassAsSet(Class clazz)
Return all interfaces that the given class implements as Set, including ones implemented by superclasses.

Parameters:
clazz - the class to analyse for interfaces
Returns:
all interfaces that the given object implements as Set

isPublic

public static boolean isPublic(Class clazz,
                               Member member)
Returns true if the member is public and the class is public.

Parameters:
clazz - the class to check for public
member - the member to check for public
Returns:
true if the member is public and the class is public

isAbstractClass

public static boolean isAbstractClass(Class clazz)
Returns true of the class is an abstract class. An abstract class is a either an abstract class or an interface.

Parameters:
clazz - the class to check for abstact
Returns:
true if the class is an abstract class

getDefaultConstructor

public static Constructor getDefaultConstructor(Class clazz)
Returns the default constructor for the class. If it is not visible, will set it to be accessible.

Parameters:
clazz - the class to get the default constructor
Returns:
the default constructor

convertResourcePathToClassName

public static String convertResourcePathToClassName(String resourcePath)
Convert a "/"-based resource path to a "."-based fully qualified class name.

Parameters:
resourcePath - the resource path pointing to a class
Returns:
the corresponding fully qualified class name

convertClassNameToResourcePath

public static String convertClassNameToResourcePath(String className)
Convert a "."-based fully qualified class name to a "/"-based resource path.

Parameters:
className - the fully qualified class name
Returns:
the corresponding resource path, pointing to the class


Copyright (c) 2004-2009 The Compass Project.