org.springframework.util
Class ClassLoaderUtils


public abstract class ClassLoaderUtils

Utility class for diagnostic purposes, to analyze the ClassLoader hierarchy for any object.

Author:
Rod Johnson
Since: 02 April 2001
See Also: ClassLoader

Method Summary
 static StringaddResourcePathToPackagePath(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 StringclassNameWithoutPackagePrefix(Class clazz)
          Given a fully qualified name, return a class name: e.g.
 static StringclassPackageAsResourcePath(Class clazz)
          Given an input class object, returns a string which consists of the class's package name as a pathname, i.e., all dots ('.') are replaced by slashes ('/').
 static StringshowClassLoaderHierarchy(Object obj, String role, String delim, String tabText)
          Show the class loader hierarchy for this class.
 static StringshowClassLoaderHierarchy(ClassLoader cl, String delim, String tabText, int indent)
          Show the class loader hierarchy for this class.

Method Detail

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.
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:
java.lang.ClassLoader.getResource(java.lang.String), java.lang.Class.getResource(java.lang.String)

classNameWithoutPackagePrefix

public static String classNameWithoutPackagePrefix(Class clazz)
Given a fully qualified name, return a class name: e.g. com.foo.Bar returns Bar, com.foo.Bar$Inner returns Bar$Inner.
Parameters:
clazz - class to find name for
Returns: the class name without the package prefix. Will include any enclosing class name, which is also after the dot.

classPackageAsResourcePath

public static String classPackageAsResourcePath(Class clazz)
Given an input class object, returns 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:
java.lang.ClassLoader.getResource(java.lang.String), java.lang.Class.getResource(java.lang.String)

showClassLoaderHierarchy

public static String showClassLoaderHierarchy(Object obj, String role, String delim, String tabText)
Show the class loader hierarchy for this class.
Parameters:
obj - object to analyze loader hierarchy for
role - a description of the role of this class in the application (e.g., "servlet" or "EJB reference")
delim - line break
tabText - text to use to set tabs
Returns: a String showing the class loader hierarchy for this class

showClassLoaderHierarchy

public static String showClassLoaderHierarchy(ClassLoader cl, String delim, String tabText, int indent)
Show the class loader hierarchy for this class.
Parameters:
cl - class loader to analyze hierarchy for
delim - line break
tabText - text to use to set tabs
indent - nesting level (from 0) of this loader; used in pretty printing
Returns: a String showing the class loader hierarchy for this class