You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by "Irving, Dave" <Da...@logicacmg.com> on 2004/08/09 09:40:38 UTC

Exception Handling

Hi,

I've used BCEL a while ago to create **new** classes on the fly, but am
getting stuck on something I've never had to do before: Inserting
instructions to existing classes.
Im trying to wrap each method in a class with a
try/catch(NullPointerException).
So this method:

public int getInt() {
  return getIntFromSomewhere();
}

would become:

public int getInt() {
  try {
    return getIntFromSomewhere();
  }
  catch(NullPointerException e) {
    // do something
    return 0;
  }
}

Im not having much luck at the moment - my current approach is this:

1) Use the methods InstructionList.getStart() to get the first instruction
2) Use InstructionList.getEnd() to get the last instruction
3) Add exception handling code (keeping an InstructionHandler to the first
instruction in the handling code)
4) Add an appropriate return (using InstructionFactory.createNull ) if
return type is not void
5) Add an exception handler to the method, using start and end obtained from
1/2 above, and the instruction handle obtained in 3 as the exception
handling code handle

Does this seem like a reasonable approach? Currently the methods produced
seem to be completely mangled - so Im guessing Im way out on something -
maybe one of my assumptions above.
Any advice would be greatly appreciated!

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Exception Handling

Posted by Andrew Huntwork <as...@huntwork.net>.
yeah, that looks generally fine.  maybe you could post the code that 
adds the exceptions and/or the "mangled" bytecode

Irving, Dave wrote:
> Hi,
> 
> I've used BCEL a while ago to create **new** classes on the fly, but am
> getting stuck on something I've never had to do before: Inserting
> instructions to existing classes.
> Im trying to wrap each method in a class with a
> try/catch(NullPointerException).
> So this method:
> 
> public int getInt() {
>   return getIntFromSomewhere();
> }
> 
> would become:
> 
> public int getInt() {
>   try {
>     return getIntFromSomewhere();
>   }
>   catch(NullPointerException e) {
>     // do something
>     return 0;
>   }
> }
> 
> Im not having much luck at the moment - my current approach is this:
> 
> 1) Use the methods InstructionList.getStart() to get the first instruction
> 2) Use InstructionList.getEnd() to get the last instruction
> 3) Add exception handling code (keeping an InstructionHandler to the first
> instruction in the handling code)
> 4) Add an appropriate return (using InstructionFactory.createNull ) if
> return type is not void
> 5) Add an exception handler to the method, using start and end obtained from
> 1/2 above, and the instruction handle obtained in 3 as the exception
> handling code handle
> 
> Does this seem like a reasonable approach? Currently the methods produced
> seem to be completely mangled - so Im guessing Im way out on something -
> maybe one of my assumptions above.
> Any advice would be greatly appreciated!
> 
> This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org