Dynamic class reloading and object state migration for the JVM
HotSwap provides a robust library for updating the implementation of an object at runtime, otherwise known as hotswapping. This is achieved through recompilation, dynamic class reloading, and object state migration throughout the life of an application. It is fundamentally useful for runtime incremental development of an application.
// Assume you have an interface 'Transformer' in your classpath
// in src/main/java. It has a single method 'Object transform(Object)'.
// Further assume that 'MatrixTransformer' is an implementation of
// Transformer in src/test/java.
// Create a compiler instance which acts as a factory for
// ProxyClass objects.
ProxyCompiler compiler = new KJavacCompiler();
compiler.setClasspath("target/classes");
compiler.setSourcepath("src/test/java");
compiler.setDestinationpath("target/hotswap");
// Create a ProxyClass. A monitor is setup that watches for
// source files changes in the file.
ProxyClass cls = compiler.load("org.example.MatrixTransformer");
// Create a Proxy object that implements Transformer
Transformer t = (Transformer)cls.newInstance();
// Before the transform method is actually invoked, a check is performed
// to see if the source file has changed. If so, the class is
// recompiled and the object instance is swapped out. The result
// object will have been generated by the updated code. Cool!
Object input = ...
Object output = t.transform(input);
MIT
WHen it was originally written in 2001, the reqs were JDK1.2 for basic
use, JDK1.3 for use of Dynamic
Proxy Classes. With the ProxyClassWatcher
implementation, JDK
1.7 is required (java.nio.file.WatchService
). If necessary, a
custom alternative ProxyClassMonitor
implementation can be provided,
removing the JDK1.7 requirement.
Maven central
Include the hotswap.jar
in your classpath. You also need
to include $JAVA_HOME/lib/tools.jar
in the classpath in
order to use KJavacCompiler
.
The manual is the org.inxar.hotswap.*
package
description apidocs.