Re: InstructionHandle to Declared Exceptions

Arrin Daley <[email protected]> Thu, 06 Dec 2007 09:54:22 +1100
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
Hi Elliot

You could try using 'Repository.loadClass' to load a class (which should 
use your existing class loading hierarchy) this will produce a 
'JavaClass' object from which you will be able to create a 'ClassGen' to 
get a 'MethodGen', or create a 'MethodGen' from the 'Method' 
(JavaClass.getMethods() then search for name or signature) your 
concerned with. From there 'MethodGen' has a method 'getExceptions()' 
which returns an array of Strings for the exception names. I think 
Repository uses the normal java class loading mechanisms so if these 
were causing your troubles this may still be the case if so have a look 
at the interface 'org.apache.bcel.util.Repository' implementations of 
which allow you to specify a classloader or generally have other options.

Hope this helps

Bye Arrin

Elliot Barlas wrote:

>I have an InstructionHandle containing an InvokeVirtual
>instruction and I would like to get the list of all
>declared exceptions thrown by the virtual method
>invocation.  I have tried the following:
>
>1. Get the InvokeVirtual instruction from handle
>2. Get the ReferenceType from the instruction
>3. Cast the ReferenceType to an ObjectType when safe to
>do so.
>4. Get the class name from the ObjectType
>5. Get the Class object from the class name via
>Class.forName(className)
>6. Get an array of methods for the Class
>7. Get the method name from the InvokeVirtual instruction
>8. Find the java.lang.reflect.Method in the array with
>the given name
>9. Get an array of exception Classes from the method
>
>This should be sufficient, but in the environment that I
>am working in, there are multiple class loaders and the
>class lookup (Class.forName) is failing.  Does anyone
>know how to do something equivalent in BCEL alone without
>requiring the use of Java reflection or class lookups?
>
>Thanks,
>Elliot
>
>  
>

-- 
Conventional wisdom says to know your limits. To know your limits you 
need to find them first. Finding you limits generally involves getting
in over your head and hoping you live long enough to benefit from the 
experience. That's the fun part.