org.springframework.util
Class ClassUtils

java.lang.Object
  extended byorg.springframework.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:
Keith Donald, Rob Harrop, Juergen Hoeller

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 Class forName(String name)
          Replacement for Class.forName() that also returns Class instances for primitives (like "int") and array class names (like "String[]").
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 uncaptilized short string name of a Java class.
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 or/and its superclasses at least have one or more methods (with any argument types)?
 
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)
                     throws ClassNotFoundException
Replacement for Class.forName() that also returns Class instances for primitives (like "int") and array class names (like "String[]").

Always uses the thread context class loader.

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

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

getShortNameAsProperty

public static String getShortNameAsProperty(Class clazz)
Return the uncaptilized short string name of a Java class.

Parameters:
clazz - the class
Returns:
the short name rendered in a standard JavaBeans property format

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

hasAtLeastOneMethodWithName

public static boolean hasAtLeastOneMethodWithName(Class clazz,
                                                  String methodName)
Does the given class or/and 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(String), Class.getResource(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(String), Class.getResource(String)


Copyright (C) 2003-2004 The Spring Framework Project.