Question about branch instruction targets

"Mark Reynolds" <[email protected]> Tue, 18 Mar 2008 18:47:34 -0400
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
I'm trying to get the target of a branch instruction, but it always gives me 
a null
instruction handle.  The code is very simple:

int fnd = (-1);
JavaClass       clazz   = Repository.lookupClass("test");
Method[]        methods = clazz.getMethods();
 // find method "hello"
for(int i = 0;i < methods.length;i++) {
          System.out.println("Method[" + i + "] = " + methods[i]);
          String s = methods[i].getName();
          System.out.println("Name is " + s);
          if ( s.equals("hello") ) fnd = i;
         }
if ( fnd >= 0 ) {
    ConstantPool cp = clazz.getConstantPool();
    methods[fnd].setConstantPool(cp);  // try setting constant pool
    Code c = methods[fnd].getCode();
    c.setConstantPool(cp);  // try again
    byte[] b = c.getCode();
    InstructionList il = new InstructionList(b);
    il.setPositions(true);
    Instruction ii[] = il.getInstructions();
    for(int w=0;w<ii.length;w++) {
          System.out.println("Instruction[" + w + "] = " + ii[w]);
          if ( ii[w] instanceof BranchInstruction ) {
               InstructionHandle ih = 
((BranchInstruction)(ii[w])).getTarget();
               if ( ih != null )
                     System.out.println("   Branch " + ih.getPosition());
          }
     }
}

and yet it fails every time. Clearly I am doing something wrong. Can someone 
please
give me a clue?

Thanks,

Mark