Skip to content

Generate, compile and run .java source dynamically at runtime

Notifications You must be signed in to change notification settings

edefazio/varcode

Repository files navigation

combines a **code generator** and **ad-hoc tools** to **compile, load, and run .java source code at runtime**.

generate / run / export .java code at runtime

//1 generate a model
_class _model = _class.of( "package mymodel;", 
    "public class Model" )
    .imports( UUID.class )
    .method( "public String createId()",
        "return UUID.randomUUID().toString();" );
// 2) create instance        
Object dynamicModel = _model.instance();        

// 3) call a method on the dynamic instance
String id1 = (String)Java.call( dynamicModel, "createId" ); 

// 4) export .java & .class files:
//    export "C:\MyApp\src\main\java\mymodel\Model.java"
Export.dir( "C:\\MyApp\\src\\main\\java\\").toFile( _model );
//    export "C:\MyApp\traget\classes\mymodel\Model.class"
Export.dir( "C:\\MyApp\\target\\classes\\").toFile( dynamicModel.getClass() );

...Export( "C:\\MyApp\\src\\main\\java\\").toFile( _model ); will create the file C:\\myapp\\src\\main\\java\\mymodel\\Model.java containing:

package mymodel;
import java.util.UUID;

public class Model
{
    public String createId(  )
    {
        return UUID.randomUUID().toString();
    }
}

metaprogramming : read in existing code, modify it & run it

varcode makes metaprogramming easy. load a ( _class, _enum, _interface, _annotationType ) from an existing class, modify it at the source level, then compile & use it at runtime (no restarting required).

// 1. build the _class model from the .java source of the Class
_class _c = Java._classFrom( OriginalClass.class ); 

// 2. modify the model
_c.setName("Tailored");// change the class Name
_c.field("private static final int ID = 100;"); // add field

// get and modify the "toString" method
_c.getMethod("toString").body( "return getClass().getSimpleName() + ID;")

// 3. compile, instantiate and use it
Object tailored = _c.instance(); // create a new instance of "Tailored"
System.out.println( tailored );  //prints "Tailored100"

construct classes step by step

classes for ( _class, _interface, _enum, _annotationType ) can be built in a single compound statement or incrementally using simple mutator methods.

_class _c = _class.of( "package ex.mutable;",
    _imports.of( Serializable.class ),
    "@Deprecated",     
    "public class MyMutableModel implements Serializable" );
    
_method _m = _method.of( "/** create a random number */", 
    "@Generated",
    "public static final double random()", 
    "return Math.random();" );
    
_c.method( _m );   //add the "random" method to the _class  
    
_field _f = _field.of( "/** field javadoc */", 
    "public static int ID = 100;"); 
    
_c.add( _f ); //add the "ID" field to the _class

About

Generate, compile and run .java source dynamically at runtime

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages