[Soot-list] StringBuffer or StringBuilder when performing string concatenation?

Jinping Zhu jnuzhjp at gmail.com
Sat May 1 14:44:43 EDT 2010


Hi, Eric:
     When I tried to examine the jimple code of java string concatenation
through "+", as to code below:
    public void experiment(boolean condition){
     String str = "demo";
     str = str + "start";
     if(condition) {
      str = str + "condition true";
     } else{
      str = str + "condition false";
     }
     str = str + "end";
    }
it produced jimp like this:
    public void experiment(boolean)
    {
        Test this;
        boolean condition;
        java.lang.String str, temp$1, temp$3, temp$5;
        java.lang.StringBuffer temp$0, temp$2, temp$4, temp$6;
        this := @this;
        condition := @parameter0;
        str = "demo";
        temp$0 = new java.lang.StringBuffer;
        specialinvoke temp$0.<init>();
        temp$0.append(str);
        temp$0.append("start");
        temp$1 = temp$0.toString();
        if condition == 0 goto label0;
        temp$2 = new java.lang.StringBuffer;
        specialinvoke temp$2.<init>();
        temp$2.append(temp$1);
        temp$2.append("condition true");
        temp$3 = temp$2.toString();
        str = temp$3;
        goto label1;
     label0:
        temp$4 = new java.lang.StringBuffer;
        specialinvoke temp$4.<init>();
        temp$4.append(temp$1);
        temp$4.append("condition false");
        temp$5 = temp$4.toString();
        str = temp$5;
     label1:
        temp$6 = new java.lang.StringBuffer;
        specialinvoke temp$6.<init>();
        temp$6.append(str);
        temp$6.append("end");
        temp$6.toString();
        return;
    }
It's obviously that string concatenation through "+" will be replaced a
sequence of code like creating a new StringBuffer and utilizing its append
to concatenate the pieces, in order to improve performance.
Well, the problem was that when I try to find out any expression
like" <java.lang.StringBuffer: java.lang.StringBuffer
append(java.lang.String)>" by using the equals() of String. The result
turned out none of the above expressions found while actually there were
serveral.
When I dubugged the program, I discovered that object actually was
StringBuilder instead of StringBuffer, so the signature became
<java.lang.StringBuilder: java.lang.StringBuilder
append(java.lang.String)>.So by comparing the signature I could not find any
matches.
Any explanation or suggestion will be greatly appreciated.
Kelvin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.cs.mcgill.ca/pipermail/soot-list/attachments/20100502/29c39ba3/attachment.html 


More information about the Soot-list mailing list