IntelliJ IDEA 14.1 Ships with Nextgen of Code Generation
IntelliJ IDEA already has impressive code generation capabilities: it can create getters and setters, equals(), hashCode(), toString(), and other methods, some of which are really simple to generate, and some are quite not.
That’s why the toString() method generator included the support for Velocity templates that could be modified and tailored to your needs. In IntelliJ IDEA 14.1 we’re extending this support to all other generated methods.
To fully support this new feature, the Template Editor has been upgraded with complete Velocity coding assistance.
Note that this particular editor improvement is only available in the Ultimate Edition, because it requires Velocity plugin, but everything else works well in Community Edition.
After the code has been generated, IntelliJ IDEA will make it clean and good-looking with automatic application of code style, Code Cleanup, and adding @Override annotations where needed.
If you’d like to give this feature a try, grab the latest IntelliJ IDEA 14.1 EAP build, and share your feedback with us on our discussion forum and in the issue tracker.
Develop with Pleasure!
Korosif says:
March 3, 2015Can I use the new Velocity based template system with AS3/Flex in IntelliJ, or is it a Java only feature?
By the way, it would be very useful when you talk about a feature if you can always disclose if it’s a Java only one, or if other language are concerned. I know it’s a Java centric IDE at core, but we’re quite a few that use IntelliJ for other things…
Thanks.
Anna Kozlova says:
March 9, 2015For now the feature works for java but the changes were made in the platform so it’s easy to extend it for all plugins.
Thanks for your notice.
Anna
Rob Freundlich says:
March 26, 2015I’ve just added a request for this in YouTrack
Dmitry Kandalov says:
March 3, 2015Thank you, looks good.
What about automatically regenerating these methods if class fields change?
Anna Kozlova says:
March 9, 2015Please watch/vote https://youtrack.jetbrains.com/issue/IDEA-89550
Thanks,
Anna
Alexander says:
March 25, 2015How to open this **** dialog?
Anna Kozlova says:
March 26, 2015Templates chooser is available over the member’s chooser. If you press ellipsis button, templates configurator would be shown
John says:
May 11, 2015Any way to change the equalsHeader.vm?
I want to have the generator add {} after every if statement
Anna Kozlova says:
May 11, 2015John, how is your code style configured? If it requires {} for if statements then generated code would contain them. In other words, generated code is automatically reformatted, so it is not expected that you need to change library part of the generators.
Anna
Max says:
August 3, 2015Hi,
Usually I use JGoodies binding and write data bean/model that extent a Model class (it has PropertyChangeSupport) and need to call following method in each setter:
firePropertyChange( String propertyName, Object oldValue, Object newValue)
.For example, I define propertyName and coresponding field as:
public static final String PROPERTY_DISPLAY_NAME = "displayName";
private String m_DisplayName;
I don’t know the syntax to write custom getter/setter code template. Want to ask how to make such a code template that will create the setter like following for that property:
public void setDisplayName( final String in_NewValue ){
final String oldValue = m_DisplayName;
m_DisplayName = in_NewValue;
firePropertyChange( PROPERTY_DISPLAY_NAME, oldValue, in_NewValue );
}
Thanks!
Anna Kozlova says:
August 10, 2015Hi, Max!
You need to write velocity template. looks like that you can leave untouched default setter name but instead of part “$field.name = $paramName;” you need to write your code which stores old value to the temp variable:
{code}
final String oldValue = $field.name;
$field.name = in_NewValue;
firePropertyChange( PROPERTY_$StringUtil.toUpperCase($field.name), oldValue, in_NewValue );
{code}
snowdream says:
January 8, 2017I need a kotlin code generator!!!
RK says:
February 21, 2017How can I break the equals() method into multiple methods:
final class Foo {
private final A a;
private final B b;
@Override
public boolean equals(Object obj) {
return (this == obj) || ((obj instanceof Foo) && equalsOther((Foo)obj));
}
private boolean equalsOther(Foo other) {
return Objects.equals(a, other.a) &&
Objects.equals(b, other.b);
}
}