用BCEL设计字节码 - 直接在原方法上加指令
时间:2011-01-19 BlogJava Frank_Fang
前一个是直接生成了一个一个方法对原方法进行调用,现在直接在原方法上进行修改
即将
public class StringBuilder
{
private String buildString(int length) {
String result = "";
for (int i = 0; i < length; i++) {
result += (char)(i%26 + ''a'');
}
System.out.println(result);
return result;
}
public static void main(String[] argv) {
StringBuilder inst = new StringBuilder();
for (int i = 0; i < argv.length; i++) {
String result = inst.buildString(Integer.parseInt(argv[i]));
System.out.println("Constructed string of length " +
result.length());
}
}
}
改成:
public class StringBuilder
{
private String buildString(int length) {
System.out.println("start start start");
long starttime = System.currentTimeMillis();
String result = "";
for (int i = 0; i < length; i++) {
result += (char)(i%26 + ''a'');
}
System.out.println(result);
System.out.println("Call to buildString$impl took " +
(System.currentTimeMillis()-starttime) + " ms.");
System.out.println("end end end end");
return result;
}
public static void main(String[] argv) {
StringBuilder inst = new StringBuilder();
for (int i = 0; i < argv.length; i++) {
String result = inst.buildString(Integer.parseInt(argv[i]));
System.out.println("Constructed string of length " +
result.length());
}
}
}
喘BCEL譜柴忖准鷹 - 岷俊壓圻圭隈貧紗峺綜(2)
扮寂:2011-01-19 BlogJava Frank_Fang
俐個旗鷹泌和?
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionConstants;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.InvokeInstruction;
i
|