Bug 495359 - Generate Constructor: generate useful constructor instead of default constructor
Summary: Generate Constructor: generate useful constructor instead of default constructor
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-03 04:38 EDT by Roland Illig CLA
Modified: 2016-06-06 05:14 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Roland Illig CLA 2016-06-03 04:38:21 EDT
Given the following code:

public class KeyValue {

  private final String key;
  private final String value;
  
  ${cursor}
  
}

I have just defined a class having two fields, and now I need to add all the boilerplate code that Java requires:

* the constructor(s); only one in this case
* the getters
* the setters

When I press Ctrl+Space now, Eclipse offers me to insert a default constructor for the class, which is completely off-topic, since all fields are final. What I want Eclipse to insert is:

* a constructor for all final fields
* the getters for these fields, followed by its setters (unless the field is final)

This is such a common task that it should be simple to achieve. Pressing Ctrl+Space is ideal in this situation because this shortcut is used heavily in this situation. It’s just that the offered options are not appropriate to the current context.

Other example:

public class ModifiableKeyValue {

  private String key; 
  private String value; 

  ${cursor}

}

Pressing Ctrl+Space should offer to generate in one go:

* the default constructor
* the constructor initializing all fields
* for each field the getter and the setter

public class KeyModifiableValue {

  private final String key; 
  private String value; 

  ${cursor}

}

Pressing Ctrl+Space should offer to generate in one go:

* the constructor initializing all final fields
* the constructor initializing all fields
* for each field the getter and the setter
Comment 1 Stephan Herrmann CLA 2016-06-03 18:46:01 EDT
Are you aware of the following commands?

  Source > Generate Getters and Setters

  Source > Generate Constructor using Fields

They seem to do exactly what you request. Just these commands are not suitable for Ctrl+Space because user input is required for selecting various options.


Off-topic:

(In reply to Roland Illig from comment #0)
> [...] and now I need to add all the boilerplate code that Java requires:
> [..]
> * the getters
> * the setters

Java doesn't require getters and setters, it's so-called "best practice", nothing more. The following classes are fully equivalent:

final class Foo1 {
   private String val;
   public void setVal(String val) {
      this.val = val;
   }
   public String getVal() {
      return this.val;
   }
}

final class Foo2 {
   public String val;
}

The choice is your's :)
Comment 2 Roland Illig CLA 2016-06-06 05:14:41 EDT
(In reply to Stephan Herrmann from comment #1)
> Are you aware of the following commands?
> 
>   Source > Generate Getters and Setters
> 
>   Source > Generate Constructor using Fields
> 
> They seem to do exactly what you request. Just these commands are not
> suitable for Ctrl+Space because user input is required for selecting various
> options.

I am aware of these commands, but they feed too far away from the code I’m currently editing. I’m focused on typing on the keyboard, and these commands currently require me to nagivate through the menus, open some large dialogs and require me to do several choices.

I think most of these choices are normally not necessary. That’s why I would like the Ctrl+Space menu to be extended with generating a _suitable_ constructor instead of the default one. And while here, I thought it would be nice if Eclipse generated the getters and setters along the way.

> Off-topic:
> 
> (In reply to Roland Illig from comment #0)
> > [...] and now I need to add all the boilerplate code that Java requires:
> > [..]
> > * the getters
> > * the setters
> 
> Java doesn't require getters and setters, it's so-called "best practice",
> nothing more.

Shouldn’t Eclipse make it easy for Java developers to follow so-called “best practice”? ;) And, by the way, Java is not the programming language alone, it is also the frameworks and common sense around that language. At _that_ requires me to write getters and setters.

> The following classes are fully equivalent:
> final class Foo1 {
>    private String val;
>    public void setVal(String val) { this.val = val; }
>    public String getVal() { return this.val; }
> }
> 
> final class Foo2 {
>    public String val;
> }

If they were fully equivalent, Java would be a great language. But they are not. I cannot use the second class in JSPs and several other technologies.

> The choice is your's :)

No, it isn’t. I’m bound to the frameworks that my projects have chosen several years ago. Therefore I am required to have all this boilerplate in my code. It would be nice of Eclipse to support me doing this boring coding.