edu.rit.classfile
Class DirectClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by edu.rit.classfile.DirectClassLoader

public class DirectClassLoader
extends ClassLoader

Class DirectClassLoader provides a class loader into which you can directly write classfiles. To write a classfile into a DirectClassLoader:

  1. Call the writeClass() method to get an output stream for the classfile, specifying the name of the class, and optionally specifying a protection domain for the class.

  2. Write the bytes of the desired classfile to the output stream. For example, you can pass the output stream to a SynthesizedClassDescription's emit() method.

  3. Close the output stream.

When the output stream is closed, the sequence of bytes that was written is recorded as the classfile for the given class name. Thereafter, if the DirectClassLoader is told to load a class with that name, it will use the written sequence of bytes as the classfile, and it will use the protection domain specified for the class if any.


Constructor Summary
DirectClassLoader()
          Construct a new direct class loader with the system class loader as the parent class loader.
DirectClassLoader(ClassLoader theParent)
          Construct a new direct class loader with the given class loader as the parent class loader.
 
Method Summary
protected  Class findClass(String theClassName)
          Finds the class with the given name in this direct class loader.
 OutputStream writeClass(String theClassName)
          Write a class with the given name and the default protection domain into this direct class loader.
 OutputStream writeClass(String theClassName, ProtectionDomain theProtectionDomain)
          Write a class with the given name and the given protection domain into this direct class loader.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findResources, 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

DirectClassLoader

public DirectClassLoader()
Construct a new direct class loader with the system class loader as the parent class loader. Calling this constructor is equivalent to calling DirectClassLoader(ClassLoader.getSystemClassLoader()).

Throws:
SecurityException - (unchecked exception) Thrown if there is a security manager and its checkCreateClassLoader() method doesn't allow creation of a new class loader.

DirectClassLoader

public DirectClassLoader(ClassLoader theParent)
Construct a new direct class loader with the given class loader as the parent class loader.

Parameters:
theParent - Parent class loader for delegation.
Throws:
SecurityException - (unchecked exception) Thrown if there is a security manager and its checkCreateClassLoader() method doesn't allow creation of a new class loader.
Method Detail

writeClass

public OutputStream writeClass(String theClassName)
Write a class with the given name and the default protection domain into this direct class loader. An output stream for writing the classfile bytes is returned. When the output stream is closed, the sequence of bytes that was written is recorded as the classfile for the given class name. If a class with the given name has already been written into this direct class loader, null is returned to prevent overwriting it.

Parameters:
theClassName - Fully-qualified class name.
Returns:
Output stream for writing the classfile bytes, or null if a class with the given name has already been written.
Throws:
NullPointerException - (unchecked exception) Thrown if theClassName is null.
IllegalArgumentException - (unchecked exception) Thrown if theClassName is zero length.

writeClass

public OutputStream writeClass(String theClassName,
                               ProtectionDomain theProtectionDomain)
Write a class with the given name and the given protection domain into this direct class loader. An output stream for writing the classfile bytes is returned. When the output stream is closed, the sequence of bytes that was written is recorded as the classfile for the given class name. If a class with the given name has already been written into this direct class loader, null is returned to prevent overwriting it.

Parameters:
theClassName - Fully-qualified class name.
theProtectionDomain - Protection domain, or null to use the default protection domain.
Returns:
Output stream for writing the classfile bytes, or null if a class with the given name has already been written.
Throws:
NullPointerException - (unchecked exception) Thrown if theClassName is null.
IllegalArgumentException - (unchecked exception) Thrown if theClassName is zero length.

findClass

protected Class findClass(String theClassName)
                   throws ClassNotFoundException
Finds the class with the given name in this direct class loader.

Overrides:
findClass in class ClassLoader
Parameters:
theClassName - Fully-qualified class name.
Returns:
Class with the given name.
Throws:
ClassNotFoundException - Thrown if no class named theClassName has been written into this direct class loader.


Copyright © 2001-2006 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.