6

I got a lot of entities, 160 to be specific. I need to override toString method in all of them. My question is: Is there some shortcut on Intellij or some external tool where I can auto generate toString method in all of those entities?

KiKo
  • 1,668
  • 7
  • 27
  • 56

2 Answers2

6

In Intellij i supposed it can be done like this :

  1. Open the desired class for editing and do one of the following:

    • On the main menu, choose Code | Generate.
    • Right-click the editor and choose Generate on the context menu
    • Press Alt+Insert.
  2. From the pop-up list that shows up, select toString() option. Generate toString() wizard displays the list of fields in the class.

In eclipse it could be done aswell :

  1. Right click the editor
  2. Select Source
  3. Then Select generate toString().
  4. Then select all the fields you want.
Karl Alexander
  • 351
  • 4
  • 16
0

A little bit late, but the easiest way is to add Project Lombok to your project and then annotate all the entities with @ToString annotation.

Simple example:

import lombok.ToString;

@ToString
public class Account {

    private String id;

    private String name;

    // standard getters and setters or you can use @Getter and @Setter form lombok too
}

Here is the result when you call the toString method:

Account(id=12345, name=An account)
KiKo
  • 1,668
  • 7
  • 27
  • 56