Elliot Barlas | 5 Dec 20:38 2007

InstructionHandle to Declared Exceptions

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
Arrin Daley | 5 Dec 23:54 2007
Picon
Picon

Re: InstructionHandle to Declared Exceptions

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
(Continue reading)

Elliot Barlas | 6 Dec 00:02 2007

RE: InstructionHandle to Declared Exceptions

Thanks Arrin, this worked perfectly.  The Repository
class was the missing link.  I found that in the
environment I am working in the classes loaded by BCEL
are not in the classpath, which is why I was not able the
use Class.forName, but the Repository provides equivalent
functionality for my purposes.

-Elliot

-----Original Message-----
From: Arrin Daley [mailto:arrin.daley <at> anu.edu.au] 
Sent: Wednesday, December 05, 2007 2:54 PM
To: BCEL Users List
Subject: Re: InstructionHandle to Declared Exceptions

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'
(Continue reading)

uprime uprime812 | 31 Dec 11:52 2007
Picon

problem with class2html

Hello,

I'm new to bcel, and has this problem when I try to convert using
class2html. This is my class:

-------------
package trybcel ;

class A {
  private A nxt = null ;
  private int x = 0 ;

  boolean m(){ return x==0 ; }
}
-----------

After compiling (success), I call Class2HTML and gets this error, which I
thinks rather wierd:

X:\tmp\trybcel>java -cp ..\bcel-5.2\bcel-5.2.jar
org.apache.bcel.util.Class2HTML
A.class
Processing A.class...java.lang.ArrayIndexOutOfBoundsException: -1
java.lang.ArrayIndexOutOfBoundsException: -1
at org.apache.bcel.util.CodeHTML.writeMethod(CodeHTML.java:520)
at org.apache.bcel.util.CodeHTML.<init>(CodeHTML.java:63)
at org.apache.bcel.util.Class2HTML.<init>(Class2HTML.java:92)
at org.apache.bcel.util.Class2HTML.main(Class2HTML.java:136)

Any help will be appreciated!
(Continue reading)

Erik Bengtson | 31 Dec 13:42 2007

RE: problem with class2html

BCEL SVN trunk and Sun JDK 1.5 works for me

-----Message d'origine-----
De : uprime uprime812 [mailto:uprime812 <at> gmail.com] 
Envoyé : lundi 31 décembre 2007 11:53
À : bcel-user <at> jakarta.apache.org
Objet : problem with class2html

Hello,

I'm new to bcel, and has this problem when I try to convert using
class2html. This is my class:

-------------
package trybcel ;

class A {
  private A nxt = null ;
  private int x = 0 ;

  boolean m(){ return x==0 ; }
}
-----------

After compiling (success), I call Class2HTML and gets this error, which I
thinks rather wierd:

X:\tmp\trybcel>java -cp ..\bcel-5.2\bcel-5.2.jar
org.apache.bcel.util.Class2HTML
A.class
(Continue reading)

uprime uprime812 | 31 Dec 14:19 2007
Picon

is BCEL a good choice?

Hello again,

I'm working on a commercial tool where I have to build some byte code
modification capability. I
need a byte code engineering library with features like:

   * offer a reasonable set of utilities
   * not buggy
   * allows byte code to be manipulated abstractly
   * free, even for commercial use :) (me being a poor starting enterpreneur
:)

Is BCEL a good choice? Or does someone has other suggestions?

Thanks,

W.P.
uprime uprime812 | 31 Dec 14:11 2007
Picon

Re: problem with class2html

Thanks, I'll try that.

On 12/31/07, Erik Bengtson <erik <at> jpox.org> wrote:
>
> BCEL SVN trunk and Sun JDK 1.5 works for me
>
> -----Message d'origine-----
> De: uprime uprime812 [mailto:uprime812 <at> gmail.com]
> Envoyé: lundi 31 décembre 2007 11:53
> À: bcel-user <at> jakarta.apache.org
> Objet: problem with class2html
>
> Hello,
>
> I'm new to bcel, and has this problem when I try to convert using
> class2html. This is my class:
>
> -------------
> package trybcel ;
>
> class A {
>   private A nxt = null ;
>   private int x = 0 ;
>
>   boolean m(){ return x==0 ; }
> }
> -----------
>
> After compiling (success), I call Class2HTML and gets this error, which I
> thinks rather wierd:
(Continue reading)

simon | 31 Dec 15:14 2007
Picon

Re: is BCEL a good choice?


On Mon, 2007-12-31 at 14:19 +0100, uprime uprime812 wrote:
> Hello again,
> 
> I'm working on a commercial tool where I have to build some byte code
> modification capability. I
> need a byte code engineering library with features like:
> 
>    * offer a reasonable set of utilities
>    * not buggy
>    * allows byte code to be manipulated abstractly
>    * free, even for commercial use :) (me being a poor starting enterpreneur
> :)
> 
> Is BCEL a good choice? Or does someone has other suggestions?

BCEL should match your above requirements.

A very well respected alternative is asm.objectweb.org
Erik Bengtson | 31 Dec 21:26 2007

RE: is BCEL a good choice?

I've been using BCEL for several years. It's certainly not buggy and the API
is very simple. BCEL source code is also very easy to understand, so you can
easily create patches.

However there is not a lot of activity in the project to support newer java
spec versions. I've tried up to JDK 1.5 features and BCEL works pretty well.

ASM is a good alternative if you need a faster byte code manipulation tool.

-----Message d'origine-----
De : uprime uprime812 [mailto:uprime812 <at> gmail.com] 
Envoyé : lundi 31 décembre 2007 14:19
À : BCEL Users List
Objet : is BCEL a good choice?

Hello again,

I'm working on a commercial tool where I have to build some byte code
modification capability. I
need a byte code engineering library with features like:

   * offer a reasonable set of utilities
   * not buggy
   * allows byte code to be manipulated abstractly
   * free, even for commercial use :) (me being a poor starting enterpreneur
:)

Is BCEL a good choice? Or does someone has other suggestions?

Thanks,
(Continue reading)


Gmane