Bug 458364 - Code Generation for Builder and Factory classes
Summary: Code Generation for Builder and Factory classes
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5   Edit
Hardware: PC Linux
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2015-01-25 15:57 EST by Michael Knapp CLA
Modified: 2019-08-06 17:19 EDT (History)
4 users (show)

See Also:


Attachments
an executable jar file that demonstrates the functionality I desire, it gives you help when you do java -jar quickCode.jar (7.69 KB, application/x-java-archive)
2015-01-25 15:57 EST, Michael Knapp CLA
no flags Details
Class before and after the builder is introduced (566 bytes, text/plain)
2018-08-24 14:07 EDT, Pierre-Yves Bigourdan CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Knapp CLA 2015-01-25 15:57:38 EST
Created attachment 250214 [details]
an executable jar file that demonstrates the functionality I desire, it gives you help when you do java -jar quickCode.jar

I have a proposed enhancement that should be pretty easy to add, but also speed up development time for many people around the world.  I'm constantly creating classes that follow the builder pattern, where you have one class whose job is to help you easily build another.  I searched through the eclipse Source functions but could not find what I was searching for.  So I wrote it.  

I attached an executable jar that demonstrates exactly what I was hoping for.  In case you don't trust it enough to run it, the source code is here:

https://bitbucket.org/msknapp84/knapp.open/src/f8b0e7655b99cacfd69648814f623e304a0ea5c9/tools/quickgen/src/quickgen/QuickCode.java?at=master

You could copy that source code, review it, compile it yourself and run it.

The way I'm hoping this would work is very simple: users would click either the Source or Refactor menus, and select a new option I would call "Generate Builder".  That would open up a window, where the user would have to populate rows in a table, describing each field name, its type, and whether it should be mutable or not.  

When the user hits ok, eclipse would either generate a new class, or update the current/active class.  When it's done, the user would have an easy way to build immutable objects.  This would make functional programming with java easier, since that style of coding strongly prefers to use immutable objects.

An example product can be found here:

https://bitbucket.org/msknapp84/knapp.open/src/251d007757c5f12f87a4c0caff7414372c7a16ba/tools/quickgen/example_outputs/Fubar.java?at=master

Please consider this since it would help many people be more productive.
Comment 1 Jay Arthanareeswaran CLA 2015-02-04 05:14:13 EST
If we were to consume this, I think the support should start from the UI. Moving to JDT UI for consideration.
Comment 2 Pierre-Yves Bigourdan CLA 2018-08-20 17:39:46 EDT
This bug hasn't received much attention, probably due to the fact that it's quite hard to find via the Bugzilla search and it does't seem to be referenced in Google. Nevertheless, it looks like quite a popular feature, at least according to this StackOverflow thread: https://stackoverflow.com/questions/29493898/automatically-create-builder-for-class-in-eclipse

Is Builder class generation still a desirable feature to integrate into the JDT UI codebase? I have had a dig through the code of existing refactoring actions, and I would be happy to have a go at implementing this.
Comment 3 Dani Megert CLA 2018-08-24 11:50:30 EDT
(In reply to Pierre-Yves B. from comment #2)

Can you provide an example in this bug here, so that one does not look at external resources?
Comment 4 Pierre-Yves Bigourdan CLA 2018-08-24 14:07:59 EDT
Created attachment 275530 [details]
Class before and after the builder is introduced

What I have in mind would be an "Introduce Builder..." tool, which would work similarly to the existing "Introduce Factory..." one. When selecting a constructor in a given class and using "Introduce Builder...", Eclipse would automatically generate a nested builder class which would make use of that constructor.

The Builder class would follow Joshua Bloch's popular builder pattern, as demonstrated in the attached example.

Some obvious user options that come to mind:
- allow customising the prefix of the builder methods (instead of "withSomeString" some users may want "addSomeString" or simply "someString".
- allow customising the name of the builder class.
- optionally switch the constructor for which the builder is introduced to private.
Comment 5 Dani Megert CLA 2018-08-30 09:36:59 EDT
(In reply to Pierre-Yves B. from comment #4)

Thanks.

I have no objections if you want to work on this.
Comment 6 Helo Spark CLA 2018-09-05 14:45:41 EDT
To avoid duplicated efforts here consider, that I have created a standalone builder generator Eclipse plugin a while back:
https://marketplace.eclipse.org/content/spark-builder-generator

It can mostly do what was discussed, but cannot (yet) create builder for certain constructor.

It is open source with a permissive licence, so feel free to reuse part of it, or integrate into that.
Comment 7 Pierre-Yves Bigourdan CLA 2019-08-06 17:19:36 EDT
(In reply to Helo Spark from comment #6)
> To avoid duplicated efforts here consider, that I have created a standalone
> builder generator Eclipse plugin a while back:
> https://marketplace.eclipse.org/content/spark-builder-generator
> 
> It can mostly do what was discussed, but cannot (yet) create builder for
> certain constructor.
> 
> It is open source with a permissive licence, so feel free to reuse part of
> it, or integrate into that.

I haven't had the time to properly look into this as I've been busy with quite a few other contributions. @Helo Spark maybe you can push this forward as you've already written most of the code to achieve the desired functionality? :)