51

Is there a way to use an automatic builder to create builder (Joshua Bloch's Builder Pattern) for classes in Eclipse? For example an option in the menu, a plugin or something else. I could not find anything under "Refactor".

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • 1
    A quick google search for `eclipse plugin builder pattern` got me (among others) https://code.google.com/p/fluent-builders-generator-eclipse-plugin/ – Dirk Lachowski Apr 07 '15 at 14:30
  • Unless you can somehow achieve this by making a template, Annotation Processing is the only thing I can think of. It allows you to create an annotation, such as `@Build`, then create a processor to look for that annotation in your source code, raise an error when there's no builder class (optional), and allow the client to generate the builder code similar to how one automatically declared unimplemented methods. As for plugins, I'm sure you know the rules by now /: It sucks, but you can't request for tools on here – Vince Apr 07 '15 at 14:30
  • 1
    @Dirk - I do not like your answer. It's not possible to downvote the comments but I am virtually giving you -1. Maybe you should not be happy just with the pure existence of a link found by google. I have tried the Fluent Builder Generator and it has not been updated for 10 years and generates quite complicated code instead of a nice simple builder. – Honza Zidek Jun 25 '15 at 14:07
  • @HonzaZidek I think you have misunderstood the point of my comment. The OP hasn't shown any effort to find an answer by himself. I only wanted to point out the fact that some simple googling got me answers (plural, that's why i have written 'among others'). I know that a "here do you have a link" isn't an answer - that's why i have put it in a comment. Maybe i better had voted to close... – Dirk Lachowski Jun 26 '15 at 06:41
  • @Dirk: I would not close it! I did myself some research and I have not found any useful eclipse plugin for the builder - that's how I came to this post. So I would enjoy if someone may *answer* the OP's question, not to close it! You may show your ability to use google and find something really working, configurable and up-to-date and post it as an answer :) – Honza Zidek Jun 26 '15 at 07:42
  • @HonzaZidek Using my google foo? I'm sorely tempted :) – Dirk Lachowski Jun 26 '15 at 09:15
  • 1
    @HonzaZidek It's not an eclipse plugin but maybe you'll give it a try: https://github.com/mkarneim/pojobuilder – Dirk Lachowski Jun 26 '15 at 09:26
  • Eclipse badly needs this functionality. Someone with more patience than me please file an issue ;) – Sridhar Sarnobat Mar 19 '18 at 01:52
  • 1
    @Sridhar-Sarnobat actually there is a bug report, hidden inside Bugzilla: https://bugs.eclipse.org/bugs/show_bug.cgi?id=458364 – Pyves Aug 20 '18 at 21:33
  • One problem of lombok. It generate all in compile time. This can be a perfomance neck. Use with caution. – user3115223 Oct 25 '21 at 14:46

5 Answers5

29

You may want to look at lombok annotations to generate builders without the boiler plate code. For example:

@Builder
public class MyPojo {
    private String name;
}

MyPojoBuilder.builder().name("yourame").build();

The limitation is that this doesn't seem to work with abstract classes.

jaco
  • 544
  • 6
  • 14
  • 1
    A definitie +1 for directing me to a great project that I had never seen before. This solves the problem and simplifies some of the other hassles with generating boilerplate code as well :). – Carsten Hoffmann Oct 23 '15 at 11:39
  • 2
    Impressive capabilities. Wonder why lombok isn't popular? – Basil Musa Mar 28 '16 at 21:45
  • 1
    I found this answer when looking for an alternative to Lombok because the `@Builder` annotation still (in April 2017) does not work for members which are provided by parent classes (as Jaco mentions). – 8bitjunkie Apr 11 '17 at 15:02
  • 6
    @BasilMusa whether lombok is or is not popular you should read "What are the risks with Project Lombok?" here: https://stackoverflow.com/questions/4589184/what-are-the-risks-with-project-lombok and also "Is it safe to use Project Lombok?" here: https://stackoverflow.com/questions/3852091/is-it-safe-to-use-project-lombok – inor Apr 10 '18 at 14:08
23

Maybe I am late to the party.

Eclipse on its own does not provide a way to generate code to support builder pattern. However it can be extended through plugins to enhance the functionality.

There is this plugin that I use this:

https://github.com/henningjensen/bpep

Edit: After 5 years, revisiting this topic, would recommend to use lombok that has become industry standard anyway and is also IDE agnostic ==> would work if your team mates use a variety of IDEs. Check out this annotation. You could also include pattern inheritance among classes.

@Builder(toBuilder = true)
// or you could use this for inheritance
@SuperBuilder
Khanna111
  • 3,627
  • 1
  • 23
  • 25
17

I currently use Spark Builder Generator with Eclipse Neon.1a Release (4.6.1) and it works well.

You can set the preferences under:
Window->Preferences->Java->Spark Builder Generator

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
Christoph
  • 3,980
  • 2
  • 40
  • 41
3

Try https://github.com/vojtek/write-it-once

package ${cls.package.name};

public class ${cls.shortName}Builder {

public static ${cls.name}Builder builder() {
    return new ${cls.name}Builder();
}
<% for(field in cls.fields) {%>
private ${field.type.name} ${field.name};
<% } %>
<% for(field in cls.fields) {%>
public ${cls.name}Builder ${field.name}(${field.type.name} ${field.name}) {
    this.${field.name} = ${field.name};
    return this;
}
<% } %>
public ${cls.name} build() {
    final ${cls.name} data = new ${cls.name}();
<% for(field in cls.fields) {%>
    data.${field.setter.name}(this.${field.name});
<% } %>
    return data;
}
}
Atmega
  • 131
  • 4
  • its quite simple solution - create separate separate project with templates, and generate java classes form main – Atmega Apr 16 '18 at 19:16
2

You could add your own template window -> preferences -> java -> editor -> templates and this will be activated with the content proposal but not by refactor action

unique_ptr
  • 586
  • 1
  • 8
  • 22
  • 5
    I do not think that you could write a template for a builder pattern. Even if it would work, it is a bad idea, because templates do not provide a way to be updated, you would have to manually delete the code and reapply the template. – Carsten Hoffmann Oct 23 '15 at 11:41