|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.ClassLoader | +--de.qfs.lib.util.DynamicClassLoader
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.
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 |
protected static java.util.Hashtable systemClasses
protected static java.util.Hashtable loadedClasses
protected static java.util.Hashtable files
protected static final java.io.File fileNotFound
protected static java.util.Vector classpath
protected static long initTime
protected static DynamicClassLoader primaryLoader
protected java.util.Hashtable classes
Constructor Detail |
public DynamicClassLoader()
Method Detail |
public static void initClasspath()
java.class.path
system property. Since qflib 0.98.0 also handles the lib/ext directory of JRE 1.2 and above.
public static java.lang.Class loadDynamicClass(java.lang.String name) throws java.lang.ClassNotFoundException
name
- The fully qualified name of the class to get.java.lang.ClassNotFoundException
- If no such class exists or some
IO problem occurs.public static java.lang.Object getDynamicObject(java.lang.String classname) throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException
classname
- The fully qualified class name.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.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
classname
- The fully qualified class name.types
- The argument classes for the constructor.arguments
- The arguments for the constructor.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.Class.getConstructor(Class[])
public java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
name
- The class name.java.lang.ClassNotFoundException
- If no such class exists or some
IO problem occurs.public java.io.InputStream getResourceAsStream(java.lang.String name)
getResourceAsStream
in class java.lang.ClassLoader
name
- The name of the resource.protected java.lang.Class loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
name
- The class name.resolve
- Whether to resolve the class.java.lang.ClassNotFoundException
- If no such class exists or some
IO problem occurs.protected static boolean isSystemClass(java.lang.String name)
name
- The name to check.protected java.lang.Class makeSystemClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
name
- The class name.resolve
- Whether to resolve the class.java.lang.ClassNotFoundException
- If no such class exists or some
IO problem occurs.
|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |