This is the nearest API conversion issue
This requires scanning the source code to find the method call for this class, and then adding instructions before and after the method call
The code will be converted to the following form:
public class StringBuilder
{
public String buildString(int length) {
String result = "";
for (int i = 0; i < length; i++) {
result += (char)(i%26 + 'a');
}
return result;
}
public static void main(String[] argv) {
StringBuilder inst = new StringBuilder();
for (int i = 0; i < argv.length; i++) {
long start = System.currentTimeMillis();
String result = inst.buildString(Integer.parseInt(argv[i]));
System.out.println("Call to buildString$impl took " +
(System.currentTimeMillis()-start) + " ms.");
System.out.println("Constructed string of length " +
result.length());
}
}
}
The conversion code is as follows:
Scan the various methods in this class first
I'm going to change the build strategy, do not write their own instructions, this write their own instructions more trouble, but the code to add more than a few more error-prone, when the added code has if, jump statements, they will not be how to write the instructions, this writing similar assembly instructions is always very troublesome.
First of all, the addition of the code in a class in advance to write the method, instance method or static method, method with parameters or return value,
These parameters and return values are used to replace and use local variables in the calling method.
Then add the code to the method before and after calling these already written methods of the bytecode of the sequence of instructions generated instructionlist, and then add to the instruction sequence using this method
As follows
public class ToolUtil {
public static long printStart() {
System.out.println("start start start start");
long start = System.currentTimeMillis();
return start;
}
public static void printEnd(String methodname,long start){
System.out.println("Call to "+methodname+" took " +
(System.currentTimeMillis()-start) + " ms.");
System.out.println("end end end end end end");
}
}