Software:JRuntime

From HandWiki
JRuntime
Developer(s)Helion-Prime Solutions Ltd.
Stable release
1.0 / June 24, 2008 (2008-06-24)
Written inJava
Operating systemCross-platform
TypeLibrary
LicenseGNU Lesser General Public License
WebsiteJRuntime Home Page

JRuntime is the simple library for Java. It makes easier an access to Java 6 compiler API.

This library enables compiling, executing and modifying Java-code dynamically while the program is working.

Usage

Dynamic class compilation

String classDef = 
	"public class MyClass {"+
	"	public String toString() {" +
	"		return \"MyClass\";" +
	"	}"+
	"}";

RuntimeClass runtimeClass = new RuntimeClass(classDef);
System.out.println(runtimeClass.newInstance());

Dynamic block execution

RuntimeBlock sum = new RuntimeBlock(
	"return (Integer)params[0]+(Integer)params[1];");

System.out.println(sum.invoke(1, 2));
System.out.println(sum.invoke(5, 100));

Dynamic class modification

public class ClassToModify {
	public String simpleMethod(String text) {
		System.out.println("Text: " + text);
		return text;
	}
}

...

MutableRuntimeClass<ClassToModify> clazz =
		new MutableRuntimeClass<ClassToModify>(ClassToModify.class);
ClassToModify instance = clazz.newInstance(new SimpleMutator() {

	@Override
	public void onBeforeMethod(Object instance, Method method,
			Object... params) {
		System.out.println("Before method: " + method.getName() + 
				", params: " + Arrays.toString(params));
	}

	@Override
	public Object onAfterMethod(Object instance, Method method,
			Object result, Object... params) {
		System.out.println("After method: " + method.getName() + 
				", params: " + Arrays.toString(params));
		return result;
	}
});

instance.simpleMethod("Test mutable class!!!");

External links