Eclipse - SPLessons

Eclipse Java Code Generation

Home > Lesson > Chapter 14
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Eclipse Java Code Generation

Eclipse Java Code Generation

shape Introduction

This chapter Eclipse Java Code Generation demonstrate about the various code generation options used in Eclipse. The Eclipse currently contain seven code generation Wizards for Java, using this code generators user can save the time by instead of writing some huge lines of boring piece of code. Following are the concepts covered.
  • Code generation in Eclipse

Code generation in Eclipse

shape Description

Using code generation wizard in Eclipse developer can quickly write a code that contain a large portion of repeated code. The JDT is designed in such a way that the code generation feature saves a lot of time for developer and JDT currently contain seven code generation wizards as follows. These Wizards are available from Source Menu and selecting a field or method is optional.

Override/Implement Method

shape Description

This method override or implement the existing method in a file, in order to deal with Override/Implement Method follow the procedure mentioned below. Open a Java file and click anywhere on the file -> click Source menu-> click Override/Implement Method as shown in the image below. Now a popup window appears for Override/Implement methods providing the list of methods and the insertion point option, user can select either all the methods or deselect using Select All and Deselect All buttons, after performing all the required changes click the OK button as shown in the image below. Now, user can see the code get generated automatically as shown in the image below.

Generate Getters and Setters

shape Description

User can generate getters and setters for the fields that exist in the classes. Generating getters and setters in Eclipse is an easy way, in order to generate select a type or a field or click on anywhere on the file and follow the procedure mentioned. Select Source menu -> Click on Generate Getters and Setters as shown in the image below. Now, a popup Window appears for generating Getters and Setters, user can select either a getter or setter and if a filed had been selected, both a getter and setter would be automatically selected and get added as shown in  the image below. The Insert point can be changed as per the need and Access modifier can be changed, after performing all the required changes proceed by clicking OK button. Now, user can see the Getter and Setter fields added to the code as shown in the image below.

Generate Delegate Methods

shape Description

Client can make methods that delegate behavior to the fields or methods of the class. This methodology makes it conceivable to offer access to the data of a field or method without direct exposing this field or method. In order to generate delegate method follow the procedure mentioned. Click Source menu -> Click Generate Delegate Method as shown in the image below. Now, a popup window appears for selecting a delegated method provided with a list of objects to be delegated, select the target filed or method and click OK button in order to generate the delegation for selected method as shown in the image below.

Generate to String ()

shape Description

The Generate toString() action enables updating or creating toString() method. The beans as a rule needs to dump their field values for troubleshoot purposes, and it's somewhat dull to compose the dump code for it. The Generate toString() activity creates the toString() technique, dumping all the fields in a basic way. In order to deal with generate toString() follow the procedure mentioned. Click Source menu -> Click Generate to String() as shown in the image below. Now, a popup window appears provided with a list of objects to be delegated, select the target filed to generate a toString() method and by default all the available fields are selected. Now, click OK button in order to generate toString() for selected method as shown in the image below.

Generate hashcode() and equals()

shape Description

The equals() method returns true when an object passed to an argument is equal to the object on that method and whereas the hashCode() method returns an integer hash code value for an object that has been implemented by this method. In order to deal with Generate hashcode() and equals() follow the procedure mentioned. Click Source menu -> Click Generate hashcode() and equals() as shown in the image below. Now, a popup window appears provided with a list of objects that are to be included with hashCode() and edquals(), select the target filed to generate hashcode() and equals() method and by default all the available fields are selected. Now, click OK button in order to generate for selected method as shown in the image below.

Generate Constructor using Fields

shape Description

The code generator places the constructor after a filed by default, but client can create an argument utilizing Generating Constructor and can be included to the any field method in eclipse. In order to deal with this method follow the procedure mentioned. Click Source menu -> Click Generate Constructor using Fields as shown in the image below. Now, a popup window appears provided with a list of Constructor in order to invoke, select the target filed to generate the constructor method and by default all the available fields are selected. Now, click OK button in order to generate constructor for selected method as shown in the image below.

Generate Constructor from Superclass

shape Description

User can develop Constructor for a Superclass using the method Generate Constructor from Superclass. In order to deal with the method follow the procedure mentioned. Click Source menu -> Click Generate Constructor from Superclass as shown in the image below. Now, a popup window appears provided with a list of Constructor in order to implement, select the target filed to generate the constructor method and by default all the constructor fields are selected. Now, click OK button in order to generate constructor for selected method as shown in the image below.

Summary

shape Key Points

  • Eclipse contain various code generation options in Source menu.
  • The code generators are very useful in order to complete the project in time and to increase the productivity
  • The JDT currently contain seven code generations wizards.