用 BCEL 设计字节码: 直接在原方法上加指令
2009-09-23 00:00:00 来源:WEB开发网前一个是直接生成了一个一个方法对原方法进行调用,现在直接在原方法上进行修改
即将
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());
}
}
}
更多精彩
赞助商链接