Skip to content

Instantly share code, notes, and snippets.

@edefazio
Created January 11, 2017 05:00
Show Gist options
  • Save edefazio/df94351d0cadc66a8fbb152b3c5d7335 to your computer and use it in GitHub Desktop.
Save edefazio/df94351d0cadc66a8fbb152b3c5d7335 to your computer and use it in GitHub Desktop.
Illustrate the varcode Java API to Author, Compile, Use and Export Ad Hoc code at runtime
package quickstart;
import java.io.Serializable;
import java.util.UUID;
import varcode.java.Java;
import varcode.java.adhoc.Export;
import varcode.java.meta._class;
/**
*
* Simple things should be simple,
* complex things should be possible --Alan Kay
*/
public class APIInANutshell
{
public static void main( String[] args )
{
buildAuthorCompileUseAndExportAdHocCode();
loadModifyCompileUseAndExportAdHocCode();
}
public static void buildAuthorCompileUseAndExportAdHocCode()
{
//model a class
_class _adHoc = _class.of( "public class A" )
.method( "public String toString()",
"return \"Hello World\";" );
//write the source ("A.java") to a file
Export.dir("C:\\myprog\\src\\main\\java\\").file( _adHoc );
//compile the "A.java" to "A.class", & create a new instance
Object adHocA = _adHoc.instance( );
//write "A.class" bytecode to a file
Export.dir("C:\\myprog\\target\\classes\\").file( adHocA.getClass() );
//call a method on the adHoc instance (msg = "Hello World!")
String msg = (String)Java.call( adHocA, "toString" );
System.out.println( msg );
}
/**
* This Class will have it's source code:
* <UL>
* <LI> read in at runtime
* <LI> converted to an AST
* <LI> converted the AST into a _class (meta model)
* </UL>
*/
public class ExistingClass
implements Serializable
{
//we are just putting a field her which will be modelled
public String existing = "BLAH";
}
public static void loadModifyCompileUseAndExportAdHocCode()
{
// load the _class model of an existing class (even an inner class)
_class _cload = Java._classFrom( ExistingClass.class );
//change class name & package, add an import, field, & method
_cload.setName( "Tailored" )
.packageName( "ex.load.then.tailor" )
.field( "public static final int ID = 100;" )
.imports( UUID.class )
.method( "public String toString()",
"return existing + UUID.randomUUID().toString();" );
//write the modified "ex.load.then.tailor.Tailored.java" source
Export.TEMP_DIR.file( _cload );
// compile &
Object tailored = _cload.instance( );
System.out.println( tailored );
//write "ex.load.then.tailor.Tailored.class" bytecode to a file
Export.TEMP_DIR.file( tailored.getClass() );
}
}
@edefazio
Copy link
Author

buildAuthorCompileUseAndExportAdHocCode(), exports the File:

public class A
{

    public String toString(  )
    {
        return "Hello World";
    }
}

loadModifyCompileUseAndExportAdHocCode(), exports the File:

package ex.load.then.tailor;

import java.io.Serializable;
import java.util.UUID;
import varcode.java.Java;
import varcode.java.adhoc.Export;
import varcode.java.meta._class;


/**
 *  This Class will have it's source code:
 *  <UL>
 *   <LI> read in at runtime
 *   <LI> converted to an AST
 *   <LI> converted the AST into a _class (meta model)
 *  </UL>
 */
public class Tailored
    implements Serializable
{
    public static final int ID = 100;
    public String existing = "BLAH";

    public String toString(  )
    {
        return existing + UUID.randomUUID().toString();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment