Bug 405455 - [getter setter] Generate field for getter method
Summary: [getter setter] Generate field for getter method
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 427180 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-04-11 07:28 EDT by Wolfgang Mattuttis CLA
Modified: 2014-02-01 14:38 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Mattuttis CLA 2013-04-11 07:28:31 EDT
Currently it is possible to generate Getters/Setters from fields.

I would like to see a feature which does the opposite. Generate (private) fields for implemented (stubbed) getters.
As an addon the body of the getters should return the (generated) field reference.

Would safe quite some coding ;-)
Comment 1 Dani Megert CLA 2013-04-11 07:51:17 EDT
Can you give a concrete example and explain why this would be useful?
Comment 2 Wolfgang Mattuttis CLA 2013-04-11 08:16:24 EDT
I have an interface:

public interace MyInterface {
    Long getId();

    Long getFirstName();

    Long getLastName();

    Date getBirthDay();

    ...
}

When I create a new class implementing this interface I initially get:

public MyClass implements MyInterface {

    public Long getId() {
        return null;
    }

    ...
}

I would like to see a feature which can generate the field based on the getters (or setters) and have the method bodies implemented with returning the field instead of null.

Example (after running new feature):

public MyClass implements MyInterface {

    private Long id;       // GENERATED

    .
    .                      // OTHER PROPERTIES GENERATED AS WELL
    .

    public Long getId() {
        return id;         // GENERATED
    }

    .
    .                      // OTHER METHOD BODIES GENERATED AS WELL
    .
}


Again could safe a lot of coding in certain circumstances (e.g. in the context of domain, DTO or ORM objects).

Hope this helps
Comment 3 Dani Megert CLA 2013-04-11 08:54:15 EDT
(In reply to comment #2)

I see.

>     public Long getId() {
>         return id;         // GENERATED


We could even be smarter when modifying the existing getter:

getX() {
  some code
  return something;
}
==>
getX() {
  if (fieldX == null) {
    some code
    fieldX= something;
  }
  return fieldX;
}
Comment 4 Wolfgang Mattuttis CLA 2013-04-11 09:05:31 EDT
Yup, this could be some optional 'templating' code.
Comment 5 jules CLA 2014-02-01 14:33:46 EST
*** Bug 427180 has been marked as a duplicate of this bug. ***
Comment 6 jules CLA 2014-02-01 14:37:37 EST
See also description in the bug listed above.  There are at least two cases where this would be useful:

* Implementing an interface/abstract class through the wizard with the option to create stubs for abstract methods enabled
* Performing a "quick fix" of an abstract method unimplemented error

As to whether two steps should be required or there is an option as to whether or not to create the fields in the original dialog/list of fixes/preferences, I'm not sure which is best, but it certainly seems it should be possible to do this.
Comment 7 jules CLA 2014-02-01 14:38:24 EST
Also note -- should also apply to setter methods, not just getters.