stringintellij-idearefactoringstringbuilder

Refactor String concatenation to StringBuilder with intelliJ


I was designated to make refactoring on a project, and I came across this situation

 this.path = DESTINY + deploy.name() + FILE_SEPARATOR + delivery.getSystem().getCode()
      + FILE_SEPARATOR + delivery.getName() + FILE_SEPARATOR + delivery.getEnviroment().getName();

I want to refactor it to something like this:

StringBuilder tmpPath = new StringBuilder();
tmpPath.append(DESTINY);
tmpPath.append(deploy.name());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getSystem().getCode());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getName());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getEnviroment().getName());
this.path = tmpPath.toString();

Or

StringBuilder path = new StringBuilder();

...

this.path.append(DESTINY);
this.path.append(deploy.name());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getSystem().getCode());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getName());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getEnviroment().getName());

Whats is the fastest way to do that ?


Solution

  • Just hit Alt + Enter when having cursor on your original code:

    enter image description here

    Then you can choose the variant you prefer the most such as replacing it with String Builder. IDEA will do all the work for you.

    enter image description here