JRuntime

JRuntime

Infobox Software
name = JRuntime
developer = [http://www.helion-prime.com/ Helion-Prime Solutions Ltd.]
latest_release_version = 1.0
latest_release_date = release date|2008|06|24
operating_system = Cross-platform
programming_language = Java | genre = Library
license = GNU Lesser General Public License
website = [http://opensource.helion-prime.com/jruntime/ JRuntime Home Page]

JRuntime is the simple library for Java. It makes easier an access to Java 6 compiler API.
This library provide us with capability to compile, execute and modify Java-code dynamically during working of the program.

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 clazz = new MutableRuntimeClass(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

[http://opensource.helion-prime.com/jruntime/ JRuntime Home Page]
[http://java.sun.com/javase/6/docs/api/javax/tools/package-summary.html Java Compiler API]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”