design patterns - Automatic generation of immutable class and matching builder class of a Java interface -
design patterns - Automatic generation of immutable class and matching builder class of a Java interface -
what tools or libraries exists java take interface
accessor method definitions , automatically generate immutable object class , "builder" class incrementally building new instances or changing existing instances creating new ones?
example input:
public interface auto { string getmodelname(); int getwheelcount(); }
example output:
import javax.annotation.concurrent.immutable; import javax.annotation.concurrent.notthreadsafe; @immutable public final class immutablecar implements auto { @notthreadsafe public static final class builder implements auto { private string modelname; private int wheelcount; public builder() { } public builder(final auto car) { modelname = car.getmodelname(); wheelcount = car.getwheelcount(); } public immutablecar build() { homecoming new immutablecar(wheelcount, modelname); } @override public string getmodelname() { homecoming modelname; } @override public int getwheelcount() { homecoming wheelcount; } public void setmodelname(final string modelname) { this.modelname = modelname; } public void setwheelcount(final int wheelcount) { this.wheelcount = wheelcount; } } private final string modelname; private final int wheelcount; public immutablecar(final int wheelcount, final string modelname) { this.wheelcount = wheelcount; this.modelname = modelname; } @override public string getmodelname() { homecoming modelname; } @override public int getwheelcount() { homecoming wheelcount; } }
check out eclipse model2text project , subprojects, acceleo , xpand. used generate emf-based java code emf models can used generate simple pojos too.
however functionality not come out of box: you'd have create own code generator , templates it. see accelelo tutorial .
edit:
one more thought - 1 simple took me day realize it
you can utilize velocity, freemarker or similar template library (which used html generation). though still need create model somewhere, in .txt or .xml file example. here's tutorial on velocity code generation.
java design-patterns immutability builder-pattern
Comments
Post a Comment