InstructionHandle last = il.getEnd(); il.setPositions(); il.setPositions(); last = il.getEnd(); instructionListSize =
il.setPositions(true);
public void setPositions() { setPositions(false); }
public void setPositions() { // TODO could be package-protected? (some test code would need to be repackaged) setPositions(false); }
/** * When everything is finished, use this method to convert the instruction * list into an array of bytes. * * @return the byte code ready to be dumped */ public byte[] getByteCode() { // Update position indices of instructions setPositions(); ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(b); try { for(InstructionHandle ih=start; ih != null; ih = ih.next) { Instruction i = ih.instruction; i.dump(out); // Traverse list } } catch(IOException e) { System.err.println(e); return null; } return b.toByteArray(); }
/** * When everything is finished, use this method to convert the instruction list into an array of bytes. * * @return the byte code ready to be dumped */ public byte[] getByteCode() { // Update position indices of instructions setPositions(); final ByteArrayOutputStream b = new ByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(b); try { for (InstructionHandle ih = start; ih != null; ih = ih.getNext()) { final Instruction i = ih.getInstruction(); i.dump(out); // Traverse list } out.flush(); } catch (final IOException e) { System.err.println(e); return new byte[0]; } return b.toByteArray(); }
JavaClass target; try { target = Repository.lookupClass("Target"); } catch (final ClassNotFoundException ex) { throw new RuntimeException("unable to resolve Target", ex); } final ClassGen targetGen = new ClassGen(target); final ConstantPoolGen pool = targetGen.getConstantPool(); final ConstantMethodref ref = (ConstantMethodref) pool.getConstant( pool.lookupMethodref("Name", "getName", "()Ljava/lang/String;")); ref.setClassIndex(pool.lookupClass("Target")); ref.setNameAndTypeIndex(pool.addNameAndType("$Name$getName", "()Ljava/lang/String;")); final InstructionList code = new InstructionList(); final InstructionFactory factory = new InstructionFactory(targetGen, pool); code.append(factory.createConstant("overriden-name")); code.append(factory.createReturn(Type.STRING)); code.setPositions(); final MethodGen methodGen = new MethodGen( Constants.ACC_PRIVATE | Constants.ACC_SYNTHETIC | Constants.ACC_STATIC, Type.STRING, new Type[0], new String[0], "$Name$getName", "Target", code, pool); methodGen.setMaxLocals(0); methodGen.setMaxStack(1); targetGen.addMethod(methodGen.getMethod()); try { targetGen.getJavaClass().dump("Target.class"); } catch (final IOException ex) { throw new RuntimeException("unable to save Target", ex); }
InstructionHandle last = il.getEnd(); il.setPositions(); il.setPositions(); last = il.getEnd(); instructionListSize =
InstructionHandle last = il.getEnd(); il.setPositions(); il.setPositions(); last = il.getEnd(); instructionListSize =
InstructionHandle last = il.getEnd(); il.setPositions(); il.setPositions(); last = il.getEnd(); instructionListSize =
list.setPositions(); mg.setMaxLocals(); mg.setMaxStack();
mg.getInstructionList().setPositions(); mg.setMaxLocals(); mg.setMaxStack();
list.setPositions(); mg.setInstructionList(list); mg.setMaxLocals();
list.setPositions(true); InstructionHandle lastInstr = list.getEnd(); InstructionHandle secondToLastInstr = lastInstr.getPrev();
list.setPositions(true); InstructionHandle lastInstr = list.getEnd(); InstructionHandle secondToLastInstr = lastInstr.getPrev();
instructionList.setPositions(true);