Generating code

In Java, you will eventually find yourself writing similar pieces of code over and over. Even though the good usage of design principles tends to minimize this, there are some methods, such as getters, setters, and constructors, in most cases look exactly the same. Eclipse can help you by generating code for these simple methods.

You can see all the code that Eclipse can generate by right-clicking anywhere in the Java Editor and choosing the Source entry. This menu can also be opened with the shortcut Alt + Shift + S.

Generating getters and setters

Getters and setters are generally the same. They provide access to some private field of the class by either returning its value or by assigning a new one. To avoid this no-brainer task and save some time to write real code, choose Generate Getters and Setters from the Source entry. You will be presented with a window, as shown in the following screenshot:

Generating getters and setters

In the Generate Getters and Setters window, you will be able to determine for which fields you want to generate getters and setters, as well as choosing their access modifiers and where in the code they will be inserted.

Generating constructors

Just like getters and setters, class constructors have a very typical template that receives values to be assigned to the class's fields and calls the superclass's constructor. You can also skip this tedious task by clicking on Generate Constructor using Fields in the Source menu entry. You will be presented with a window similar to the Generate Getters and Setters one in which you will be prompted to choose which fields will have their values initialized by the constructor, and where the constructor will be inserted in the code, among others.

Generating the hashCode() and equals() methods

There are cases in which you have to override the equals() and hashCode() methods inherited from the Object class in order to treat two different instances as being equal when they have the same parameter values.

Eclipse also has a template for these two methods. By choosing Generate hashCode() and equals() inside the Source menu entry, you will be able to generate methods that utilize the fields of your choice to compare two instances and to generate the hash code of the instance. The template for the equals() method will check if the other object is null, if it is an instance of the same class, and if the selected parameters are equal.

Generating the toString() method

To generate a toString() method that returns the value of the class's fields in a human-readable form, choose the Generate toString() option. Like the previous generators, this one will allow you to choose which fields you want your to String() method to contain.

Generating method comments

Comments for methods in the Javadoc format can easily be generated in Eclipse. For this, right-click on the desired method, and navigate to Source | Generate Element Comment, or use the shortcut Alt + Shift + J with the caret positioned in the method's signature line. These comments will be shown in Eclipse when the user hovers the mouse over the method's call or when the content assist feature is used.

The generated comment contains the method's parameters and return type. You only have to add a simple description of the method's usage and some words about the parameters when required. An example of a generated comment is presented in the following screenshot:

Note

These shortcuts might look useless and hard to memorize, but once you get used to them, they become incredibly handy and increase your coding speed considerably since you'll rarely have to take your hands off your keyboard when you know the ones you use most by heart. The Appendix, Keyboard Shortcuts contains an extensive list of all the shortcuts presented in this book.

Generating method comments

Editing code and comment templates

Although these templates will work on most of the situations, your project might have peculiarities that require a specific type of constructor. You can still take advantage of code and comment generation by editing the templates and tailoring them to fit your needs. For this, right-click on the project's entry in the Package Explorer and choose Properties. Expand the Java Code Style entry in the left menu and choose Code Templates. The following window will open:

Editing code and comment templates

By choosing a type of generated code or comment and clicking on Edit, you will be able to add code and variables to the template. You can also choose to add comments on newly generated methods and types automatically.

The code template settings edited in this window are project specific. The settings are saved in a metadata file inside the project folder, so when a project is imported, the coding style settings are automatically set. This allows you to enforce the project's coding style among all the project's developers. If you want to edit the default code templates for the whole workspace, open the Preferences window and navigate to Java | Code Style | Code Templates. Projects that don't contain any specific setting for templates will use these settings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset