Designing bytecode with Bcel

Source: Internet
Author: User

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");
     }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.