Software:JRuntime
From HandWiki
Developer(s) | Helion-Prime Solutions Ltd. |
---|---|
Stable release | 1.0
/ June 24, 2008 |
Written in | Java |
Operating system | Cross-platform |
Type | Library |
License | GNU Lesser General Public License |
Website | JRuntime 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