This content has been marked as final.
Show 2 replies
-
1. Re: try finally around a method
sanjayvacharya Sep 14, 2008 11:53 AM (in response to sanjayvacharya)I can accomplish the above by doing an :
ctMethod.insertAfter("System.out.println(\"After Foo\"", true).
and then an:ctMethod.insertBefore("System.out.println(\"Before Foo\"");
However I cannot seem to access a local variable in the finally for example:long start = System.currrentTimeMillis(); System.out.println("Before Foo"); try { System.out.println("Foo"); } finally { long end = System.currentTimeMillis(); System.out.println("Foo too:" + (end - start)); }
The problem is when I am altering the method for the finally, I cannot access the start variable I introduced.
Any tips would be welcomed. -
2. Re: try finally around a method
sanjayvacharya Sep 14, 2008 12:43 PM (in response to sanjayvacharya)I thing I figured it out. I did the following:
final String beforeMethod = "{long startTime = System.currentTimeMillis(); System.out.println(\"Before Foo\");"; final String afterMethod = "finally {long diff = System.currentTimeMillis() - startTime; System.out.println(\"Foo completed in:\" + diff);}}"; mold.instrument( new ExprEditor() { public void edit(MethodCall m) throws CannotCompileException { m.replace(beforeMethod + " try {$_ = $proceed($$); } " + afterMethod); } });