Skip to content
Advertisement

How to configure Intellij toString() template to align with Eclipse

Recently we moved to IntelliJ and using it for JAVA, earlier we were using Eclipse, we are able to configure the IntelliJ code format setting same as Eclipse except for toString().

toString() output in POJO is very different from eclipse as shown below,

toString() in IntelliJ

    @Override 
    public String toString() {
        return "Entity{" +
            "entityName='" + entityName + ''' +
            ", isBaseEntity=" + isBaseEntity +
            ", entityAttribute=" + entityAttribute +
            '}';
    }

toString() in Eclipse

    @Override
    public String toString() {
        return "Entity [entityName=" + entityName + ", isBaseEntity=" + isBaseEntity
            + ", entityAttribute=" + entityAttribute;
    }

Question

Is there any way/setting through which I can make IntelliJ toString() same as eclipse?

Advertisement

Answer

Code > Generate… > toString()

On the dialog that opens, in the top right, there is a button labelled ‘Settings…’. You can specify a template. There are 11 default templates. You can add your own to generate whatever you want.

toString generate dialog

Better yet, use Lombok to generate your toString. It’s opinionated about the format, but I much prefer that to having my code littered with boilerplate. It also automatically updates if you add a field to the class, which an IDE generated method will not do.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement