Created
January 11, 2017 05:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buildAuthorCompileUseAndExportAdHocCode()
, exports the File:loadModifyCompileUseAndExportAdHocCode()
, exports the File: