Last active
July 2, 2018 22:17
-
-
Save edefazio/f0870c9af3470356787faf56f0817c9a to your computer and use it in GitHub Desktop.
Example Demonstrating using _draft API to create compile. load. export, and use Java 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 example; | |
import draft.java.*; | |
import draft.java.file.*; | |
import draft.java.runtime.*; | |
import junit.framework.TestCase; | |
public class _draft_1 extends TestCase { | |
public void testOfString(){ | |
_class _c = _draft._class("package example.tutorial.draft1;", | |
"public class C{", | |
" public static final int ST = 103;", | |
" public static void main(String[] args){ System.out.println(\"Hello\"); }", | |
"}"); | |
assertEquals( "example.tutorial.draft1", _c.getPackageName() ); | |
assertEquals( "C", _c.getName() ); | |
_field _f = _c.getField("ST"); | |
assertTrue( _f.getModifiers().is("public static final")); | |
assertEquals("ST", _f.getName()); | |
assertEquals( AST.expr( "103"), _f.getInit() ); | |
_method _m = _c.getMethod("main"); | |
assertTrue( _m.getParam(0).getType().is(String[].class)); | |
_param _p = _m.getParam(0); | |
assertEquals( _param.of("String[] args"), _p); | |
assertEquals( _params.of("String[] args"), _m.getParams()); | |
assertEquals( AST.stmt("System.out.println(\"Hello\");"), _m.getStmt(0)); | |
_c.getMethod("main").getParam(0).setFinal(); | |
_c.getMethod("main").addCode("System.out.println(\"Bye\");"); | |
_c.remove("ST"); | |
_c.field("public int f=100;"); | |
_c.method("public int getF(){ return f;}"); | |
_draft._proxy(_c).main(); | |
_export.toRelativeDir(_c, "generated" ); | |
} | |
public void testClassViaBuilder() { | |
_class _c = _draft._class("example.tutorial.draft1.C2") | |
.field("public static final int ST = 103") | |
.main("System.out.println(\"Hello\");"); | |
dynamicClassInstanceProxy( _c ); | |
_export.toRelativeDir(_c, "generated" ); | |
} | |
public void testSourceOfRuntimeClass() { | |
_class _c = _draft._class(Inner.class) | |
.packageName("example.tutorial.draft1") | |
.setPublic() | |
.removeImports() | |
.setStatic(false); | |
_draft._proxy(_c).main(); | |
_export.toRelativeDir(_c, "generated"); | |
} | |
private static class Inner{ | |
public static void main(String[] args){ | |
System.out.println("Hello"); | |
} | |
} | |
public void testLocalClassWithAnnotations(){ | |
@__package("example.tutorial.draft1") | |
@__import({}) | |
@_public | |
class LocalAnno { | |
@_static | |
public void main(String[] args){ | |
System.out.println("Hello"); | |
} | |
} | |
_classFiles _cfs = _draft._javac( LocalAnno.class ); | |
_export.toRelativeDir( _cfs, "out/test/generated"); | |
} | |
public void dynamicClassInstanceProxy(_class _c ){ | |
_proxy _p1 = _draft._proxy(_c); | |
_c.field("public static final int VAL=10101;"); | |
_proxy _p2 = _draft._proxy(_c); | |
assertEquals(10101, _p2.get("VAL")); | |
assertTrue( !_p1.equals(_p2) ); | |
assertTrue( !_p1.instance.getClass().equals(_p2.instance.getClass()) ); | |
assertNotSame( _p1.instance.getClass().getClassLoader(), _p2.instance.getClass().getClassLoader() ); | |
_proxy _ap1 = _p1.proxyAnother(); | |
assertEquals( _ap1.instance.getClass(), _p1.instance.getClass()); | |
assertEquals( _ap1.instance.getClass().getClassLoader(), _p1.instance.getClass().getClassLoader() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
commented version