java - How to get the values on operand stack which the JVM instruction operates on with Javassist? -
javassist provides codeiterator editing code attribute, can used transverse instructions in method.
for jvm instruction, follows specification:   mnemonic operand1 operand2 ...  different binary assembly, stack-based jvm instructions takes value on operand stack. take ifge example. instruction has following format  if<cond> branchbyte1 branchbyte2  ifge succeeds if , if value on stack ≥ 0, branchbyte1 , branchbyte2 targets of jump.
my question is, can value on operand stack using javassist?
the answer javassist.bytecode.analysis module. according jvm specification, frame used store data , partial results. each frame has own array of local variables, own operand stack, , reference run-time const pool. 
in javassist.bytecode.analysis.frameprinter, funciton print shows how print each frame @ each instruction.
/**  * prints instructions , frame states of given method.  */ public void print(ctmethod method) {     stream.println("\n" + getmethodstring(method));     methodinfo info = method.getmethodinfo2();     constpool pool = info.getconstpool();     codeattribute code = info.getcodeattribute();     if (code == null)         return;      frame[] frames;     try {         frames = (new analyzer()).analyze(method.getdeclaringclass(), info);     } catch (badbytecode e) {         throw new runtimeexception(e);     }      int spacing = string.valueof(code.getcodelength()).length();      codeiterator iterator = code.iterator();     while (iterator.hasnext()) {         int pos;         try {             pos = iterator.next();         } catch (badbytecode e) {             throw new runtimeexception(e);         }          stream.println(pos + ": " + instructionprinter.instructionstring(iterator, pos, pool));          addspacing(spacing + 3);         frame frame = frames[pos];         if (frame == null) {             stream.println("--dead code--");             continue;         }         printstack(frame);          addspacing(spacing + 3);         printlocals(frame);     }  }   from code, can see:
the frames can acquired frames = (new analyzer()).analyze(method.getdeclaringclass(), info);
as values each instruction uses, should treat differently according specification of instruction.
Comments
Post a Comment