ptolemy.backtrack.util
Class ClassFileLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by ptolemy.backtrack.util.ClassFileLoader

public class ClassFileLoader
extends java.net.URLClassLoader

Class loader that tries to load a class from the given file. This class loader, unlike other class loaders that accept class names and load classes with those names, accepts class file names and loads them as classes. It uses defineClass(java.lang.String, sun.misc.Resource) in URLClassLoader to define the class with the contents in the given file. If error occurs, it falls back to traditional class loading with the class name in the exception message.

Since:
Ptolemy II 5.1
Version:
$Id: ClassFileLoader.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Thomas Feng
Accepted Rating:
Red (tfeng)
Proposed Rating:
Red (tfeng)

Constructor Summary
ClassFileLoader()
          Construct a class loader with no special class path.
ClassFileLoader(java.lang.String[] classPaths)
          Construct a class loader with a set of class paths specified as a string array.
 
Method Summary
 java.lang.Class loadClass(java.io.File classFile)
          Load a class defined in a file, and return the Class object of the class.
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findClass, findResource, findResources, getPermissions, getURLs, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassFileLoader

public ClassFileLoader()
                throws java.net.MalformedURLException
Construct a class loader with no special class path. This class loader can only load Java built-in classes indirectly, when they are required by the class file to be loaded.

Throws:
java.net.MalformedURLException - If a string is not a proper URL.

ClassFileLoader

public ClassFileLoader(java.lang.String[] classPaths)
                throws java.net.MalformedURLException
Construct a class loader with a set of class paths specified as a string array. When the class file to be loaded requires other classes, this class loader loads those required classes with the given class paths.

Parameters:
classPaths - The array of class paths to be searched in order.
Throws:
java.net.MalformedURLException - If a string is not a proper URL.
Method Detail

loadClass

public java.lang.Class loadClass(java.io.File classFile)
                          throws java.io.FileNotFoundException,
                                 java.io.IOException,
                                 java.lang.LinkageError,
                                 java.lang.ClassNotFoundException
Load a class defined in a file, and return the Class object of the class.

Parameters:
classFile - The file that defines the class.
Returns:
The object of the loaded class.
Throws:
java.io.FileNotFoundException - If the file cannot be found.
java.io.IOException - If error occurs when trying to read the file.
java.lang.LinkageError - If URLClassLoader.defineClass(java.lang.String, sun.misc.Resource) issues a LinkageError and fails to define the class.
java.lang.ClassNotFoundException - If some classes referenced by the class in the file cannot be found.