Back to index

wims  3.65+svn20090927
Static Public Member Functions | Private Member Functions
ptolemy.util.ClassUtilities Class Reference

A collection of utilities for manipulating classes. More...

List of all members.

Static Public Member Functions

static URL jarURLEntryResource (String jarURLString) throws IOException
 Lookup a jar URL and return the resource.
static String lookupClassAsResource (String necessaryClass)
 Given a dot separated classname, return the jar file or directory where the class can be found.

Private Member Functions

 ClassUtilities ()
 Instances of this class cannot be created.

Detailed Description

A collection of utilities for manipulating classes.

These utilities do not depend on any other ptolemy.* packages.

Christopher Hylands
Id:,v 1.24 2005/10/24 19:09:12 cxh Exp
Ptolemy II 4.0 .ProposedRating Green (cxh) .AcceptedRating Green (cxh)

Definition at line 48 of file

Constructor & Destructor Documentation

Instances of this class cannot be created.

Definition at line 51 of file


Member Function Documentation

static URL ptolemy.util.ClassUtilities.jarURLEntryResource ( String  jarURLString) throws IOException [inline, static]

Lookup a jar URL and return the resource.

A resource is a file such as a class file or image file that is found in the classpath. A jar URL is a URL that refers to a resource in a jar file. For example, file://./foo.jar!/a/b/c.class is a jar URL that refers to the a/b/c.class resource in foo.jar. If this method is called with file://./foo.jar!/a/b/c.class then it will return a/b/c.class if a/b/c.class can be found as a resource in the class loader that loaded this class (ptolemy.util.ClassUtilities). If the resource cannot be found, then an IOException is thrown. If the jarURLString parameter does not contain !/, then return null. Note that everything before the !/ is removed before searching the classpath.

This method is necessary because Web Start uses jar URL, and there are some cases where if we have a jar URL, then we may need to strip off the jar:url!/ part so that we can search for the {entry} as a resource.

jarURLStringThe string containing the jar URL.
The resource, if any.If the spec string does not contain !/, then return null.
IOExceptionIf this method cannot convert the specification to a URL.
See also:

Definition at line 86 of file

        // At first glance, it would appear that this method could appear
        // in specToURL(), but the problem is that specToURL() creates
        // a new URL with the spec, so it only does further checks if
        // the URL is malformed.  Unfortunately, in Web Start applications
        // the URL will often refer to a resource in another jar file,
        // which means that the jar url is not malformed, but there is
        // no resource by that name.  Probably specToURL() should return
        // the resource after calling new URL().
        int jarEntry = jarURLString.indexOf("!/");

        if (jarEntry == -1) {
            jarEntry = jarURLString.indexOf("!\\");

            if (jarEntry == -1) {
                return null;

        try {
            // !/ means that this could be in a jar file.
            String entry = jarURLString.substring(jarEntry + 2);

            // We might be in the Swing Event thread, so
            // Thread.currentThread().getContextClassLoader()
            // .getResource(entry) probably will not work.
            Class refClass = Class.forName("ptolemy.util.ClassUtilities");
            URL entryURL = refClass.getClassLoader().getResource(entry);
            return entryURL;
        } catch (Exception ex) {
            // IOException constructor does not take a cause, so we add it.
            IOException ioException = new IOException("Cannot find \""
                    + jarURLString + "\".");
            throw ioException;

Here is the caller graph for this function:

static String ptolemy.util.ClassUtilities.lookupClassAsResource ( String  necessaryClass) [inline, static]

Given a dot separated classname, return the jar file or directory where the class can be found.

necessaryClassThe dot separated class name, for example "ptolemy.util.ClassUtilities"
If the class can be found as a resource, return the directory or jar file where the necessary class can be found. otherwise, return null. If the resource is found in a directory, then the return value will always have forward slashes, it will never use backslashes.

Definition at line 135 of file

        // This method is called from copernicus.kernel.GeneratorAttribute
        // and actor.lib.python.PythonScript.  We moved it here
        // to avoid dependencies.
        String necessaryResource = StringUtilities.substitute(necessaryClass,
                ".", "/")
                + ".class";

        URL necessaryURL = Thread.currentThread().getContextClassLoader()

        if (necessaryURL != null) {
            String resourceResults = necessaryURL.getFile();

            // Strip off the file:/ and the necessaryResource.
            if (resourceResults.startsWith("file:/")) {
                resourceResults = resourceResults.substring(6);

            // Strip off the name of the resource we were looking for
            // so that we are left with the directory or jar file
            // it is in
            resourceResults = resourceResults.substring(0, resourceResults
                    - necessaryResource.length());

            // Strip off the trailing !/
            if (resourceResults.endsWith("!/")) {
                resourceResults = resourceResults.substring(0, resourceResults
                        .length() - 2);

            // Unfortunately, under Windows, URL.getFile() may
            // return things like /c:/ptII, so we create a new
            // File and get its path, which will return c:\ptII
            File resourceFile = new File(resourceResults);

            // Convert backslashes
            String sanitizedResourceName = StringUtilities.substitute(
                    resourceFile.getPath(), "\\", "/");
            return sanitizedResourceName;

        return null;

Here is the call graph for this function:

The documentation for this class was generated from the following file: