Skip to content

Commit 0496888

Browse files
committed
generating code, refactoring and summary
1 parent 4af8ccf commit 0496888

File tree

10 files changed

+184
-2
lines changed

10 files changed

+184
-2
lines changed
186 KB
Loading
231 KB
Loading
115 KB
Loading
96 KB
Loading
82.2 KB
Loading
63.3 KB
Loading
16.2 KB
Loading
19.6 KB
Loading
107 KB
Loading

app/pages/learn/01_tutorial/01_your-first-java-app/03_writing-java-applications-with-eclipse.md

Lines changed: 184 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ toc:
1414
- Dealing with compilation errors and warnings {errors}
1515
- Running a program {run}
1616
- Debugging {debugging}
17+
- Generating code {generating}
18+
- Refactoring {refactoring}
19+
- Summary {summary}
1720
description: "Installing and getting started with the Eclipse IDE for developing Java applications"
18-
last_update: 2024-04-02
21+
last_update: 2024-04-15
1922
author: ["DanielSchmid"]
2023
---
2124
<a id="intro">&nbsp;</a>
@@ -52,7 +55,7 @@ This opens a dialog similar to the project creation dialog. It allows specifying
5255
<a id="content_assist">&nbsp;</a>
5356
## Content Assist
5457

55-
Eclipse can help you write Java code by automatically completing parts of it. When pressing the key combination `Ctrl`+`Space` (or ``+`Space` on MacOS or `Alt`+`/` on chinese systems) while editing Java code, Eclipse automatically suggests ways to complete the code. These suggestions can be confirmed by pressing `Enter` or double-clicking on the suggestions.
58+
Eclipse can help you write Java code by automatically completing parts of it. When pressing the key combination `Ctrl`+`Space` (or ``+`Space` on macOS or `Alt`+`/` on chinese systems) while editing Java code, Eclipse automatically suggests ways to complete the code. These suggestions can be confirmed by pressing `Enter` or double-clicking on the suggestions.
5659

5760
For example, typing `main` in a class followed by pressing `Ctrl`+`Space` suggests adding a main method.
5861
[![Content assist suggesting a main method](/assets/images/eclipse/content_assist_main.png)](/assets/images/eclipse/content_assist_main.png)
@@ -110,3 +113,182 @@ Upon opening the debug perspective, you should still see your code in the middle
110113
While the program is suspended, you can tell it how to continue executing using buttons in the toolbar at the top.
111114
[![Buttons for controlling execution flows in the toolbar](/assets/images/eclipse/debug_toolbar_buttons.png)](/assets/images/eclipse/debug_toolbar_buttons.png)
112115
You can execute one line using `Step Over` [![Step Over button](/assets/images/eclipse/debug_step_over.png)](/assets/images/eclipse/debug_step_over.png) (`F6`), go into a method using `Step Into` [![Step Into button](/assets/images/eclipse/debug_step_into.png)](/assets/images/eclipse/debug_step_into.png) (F5) or continue executing the program until the next breakpoint with `Resume` [![Resume button](/assets/images/eclipse/debug_resume.png)](/assets/images/eclipse/debug_resume.png) (`F8`).
116+
117+
<a id="generating">&nbsp;</a>
118+
## Generating code
119+
120+
Sometimes you might need to write repetitive code that doesn't contain much business logic and can be generated using information from existing code. An example of this is getters/setters or `equals`/`hashCode`/`toString` methods which typically just need to access some fields. While it is often preferable to use [records](/learn/records), Eclipse allows comes with functionality to generate these pieces of repetitive code.
121+
122+
In order to do this, you first need to create a class with some fields you want to generate these methods for. In this example, we will create a `Person` class that stores the first name, last name and age of a person.
123+
```java
124+
public class Person {
125+
private String firstName;
126+
private String lastName;
127+
private int age;
128+
//we want to generate code here
129+
130+
}
131+
```
132+
133+
When right-clicking in that class, there is an option called `Source` providing various ways to generate code. Here, we can select `Generate Getters and Setters...` in order to generate accessor methods for the fields in the `Person` class.
134+
[![Generate Getters and Setters](/assets/images/eclipse/context_generate_getters_setters.png)](/assets/images/eclipse/context_generate_getters_setters.png)
135+
136+
This option should open up a new window allowing us to configure which fields we want to generate accessors for. In order to create accessors for all fields, use the `Select All` button. and click `Generate` on the bottom right.
137+
[![Generate Getters and Setters](/assets/images/eclipse/getter_setter_modal.png)](/assets/images/eclipse/getter_setter_modal.png)
138+
139+
After doing this, the class should look as follows:
140+
```java
141+
public class Person {
142+
private String firstName;
143+
private String lastName;
144+
private int age;
145+
//we want to generate code here
146+
public String getFirstName() {
147+
return firstName;
148+
}
149+
public void setFirstName(String firstName) {
150+
this.firstName = firstName;
151+
}
152+
public String getLastName() {
153+
return lastName;
154+
}
155+
public void setLastName(String lastName) {
156+
this.lastName = lastName;
157+
}
158+
public int getAge() {
159+
return age;
160+
}
161+
public void setAge(int age) {
162+
this.age = age;
163+
}
164+
165+
}
166+
```
167+
168+
Similarly, it is possible to generate the `hashCode` and `equals` methods using the menu `Source` > `Generate hashCode() and equals()...`.
169+
[![Generate hashCode and equals](/assets/images/eclipse/context_generate_hashcode_equals.png)](/assets/images/eclipse/generate_hashcode_equals.png)
170+
171+
This also opens a window which allows to select the fields to include in the `hashCode` and `equals` methods.
172+
[![Selecting fields to use in hashCode and equals](/assets/images/eclipse/hashcode_equals_modal.png)](/assets/images/eclipse/hashcode_equals_modal.png)
173+
174+
After clicking `Generate`, Eclipse automatically adds these methods to the class.
175+
```java
176+
import java.util.Objects;
177+
178+
public class Person {
179+
private String firstName;
180+
private String lastName;
181+
private int age;
182+
//we want to generate code here
183+
public String getFirstName() {
184+
return firstName;
185+
}
186+
public void setFirstName(String firstName) {
187+
this.firstName = firstName;
188+
}
189+
public String getLastName() {
190+
return lastName;
191+
}
192+
public void setLastName(String lastName) {
193+
this.lastName = lastName;
194+
}
195+
public int getAge() {
196+
return age;
197+
}
198+
public void setAge(int age) {
199+
this.age = age;
200+
}
201+
@Override
202+
public int hashCode() {
203+
return Objects.hash(age, firstName, lastName);
204+
}
205+
@Override
206+
public boolean equals(Object obj) {
207+
if (this == obj)
208+
return true;
209+
if (obj == null)
210+
return false;
211+
if (getClass() != obj.getClass())
212+
return false;
213+
Person other = (Person) obj;
214+
return age == other.age && Objects.equals(firstName, other.firstName)
215+
&& Objects.equals(lastName, other.lastName);
216+
}
217+
218+
}
219+
```
220+
221+
Another method that is often generated is `toString()` which returns a `String` representation of the object.
222+
To generate that method, select `Generate toString()...` in the `Source` menu.
223+
[![Generate toString](/assets/images/eclipse/context_tostring.png)](/assets/images/eclipse/context_tostring.png)
224+
225+
As before, this opens a window allowing to specify options on how exactly the code should be generated.
226+
[![Options for toString](/assets/images/eclipse/tostring_options.png)](/assets/images/eclipse/tostring_options.png)
227+
228+
Using the `Generate` button, Eclipse generates the `toString` method as it did with the other methods before.
229+
```java
230+
import java.util.Objects;
231+
232+
public class Person {
233+
private String firstName;
234+
private String lastName;
235+
private int age;
236+
//we want to generate code here
237+
public String getFirstName() {
238+
return firstName;
239+
}
240+
public void setFirstName(String firstName) {
241+
this.firstName = firstName;
242+
}
243+
public String getLastName() {
244+
return lastName;
245+
}
246+
public void setLastName(String lastName) {
247+
this.lastName = lastName;
248+
}
249+
public int getAge() {
250+
return age;
251+
}
252+
public void setAge(int age) {
253+
this.age = age;
254+
}
255+
@Override
256+
public int hashCode() {
257+
return Objects.hash(age, firstName, lastName);
258+
}
259+
@Override
260+
public boolean equals(Object obj) {
261+
if (this == obj)
262+
return true;
263+
if (obj == null)
264+
return false;
265+
if (getClass() != obj.getClass())
266+
return false;
267+
Person other = (Person) obj;
268+
return age == other.age && Objects.equals(firstName, other.firstName)
269+
&& Objects.equals(lastName, other.lastName);
270+
}
271+
@Override
272+
public String toString() {
273+
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
274+
}
275+
276+
}
277+
```
278+
279+
280+
<a id="refactoring">&nbsp;</a>
281+
## Refactoring
282+
283+
When working on Java applications, it is often necessary to change existing code in various ways while preserving functionality. Eclipse supports developers doing that by providing various refactoring options. An example of that is renaming class, methods or fields. This can be done by clicking on a class, method or variable name, right-clicking and selecting `Refactor` > `Rename`.
284+
[![Rename context menu](/assets/images/eclipse/context_rename.png)](/assets/images/eclipse/context_rename.png)
285+
286+
It is then possible to change to name to something different and confirming it using the `Enter` key. This also updates all references to the renamed element.
287+
[![Renaming a class name](/assets/images/eclipse/rename_box.png)](/assets/images/eclipse/rename_box.png)
288+
[![Renaming a class name](/assets/images/eclipse/rename_different_text.png)](/assets/images/eclipse/rename_different_text.png)
289+
290+
291+
<a id="summary">&nbsp;</a>
292+
## Summary
293+
294+
As you can see, the Eclipse IDE provides a lot of tools that help developers writing Java applications. While this article shows some, Eclipse comes with many more features which can be especially useful when working on bigger applications. If you are interested in reading more, check out the [Java Development user guide](https://help.eclipse.org/latest/index.jsp?nav=%2F1).

0 commit comments

Comments
 (0)