Search code examples
javaexceptionbytecodejava-bytecode-asm

How to implement "throws InterrruptedException" with ASM?


I'm using the Java ASM Byte Code Library to generate byte code for the following method body:

public void SearchOrSort(MyList<Integer> list) throws InterruptedException {

    synchronized (this) {
        this.wait();
    }
    Thread.sleep(2000);
    
}

Using ASM to print the byte code of the method shown above, I got the output on the attached picture. (left column)

attached picture

The right column shows the source code I've created based on the printed byte code.

I'm struggling to implement the Label 3, 5 and 4.


Solution

  • "Frame full" and "chop" are part of the StackMapTable attribute. In most cases, you don't have to worry about those - ASM can generate them automatically for you.

    As for the label of the goto, that's just whatever label you used for the code you want to jump to. It's the same as any other control flow in that respect. Incidentally, you'll want to store your ASM labels in variables so you can reference them in later control flow instructions.

    The only complication in this case is that the exception handler is autogenerated, rather than part of the code you wrote. In Java synchronized blocks are compiled with a secret exception handler around the whole thing.