Lars Olsson | 18 Mar 2002 16:03
Picon

Finding instructions in class file...

Hello!

I am trying to write a program that finds and processes all invoke instructions
in a class file. My idea is to write something like this:

   public static void main(String[] args) throws Exception {
     ClassParser parser = new ClassParser(args[0]);
     JavaClass jclcass = parser.parse();
     Method[] amethod = jclcass.getMethods();
     for (int i = 0 ; i < amethod.length ; i++) {
       Code code = amethod[i].getCode();
       InstructionList instrlist = MAGIG(code);
       Instruction[] ainstr = instrlist.getInstructions();
       for (int j = 0 ; j < ainstr.length ; j++) {
         if (ainstr[j] instanceof InvokeInstruction) {
           ...
         }
       }
     }
   }

I have not been able to figure out how to write the method MAGIC()
which takes a Code instance and returns the set of instructions
(in the form of an InstructionList) contained in that byte code chunk.

I could try to copy the code found in Utility.codeToString() but before
I do that I would like to know if there is a better (== easier) way.

Thanks,

(Continue reading)

Juozas Baliuka | 18 Mar 2002 17:29
Picon

Re: Finding instructions in class file...

Hi,
I think you can use Visitor to traverse instructions, it is more simple.

> Hello!
>
> I am trying to write a program that finds and processes all invoke
instructions
> in a class file. My idea is to write something like this:
>
>    public static void main(String[] args) throws Exception {
>      ClassParser parser = new ClassParser(args[0]);
>      JavaClass jclcass = parser.parse();
>      Method[] amethod = jclcass.getMethods();
>      for (int i = 0 ; i < amethod.length ; i++) {
>        Code code = amethod[i].getCode();
>        InstructionList instrlist = MAGIG(code);
>        Instruction[] ainstr = instrlist.getInstructions();
>        for (int j = 0 ; j < ainstr.length ; j++) {
>          if (ainstr[j] instanceof InvokeInstruction) {
>            ...
>          }
>        }
>      }
>    }
>
> I have not been able to figure out how to write the method MAGIC()
> which takes a Code instance and returns the set of instructions
> (in the form of an InstructionList) contained in that byte code chunk.
>
> I could try to copy the code found in Utility.codeToString() but before
(Continue reading)

m.dahm | 18 Mar 2002 16:15
Picon

AW: Finding instructions in class file...

>    public static void main(String[] args) throws Exception {
>      ClassParser parser = new ClassParser(args[0]);
>      JavaClass jclcass = parser.parse();
>      Method[] amethod = jclcass.getMethods();
>      for (int i = 0 ; i < amethod.length ; i++) {
>        Code code = amethod[i].getCode();
>        InstructionList instrlist = MAGIG(code);
>        Instruction[] ainstr = instrlist.getInstructions();
>        for (int j = 0 ; j < ainstr.length ; j++) {
>          if (ainstr[j] instanceof InvokeInstruction) {
>            ...
>          }
>        }
>      }
>    }

How about new InstructionList(code.getCode()) ...?

It's just a RTFM question :)

Cheers
	Markus
Stephen Colebourne | 29 Mar 2002 19:20
Picon

Instantiating an abstract class

Hi,
I would like to be able to define a class as abstract so I only have to
define the method signatures. (BCEL would then be used to generate the
implementations). However, I would also like to be able to instantiate the
class using the standard new operator (This must work in a standard IDE,
like Eclipse). From what I have seen so far its impossible (I will have to
use a factory), but have I missed something? Any other bright ideas?

Stephen

Gmane