qflib 0.98.0

de.qfs.lib.util
Class DynamicClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--de.qfs.lib.util.DynamicClassLoader

public class DynamicClassLoader
extends java.lang.ClassLoader

A DynamicClassLoader is a valuable tool, particularly during the development phase of a program, since it can load new versions of a class into a running program without restarting it. There are a few restrictions though:

If a class file is older than the initialization time of the program, the standard system class will be used. If it is newer, a new DynamicClassLoader will be used to load and define the class, since any ClassLoader can hold only one class of the same name.

Other classes used by the newly created class will be resolved through the same DynamicClassLoader, but unless they have also been modified recently, their system class versions will be used. In this way it is possible to cast an object of a dynamically reinstantiated class to its system base class or interface and use it accordingly, i.e.

 JFrame frame = (JFrame) DynamicClassLoader.getObject("my.package.MySpecialFrame");
 
will work, while
 JFrame frame = (MySpecialFrame) DynamicClassLoader.getObject("my.package.MySpecialFrame");
 
will only work until the MySpecialFrame.class file is modified and a DynamicClassLoader is used to create a new version of the class, since the system class and the dynamic class are incompatible for assignment.

If you want to reload a class whose (direct or indirect) base class has been changed you have to recompile the class itself as well, otherwise its cached version is used, which refers to the old version of the base class.

Version:
$Revision: 1.11 $
Author:
Gregor Schmid

Field Summary
protected  java.util.Hashtable classes
          Cache for dynamically created classes.
protected static java.util.Vector classpath
          Files and ZipFiles for the classpath.
protected static java.io.File fileNotFound
          Special File to cache unknowns.
protected static java.util.Hashtable files
          Cache for files for class resources.
protected static long initTime
          Initialization time.
protected static java.util.Hashtable loadedClasses
          Cache for loaded classes.
protected static DynamicClassLoader primaryLoader
          The primary class loader that will be used to load classes that have changed for the first time after initialization.
protected static java.util.Hashtable systemClasses
          Cache for system classes.
 
Constructor Summary
DynamicClassLoader()
          Create a new DynamicClassLoader.
 
Method Summary
static java.lang.Object getDynamicObject(java.lang.String classname)
          Get an Object instance of a class.
static java.lang.Object getDynamicObject(java.lang.String classname, java.lang.Class[] types, java.lang.Object[] arguments)
          Get an Object instance of a class.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Find a resource in the classpath and open a stream for it.
static void initClasspath()
          Initialize the classpath.
protected static boolean isSystemClass(java.lang.String name)
          Check whether a class name refers to a system class.
 java.lang.Class loadClass(java.lang.String name)
          Load and resolve the class named name.
protected  java.lang.Class loadClass(java.lang.String name, boolean resolve)
          Load a class and maybe resolve it.
static java.lang.Class loadDynamicClass(java.lang.String name)
          Get a Class object for a class name.
protected  java.lang.Class makeSystemClass(java.lang.String name, boolean resolve)
          Get a system class and maybe resolve it.
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

systemClasses

protected static java.util.Hashtable systemClasses
Cache for system classes.

loadedClasses

protected static java.util.Hashtable loadedClasses
Cache for loaded classes.

files

protected static java.util.Hashtable files
Cache for files for class resources.

fileNotFound

protected static final java.io.File fileNotFound
Special File to cache unknowns.

classpath

protected static java.util.Vector classpath
Files and ZipFiles for the classpath.

initTime

protected static long initTime
Initialization time. Any non-system class file newer than this will be dynamically created.

primaryLoader

protected static DynamicClassLoader primaryLoader
The primary class loader that will be used to load classes that have changed for the first time after initialization. For every further change of the class file a new DynamicClassLoader will be used.

classes

protected java.util.Hashtable classes
Cache for dynamically created classes.
Constructor Detail

DynamicClassLoader

public DynamicClassLoader()
Create a new DynamicClassLoader.
Method Detail

initClasspath

public static void initClasspath()
Initialize the classpath. All elements of the classpath are looked up and either stored as Files or ZipFiles. This method is called automatically but you may have to call it again if you modify the java.class.path system property.

Since qflib 0.98.0 also handles the lib/ext directory of JRE 1.2 and above.


loadDynamicClass

public static java.lang.Class loadDynamicClass(java.lang.String name)
                                        throws java.lang.ClassNotFoundException
Get a Class object for a class name.
Parameters:
name - The fully qualified name of the class to get.
Returns:
The Class object.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.

getDynamicObject

public static java.lang.Object getDynamicObject(java.lang.String classname)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.InstantiationException,
                                                java.lang.IllegalAccessException
Get an Object instance of a class. The class must have a public zero argument constructor.
Parameters:
classname - The fully qualified class name.
Returns:
The new instance.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.
java.lang.InstantiationException - If the class cannot be instantiated.
java.lang.IllegalAccessException - If the caller doesn't have access to the class.

getDynamicObject

public static java.lang.Object getDynamicObject(java.lang.String classname,
                                                java.lang.Class[] types,
                                                java.lang.Object[] arguments)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.InstantiationException,
                                                java.lang.IllegalAccessException,
                                                java.lang.NoSuchMethodException,
                                                java.lang.reflect.InvocationTargetException
Get an Object instance of a class. The class must have a constructor matching arguments.
Parameters:
classname - The fully qualified class name.
types - The argument classes for the constructor.
arguments - The arguments for the constructor.
Returns:
The new instance.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.
java.lang.InstantiationException - If the class cannot be instantiated.
java.lang.IllegalAccessException - If the caller doesn't have access to the class.
java.lang.NoSuchMethodException - If no matching constructor exists.
java.lang.reflect.InvocationTargetException - If the constructor throws an exception.
Since:
0.96.0
See Also:
Class.getConstructor(Class[])

loadClass

public java.lang.Class loadClass(java.lang.String name)
                          throws java.lang.ClassNotFoundException
Load and resolve the class named name.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - The class name.
Returns:
The class.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Find a resource in the classpath and open a stream for it.
Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - The name of the resource.
Returns:
The stream.

loadClass

protected java.lang.Class loadClass(java.lang.String name,
                                    boolean resolve)
                             throws java.lang.ClassNotFoundException
Load a class and maybe resolve it.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - The class name.
resolve - Whether to resolve the class.
Returns:
The class.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.

isSystemClass

protected static boolean isSystemClass(java.lang.String name)
Check whether a class name refers to a system class.
Parameters:
name - The name to check.
Returns:
True if it is a system class.

makeSystemClass

protected java.lang.Class makeSystemClass(java.lang.String name,
                                          boolean resolve)
                                   throws java.lang.ClassNotFoundException
Get a system class and maybe resolve it.
Parameters:
name - The class name.
resolve - Whether to resolve the class.
Returns:
The class.
Throws:
java.lang.ClassNotFoundException - If no such class exists or some IO problem occurs.

qflib 0.98.0