3

I'm trying to make a code template that will generate tostring, constructor from field, and a default constructor.

I already looked at Useful Eclipse Java Code Templates and in http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-tostring-templates.htm but it was not what I was looking for.

I tried this plugin http://eclipse-jutils.sourceforge.net/ but I still need to manually select an option in the menu (and it doesn't have a "constructor from fields" option).

I need to generate these methods and constructors for more then 100 classes so this the best way i found coz eclipse dont give tool to do it for more then one class and for this one class that he give this tool i need to do it one by one the (generate tostring ,constructor from field and also default constructor)

i will love to some help or some advice on a way to create these methods for all my classes, automatically. thanks in advance.

Community
  • 1
  • 1
Idan
  • 901
  • 4
  • 13
  • 29
  • 1
    @user986474 Do you know about Lombok project? It may generate many of the methods for you based on annotations (which you may add automatically by using shell script, for example). See more details there: http://projectlombok.org/features/index.html – Slava Semushin Oct 09 '11 at 16:15
  • JUtils plugin seems to be quite old and unmanaged, it misses the ability to create default constructor, also for toString there should be possibility to decide either to use inherited fields or not. There is full-POJO-ise missing (default + copying + all fields constructor, hashCode, equals, toString, serializedVersionUID, getters and setters) – Danubian Sailor Jan 27 '12 at 09:25

2 Answers2

2

I don't know of a plugin that will do this for multiple classes.

I'd just do it manually, even though it'd take time.

You could also use reflection and a scripting language like Groovy/JRuby/etc. to create the constructors, and rely on something like Commons' ToStringBuilder to create a toString, or just use reflection again.

(One problem is if you don't want a property in the constructor or toString you need to have a mechanism to tell the generator as much.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

I have just used Practically Macros, within a few minutes of install from the market place, I could generate *constructors*, getters / setters, toString, hashcode and equals (basically chaining the standard eclipse commands) in a single command. Just what I was looking for and saved me loads of time. I can also see a lot more uses for it, well done to Earnst (the creator).

davidhilton68
  • 321
  • 2
  • 6