oliviercoma / fluent-builders-generator-eclipse-plugin

Automatically exported from code.google.com/p/fluent-builders-generator-eclipse-plugin
0 stars 0 forks source link

Support immutable object builders as static inner classes (as proposed by Josh Bloch) #15

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is a request for an enhancement to your plugin to support Java builders as 
static inner classes as proposed by Josh Bloch in Effective Java (item #2 and 
here: http://drdobbs.com/java/208403883?pgno=2 ).

This pattern enforces immutability on the object being built, which is 
important in a multi-threaded environment. Encapsulating the builder inside the 
class that it builds also facilitates code maintenance. 

Original issue reported on code.google.com by cdore...@gmail.com on 16 Apr 2011 at 3:24

GoogleCodeExporter commented 8 years ago

Original comment by kube...@gmail.com on 16 Apr 2011 at 8:20

GoogleCodeExporter commented 8 years ago
You are right, this pattern is really nice. It avoids having setters, and 
setters are evil :) However, in real life setters are hard to avoid. This 
plug-in was created to aid unit testing the contract-first web services with 
large DTO structures. Also with the dependency injection frameworks you are 
more likely to use setters or factory methods than inner builders.

On the other hand, generating the inner builders on compile time with APT is 
very compelling. You'd just have to annotate the classes to generate builders 
for. The builders would be generated every time you compile the code, so they'd 
never go out of date. Also, the builders could use each other to generate whole 
structures of objects.

If you just need a simple inner builders you can use this plug-in: 
http://code.google.com/p/bpep/

Currently we are working on abstracting the Java model, in order to have the 
implementation based on reflection. Later we'll add an implementation for 
javax.lang.model.type classes. That will open a way to implementing the APT 
plug-ins. Unfortunately it's a long way to get there.

Original comment by pwyrzy...@gmail.com on 26 Apr 2011 at 10:09