yes you can, mostly. Check out disassembly for an empty catch block:

public void testFoo();
Signature: ()V
Code:
0: aload_0
1: ifnull 16
4: new #16; //class java/lang/RuntimeException
7: dup
8: invokespecial #17; //Method java/lang/RuntimeException."<init>":()V
11: athrow
12: goto 16
15: astore_1
16: return
Exception table:
from to target type
0 15 15 Class java/lang/RuntimeException

Here's the source code:
public void testFoo() {
try { if(this != null) throw new RuntimeException(); }
catch(RuntimeException e) {}
}

You can find empty catch blocks by finding exception ranges where the last
instruction targets the instruction after the handler instruction and where
the handler instruction is either astore or pop. In this example, the
process would be as follows: look at the exception handler table. notice
that an exception range ends at 15, which means the last instruction in the
range is 12: goto 16 (since the range is exclusive of the range end). Then
look at the handler instruction: 15: astore_1. That's a potential empty
catch block instruction, so look at the following instruction 16: return.
Notice that this instruction is targeted by 12: goto 16, and conclude that
the catch block is empty.

Use the MethodGen.getExceptionHandlers method to get the list of exception
ranges and their handlers in the form of CodeExceptionGen objects.

A more general method of finding empty catch blocks would involve more
complicated control flow analysis, but this should at least get you started.

On 11/2/05, Saravanan Bellan <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
> Is it possible to detect empty catch blocks using BCEL and insert
> instructions for those catch blocks?
>
> Thanks,
>
>

Reply via email to