Anatoly
Anatoly

Reputation: 5241

Is it possible to generate builder code automatically in Eclipse according to builder pattern?

There's builder pattern which was introduced by Joshua Bloch in him book Effective Java (2nd Edition), you can see description and examples of this pattern here: Too Many Parameters in Java Methods, Part 3: Builder Pattern

Is there any way to make this task done automatically by Eclipse? For example I define only properties, and builder and other methods and constructors will be generated by Eclipse?

Upvotes: 0

Views: 1244

Answers (2)

dimo414
dimo414

Reputation: 48864

A better solution than Eclipse code-completion (which tightly-couples your development process to a single IDE) is to use code-generation tools like AutoValue. With AutoValue you define an interface (specifically an abstract class) and it generates an implementation of that interface for you, complete with good default .equals(), .hashCode(), and .toString() implementations.

AutoValue also supports constructing AutoValue instances with a builder pattern using a similar syntax - you define the builder's interface, AutoValue generates the implementation.

The third edition of Effective Java in fact recommends AutoValue.

Upvotes: 0

Gavin
Gavin

Reputation: 1767

There plugins available that will generate builders for you, Eclipse might even have that built into the IDE these days.

We tried a few, with different results, I never liked them, i felt they were more trouble than they were worth, but thats a personal opinion.

Upvotes: 0

Related Questions