History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEADEV-10999
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Alexey Kudravtsev
Reporter: Taras Tielkes
Votes: 20
Watchers: 18
Operations

If you were logged in you would be able to see more operations.
IDEA: Development

Allow cusomization of generated getter/setter/hashCode/equals

Created: 25 Mar 05 15:24   Updated: 06 Nov 08 23:29
Component/s: Editor. Code Completion
Fix Version/s: Undefined

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown
Issue Links:
Duplicate
 
This issue is duplicated by:
IDEA-14545 Generate getter for collections field... Resolved
IDEADEV-10994 setter/getter template Major Resolved
Relates
This issue relates to:
IDEABKL-3636 Generated code should not produce war... Normal Open
 

Build: 3,265


 Description  « Hide
The current GenerateToString plugin allows the developer to customize the generated code by editing a template.

It would be a nice improvement if a similar facility was available for the built-in code generation facility (Alt-Insert) for getters/setters/hashCode/equals.

Some simple things I'd like to customize:
-generete javadoc for getters/setters, similar to the Eclipse default, @see field declaration
-generete javadoc for hashCode()/equals(), @see Object#equals()/hashCode()
-make the parameters for setters/equals final

Although the generated code for equals()/hashCode() has improved quite a lot from older IDEA releases, it would be good to have the opportunity to alter it.



 All   Comments   Work Log   Change History      Sort Order:
Niels Ull Harremoës - 31 Aug 05 16:32
The currently generated equals code trigger the "Redundant 'if' statement" inspection. We'd like to modify it.

Eran Lo - 25 Aug 06 23:36
Josh Bloch, in his "Effective Java Reloaded" talk at Java One, suggested using @Override notation with the equals method, causing a compilation error if the equals method overloads the Object equals, rather than overrides it. Having a customizable equals would make this very easy. Addmitedly, the generated equals is always for an Object parameter. Still, if you want to adopt the habit you'd want it with every equals method.

Tim Fennell - 15 Dec 06 20:49
II also would like to control the format that is used when IDEA generates getter/setter pairs for me. For example, if I'm generating getter/setter pairs for two members (foo and bar), I'd like it to look like this:

public void setFoo(Foo foo) { this.foo = foo; }
public Foo getFoo() { return this.foo; }

public void setBar(Bar bar) { this.bar = bar; }
public Bar getBar() { return this.bar; }

So basically, one method == one line, and no space between the getter/setter pair, but a line between each pair. Right now, it generates each getter/setter over three lines and with white space between each method.

If there was a generic mechanism to edit the output of these methods (and the templates could be tied to a project file so that it's used for all developers in a project), that'd rock.


Esko Luontola - 09 Dec 07 15:51
I would like it to be possible to generate "setters and getters" using methods which do not start with "set" and "get". For example like this:
public Foo foo() {
    return foo;
}

public void changeFooTo(Foo foo) {
    this.foo = foo;
}

It would be best to be able to have many setter/getter templates in use at the same time, so that you could choose at code generation time whether you need to create a setter which uses the traditional "setFoo" style (required by many frameworks) or a fluent "changeFooTo" style (makes the code more readable).


Esko Luontola - 09 Dec 07 23:46
Also this kind of "setter" for immutable objects could be handy:
public MyClass withFoo(Foo foo) {
    MyClass copy = this.clone();
    copy.foo = foo;
    return copy;
}

or

public MyClass withFoo(Foo foo) {
    MyClass copy = new MyClass(this);
    copy.foo = foo;
    return copy;
}

In other words, the template system should be flexible enough to allow creating just about any kind of setters and getters.


Bryan Young - 09 Jan 08 08:48
I have another use case: Lazy initialized getters. A previous employer used to use them as a convention in Swing guis to initialize each component before returning them. It actually went a long way towards cleaning up Swing code which very easily becomes messy. At the time, I tried using code templates for this, but I was never able to get them to work:

public $FIELDTYPE get$FIELDNAME() {
if ($FIELDNAME == null) { $FIELDNAME = new $FIELDTYPE(); $END }
return $FIELDNAME;
}