Created
July 2, 2018 23:23
-
-
Save edefazio/4a6ca1caf78e98fc169575e0f1e16399 to your computer and use it in GitHub Desktop.
Demonstrate using $macros to modify _draft models to automate boilerplate
This file contains 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._export; | |
import draft.java.macro.*; | |
import junit.framework.TestCase; | |
import java.util.*; | |
public class _draft_2_$macro extends TestCase { | |
public void testApply$MacroAfterLoading() { | |
@__package("example.tutorial.draft2") | |
@__import({}) | |
@_public | |
class L { | |
@_final | |
int x; | |
int y; | |
int z; | |
} | |
_class _cmac = _draft._class(L.class); | |
_cmac = $autoCtor.to( $setFluent.to( $get.to( _cmac ) ) ); | |
_export.toRelativeDir(_cmac, "generated" ); | |
} | |
public void testApply$macrosViaAnnotations(){ | |
@__package("example.tutorial.draft2") | |
@_public | |
@__import({}) | |
@_macro({$get.class, $setFluent.class, $autoCtor.class}) | |
class L2{ | |
@_final int x; | |
int y; | |
int z; | |
} | |
_class _cam = _draft._class(L2.class); | |
assertTrue(_cam.getCtor(0).getParam(0).getType().is(int.class)); | |
assertEquals( AST.stmt("this.x = x;"), _cam.getCtor(0).getStmt(0)); | |
assertTrue( _cam.getMethod("getX").getType().is(int.class)); | |
assertTrue( _cam.getMethod("getY").getType().is(int.class)); | |
assertTrue( _cam.getMethod("getZ").getType().is(int.class)); | |
assertTrue( _cam.getMethod("setY").getParam(0).getType().is(int.class)); | |
assertTrue( _cam.getMethod("setZ").getParam(0).getType().is(int.class)); | |
_export.toRelativeDir(_cam, "generated" ); | |
} | |
public void testCustom$Macro(){ | |
@__package("example.tutorial.draft2") | |
@__import({}) | |
@_macro( $serialVersionUID.class ) | |
@_public | |
class CM { | |
public int a; | |
public String name; | |
transient Map m; | |
} | |
_class _c = _draft._class(CM.class); | |
_field _f = _c.getField("serialVersionUID"); | |
assertTrue( _f.isStatic() ); | |
assertTrue( _f.isFinal() ); | |
assertTrue( _f.hasInit() ); | |
assertTrue( _f.getType().is(long.class) ); | |
_export.toRelativeDir(_c, "generated" ); | |
} | |
public static class $serialVersionUID { | |
public static _type to( _type _t ){ | |
if( _t instanceof _class){ | |
_class _c = (_class)_t; | |
List<_field> _fs = _c.listFields(f -> !f.isStatic() && !f.isTransient() ); | |
Set<_field> sfs = new HashSet<_field>(); | |
_fs.forEach(f -> sfs.add(_field.of(f.getType() + " " + f.getName()))); | |
long id = (0L + Objects.hashCode( _t.getFullName() )) << 32; | |
int fieldsHash = Objects.hashCode( sfs ); | |
id = id + fieldsHash; | |
_field _f = _field.of("public static final long serialVersionUID = "+id+"L"); | |
_c.addOrReplace(_f); | |
} | |
return _t; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
commented tutorial version