java - By analyzing the bytecode, how can I detect explicit throw statement invocations from within a catch block? -
i want detect throw statements occur within catch block. instance:
try { def(); } catch (ioexception e) { throw e; } catch (exception e) { throw new runtimeexception(e); }
first, using eclipse-jdt detect these cases , quite simple, since traverse abstract syntax tree.
now have use framework (bat - bytecode analysis toolkit) deals directly bytecode.
first of all, how catch block represented in bytecode? , how can detect throw statement within it?
each method has exception table maps range of instructions plus , exception type exception handler (it's entry point). not easy translate java code. in general, need examine table , analyse reachable code entry points. of code belongs catch
clause. it's matter of identifying athrow
instructions.
use javap
or other bytecode visualizer play around , understand better. completing code, compiling it, , subjecting javap
produces:
public class test extends java.lang.object{ public test(); code: 0: aload_0 1: invokespecial #1; //method java/lang/object."<init>":()v 4: return public static void def() throws java.io.ioexception; code: 0: new #2; //class java/io/ioexception 3: dup 4: invokespecial #3; //method java/io/ioexception."<init>":()v 7: athrow public static void main(java.lang.string[]) throws java.io.ioexception; code: 0: invokestatic #4; //method def:()v 3: goto 19 6: astore_1 7: aload_1 8: athrow 9: astore_1 10: new #6; //class java/lang/runtimeexception 13: dup 14: aload_1 15: invokespecial #7; //method java/lang/runtimeexception."<init>":(ljava/lang/throwable;)v 18: athrow 19: return exception table: target type 0 3 6 class java/io/ioexception 0 3 9 class java/lang/exception }
for method main
have 2 exception entry points ("targets"): 6 , 9. following 6, have athrow
@ offset 8. following entry point @ 9, have athrow
@ offset 18. that's it!
Comments
Post a Comment