2

I have recently started using Eclipse for developing some Google App Engine code, I'm at the early stages where I am creating lots of entity / domain classes. My usual pattern is to create the domain class, add the private properties, then I go through the IDE menus to

  1. Generate constructors based on fields
  2. Generate default constructor
  3. Generate getters and setters
  4. Generate hashcode / equal functions
  5. Generate toString functions

What seems crazy to me is that I can't just go to a combined view that enables me to specify the fields and have checkboxes for each of the items listed above, in one step.

I guess Groovy would give me a resolution to number 3 - as it will generate automatic getters and setters, but I don't want to add anything 'new' to my learning curve for a quick project.

Is there a way in Eclipse to do this code-generation quickly - rather than my manual 5 steps that are driving me crazy?! I would also be interested to know if this feature is available in Netbeans as I also use this heavily.

Thanks, Rob.

RobbiewOnline
  • 1,350
  • 1
  • 16
  • 36

2 Answers2

1

I'm not aware of an automatic way to generate the type of constructor mentioned in #1.

#2 through #5 can be addressed quickly using the ctrl+space drop-down. In the drop-down below, you can see the code generation options for the default constructor, equals, hashCode, toString, and the two getter/setter pairs.

Typing the first few letters of an option first will narrow the options down quicker. For example, typing "set" followed by ctrl+space will reduce the list down to those options that start with "set", like the setter-generating options.

Example of ctrl+space for code generation

user1201210
  • 3,801
  • 22
  • 25
0

Take a look at the code generation capabilities in the Eclipse M2T (Model to Text) project. There are several technologies there that each address this problem in a different way. The basic idea is that you model the code you want to generate and then generate your code from that model using some sort of templating approach. The model, for example, would list the names, types and dimensions of each property in a class and the templates would provide boilerplate for the declaration, getters, setters and any other use of those variables.

It turns out you can model an architectural pattern for the entire set of classes you want to generate and so it is possible to generate almost of of your code with the exception of business logic.

I would tell you to use M2T-JET, but there will be equally outspoken advocates for each of the other technologies in Eclipse M2T. Here's a link to another answer in which I gave an example of how to use M2T-JET

Community
  • 1
  • 1
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59