IntelliJ IDEA 14.1 Ships with Nextgen of Code Generation

Anna Kozlova

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.

generate_equals_hash_code

To fully support this new feature, the Template Editor has been upgraded with complete Velocity coding assistance.

edit_code_generation_templates

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!

Comments below can no longer be edited.

13 Responses to IntelliJ IDEA 14.1 Ships with Nextgen of Code Generation

  1. Korosif says:

    March 3, 2015

    Can 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, 2015

      For 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, 2015

        I’ve just added a request for this in YouTrack

  2. Dmitry Kandalov says:

    March 3, 2015

    Thank you, looks good.
    What about automatically regenerating these methods if class fields change?

  3. Alexander says:

    March 25, 2015

    How to open this **** dialog?

    • Anna Kozlova says:

      March 26, 2015

      Templates chooser is available over the member’s chooser. If you press ellipsis button, templates configurator would be shown

  4. John says:

    May 11, 2015

    Any way to change the equalsHeader.vm?
    I want to have the generator add {} after every if statement

    • Anna Kozlova says:

      May 11, 2015

      John, 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

  5. Max says:

    August 3, 2015

    Hi,

    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, 2015

      Hi, 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}

  6. snowdream says:

    January 8, 2017

    I need a kotlin code generator!!!

  7. RK says:

    February 21, 2017

    How 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);
    }

    }

Subscribe

Subscribe for updates