org.cojen.util
Class ClassInjector

java.lang.Object
  extended by org.cojen.util.ClassInjector

public class ClassInjector
extends Object

ClassInjector allows transient classes to be loaded, where a transient class is defined as being dynamically created at runtime. Unless explicit, the name given to transient classes is randomly assigned to prevent name collisions and to discourage referencing the classname persistently outside the runtime environment.

Classes defined by ClassInjector may be unloaded, if no references to it exist. Once unloaded, they cannot be loaded again by name since the original bytecode was never preserved.

Debugging can be enabled via the java command-line option "-Dcojen.util.ClassInjector.DEBUG=true". This causes all generated classes to be written to the temp directory, and a message is written to System.out indicating exactly where.

Author:
Brian S O'Neill

Method Summary
static ClassInjector create()
          Create a ClassInjector for defining one class.
static ClassInjector create(String prefix, ClassLoader parent)
          Create a ClassInjector for defining one class.
static ClassInjector createExplicit(String name, ClassLoader parent)
          Create a ClassInjector for defining one class with an explicit name.
 Class defineClass(ClassFile cf)
          Define the new class from a ClassFile object.
 String getClassName()
          Returns the name that must be given to the new class.
 Class getNewClass()
          Returns the newly defined class.
 OutputStream openStream()
          Open a stream to define the new class into.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static ClassInjector create()
Create a ClassInjector for defining one class. The parent ClassLoader used is the one which loaded the ClassInjector class.


create

public static ClassInjector create(String prefix,
                                   ClassLoader parent)
Create a ClassInjector for defining one class. The prefix is optional, which is used as the start of the auto-generated class name. If the parent ClassLoader is not specified, it will default to the ClassLoader of the ClassInjector class.

If the parent loader was used for loading injected classes, the new class will be loaded by it. This allows auto-generated classes access to package accessible members, as long as they are defined in the same package.

Parameters:
prefix - optional class name prefix
parent - optional parent ClassLoader

createExplicit

public static ClassInjector createExplicit(String name,
                                           ClassLoader parent)
Create a ClassInjector for defining one class with an explicit name. If the parent ClassLoader is not specified, it will default to the ClassLoader of the ClassInjector class.

If the parent loader was used for loading injected classes, the new class will be loaded by it. This allows auto-generated classes access to package accessible members, as long as they are defined in the same package.

Parameters:
name - required class name
parent - optional parent ClassLoader
Throws:
IllegalArgumentException - if name is null

getClassName

public String getClassName()
Returns the name that must be given to the new class.


openStream

public OutputStream openStream()
                        throws IllegalStateException
Open a stream to define the new class into.

Throws:
IllegalStateException - if new class has already been defined or if a stream has already been opened

defineClass

public Class defineClass(ClassFile cf)
Define the new class from a ClassFile object.

Returns:
the newly created class
Throws:
IllegalStateException - if new class has already been defined or if a stream has already been opened

getNewClass

public Class getNewClass()
                  throws IllegalStateException,
                         ClassFormatError
Returns the newly defined class.

Throws:
IllegalStateException - if class was never defined
ClassFormatError


Copyright © 2004-2011 Brian S O'Neill. All Rights Reserved.