akshattandon / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

@Data add toString with + concatenation (use StringBuilder instead +) #782

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please discuss feature requests first on
https://groups.google.com/forum/#!forum/project-lombok

What steps will reproduce the problem?
1. @Data generate the right toString but use + concatenation instead of 
StringBuilder

What is the expected output? What do you see instead?
I expect toString generated by @Data use StringBuilder instead of + 
concatenation

What version of the product are you using? On what operating system?
- 1.16.0 on Linux ubuntu and JDK 1.8

Original issue reported on code.google.com by amine.bouhamidi on 5 Feb 2015 at 7:08

GoogleCodeExporter commented 9 years ago
If you look at the bytecode you will notice that only one StringBuilder is 
created in the toString method.

The java compiler will generate this for you if you concatenate String objects 
in one expression.

The code 
   return "foo" + x + ":" + y.toString() + " = " + (x == null);
will result in (nearly) the same bytecode as
   return new StringBuilder().append("foo").append(x).append(":").append(y.toString()).append(" = ").append(x == null).toString();

If you introduce if-s and for-s, it is a different story.

So unless you can prove the bytecode is wrong we're going to assume lombok does 
the right thing.

Original comment by r.spilker on 5 Feb 2015 at 11:12