Package ptolemy.util

Class ClassUtilities


  • public class ClassUtilities
    extends java.lang.Object
    A collection of utilities for manipulating classes. These utilities do not depend on any other ptolemy.* packages.
    Since:
    Ptolemy II 4.0
    Version:
    $Id$
    Author:
    Christopher Hylands
    Pt.AcceptedRating:
    Green (cxh)
    Pt.ProposedRating:
    Green (cxh)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.net.URL getResource​(java.lang.String spec)
      Get the resource.
      static java.util.List jarURLDirectories​(java.net.URL jarURL)
      Return the directories in a jar URI, relative to the jar entry, if any.
      static java.net.URL jarURLEntryResource​(java.lang.String jarURLString)
      Lookup a jar URL and return the resource.
      static java.lang.String lookupClassAsResource​(java.lang.String necessaryClass)
      Given a dot separated classname, return the jar file or directory where the class can be found.
      static java.net.URL sourceResource​(java.lang.String sourceURLString)
      Lookup a URL in the classpath, but search up the classpath for directories named src.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • jarURLDirectories

        public static java.util.List jarURLDirectories​(java.net.URL jarURL)
                                                throws java.io.IOException
        Return the directories in a jar URI, relative to the jar entry, if any. . Jar URLS have several forms, the most common being: jar:file:///foo/bar.jar/!/bif/baz, which means that the jar file /foo/bar.jar has a directory or file name bif/baz. If such a file is passed to this method, then any directories in the jar file directory bif/baz will be returned.
        Parameters:
        jarURL - The Jar URL for which we are to look for directories.
        Returns:
        An list of Strings that name the directories
        Throws:
        java.io.IOException - If opening the connection fails or if getting the jar file from the connection fails
      • jarURLEntryResource

        public static java.net.URL jarURLEntryResource​(java.lang.String jarURLString)
                                                throws java.io.IOException
        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.

        Parameters:
        jarURLString - The string containing the jar URL. If no resource is found and the string contains a "#" then the text consisting of the # and the remaining text is removed and the shorter string is used as a search pattern.
        Returns:
        The resource, if any. If the spec string does not contain !/, then return null.
        Throws:
        java.io.IOException - If this method cannot convert the specification to a URL.
        See Also:
        JarURLConnection
      • sourceResource

        public static java.net.URL sourceResource​(java.lang.String sourceURLString)
                                           throws java.io.IOException
        Lookup a URL in the classpath, but search up the classpath for directories named src. This method is useful for IDEs such as Eclipse where the default is to place .class files in a separate directory from the .java files.

        Before calling this method, call ClassLoader.getResource() because ClassLoader.getResource() is presumably faster.

        Parameters:
        sourceURLString - The string containing the URL.
        Returns:
        The resource, if any. If the spec string does not contain !/, then return null.
        Throws:
        java.io.IOException - If this method cannot convert the specification to a URL.
        See Also:
        JarURLConnection
      • getResource

        public static java.net.URL getResource​(java.lang.String spec)
        Get the resource. If the current thread has a non-null context class loader, then use it to get the resource. Othewise, get the NamedObj class and use that to get the resource. This is necessary because Thread.currentThread() can return null.
        Parameters:
        spec - The string to be found as a resource.
        Returns:
        The URL
      • lookupClassAsResource

        public static java.lang.String lookupClassAsResource​(java.lang.String necessaryClass)
        Given a dot separated classname, return the jar file or directory where the class can be found.
        Parameters:
        necessaryClass - The dot separated class name, for example "ptolemy.util.ClassUtilities"
        Returns:
        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.