public static void main(String[] args) { CodeAttributeComposer composer = new CodeAttributeComposer(); composer.beginCodeFragment(4); composer.appendInstruction(0, new SimpleInstruction(InstructionConstants.OP_ICONST_0)); composer.appendInstruction(1, new VariableInstruction(InstructionConstants.OP_ISTORE, 0)); composer.appendInstruction(2, new BranchInstruction(InstructionConstants.OP_GOTO, 1)); composer.beginCodeFragment(4); composer.appendInstruction(0, new VariableInstruction(InstructionConstants.OP_IINC, 0, 1)); composer.appendInstruction(1, new VariableInstruction(InstructionConstants.OP_ILOAD, 0)); composer.appendInstruction(2, new SimpleInstruction(InstructionConstants.OP_ICONST_5)); composer.appendInstruction(3, new BranchInstruction(InstructionConstants.OP_IFICMPLT, -3)); composer.endCodeFragment(); composer.appendInstruction(3, new SimpleInstruction(InstructionConstants.OP_RETURN)); composer.endCodeFragment(); } }
/** * Appends the given exception to the exception table. * @param exceptionInfo the exception to be appended. */ public CompactCodeAttributeComposer appendException(ExceptionInfo exceptionInfo) { codeAttributeComposer.appendException(exceptionInfo); return this; }
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) { if (branchTargetFinder.isSubroutineStart(offset)) { if (DEBUG) { System.out.println(" Replacing first subroutine instruction "+instruction.toString(offset)+" by a label"); } // Append a label at this offset instead of saving the subroutine // return address. codeAttributeComposer.appendLabel(offset); } else { // Append the instruction. codeAttributeComposer.appendInstruction(offset, instruction); } }
/** * Appends the code of the given code attribute. */ private void copyCode(Clazz clazz, Method method, CodeAttribute codeAttribute) { // The code may expand, due to expanding constant and variable // instructions. codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); // Copy the instructions. codeAttribute.instructionsAccept(clazz, method, this); // Append a label just after the code. codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); codeAttributeComposer.endCodeFragment(); }
new CodeAttribute(constantPoolEditor.addUtf8Constant(ClassConstants.ATTR_Code)); CodeAttributeComposer composer = new CodeAttributeComposer(); composer.reset(); composer.beginCodeFragment(0); composer.appendInstructions(instructions); if (firstInstructions != null) { for (Instruction instruction : firstInstructions) { composer.appendInstruction(instruction); composer.appendInstruction(lastInstruction); composer.endCodeFragment(); composer.visitCodeAttribute(programClass, method, codeAttribute);
EMPTY_ATTRIBUTES); CodeAttributeComposer codeAttributeComposer = new CodeAttributeComposer(); codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); codeAttributeComposer)); codeAttributeComposer.endCodeFragment(); codeAttributeComposer.visitCodeAttribute(targetClass, (Method)targetMember, newCodeAttribute);
codeAttributeComposer.reset(); codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); codeAttributeComposer.endCodeFragment(); codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);
codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); codeAttributeComposer.insertLineNumber(minimumLineNumberIndex, new ExtendedLineNumberInfo(0, METHOD_DUMMY_START_LINE_NUMBER, codeAttributeComposer.insertLineNumber(minimumLineNumberIndex, new ExtendedLineNumberInfo(codeAttribute.u4codeLength, INLINED_METHOD_END_LINE_NUMBER, codeAttributeComposer.endCodeFragment();
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) { // Add the instruction. codeAttributeComposer.appendInstruction(offset, instruction); }
/** * Appends the given label with the given old offset. * @param oldInstructionOffset the old offset of the label, to which * branches and other references in the current * code fragment are pointing. */ public void appendLabel(int oldInstructionOffset) { if (DEBUG) { println("["+codeLength+"] <- ", "[" + oldInstructionOffset + "] (label)"); } // Make sure the code and offset arrays are large enough. ensureCodeLength(codeLength + 1); // Remember the old offset of the following instruction. oldInstructionOffsets[codeLength] = oldInstructionOffset; // Fill out the new offset of the following instruction. instructionOffsetMap[level][oldInstructionOffset] = codeLength; }
private void println(String string1, String string2) { print(string1, string2); System.out.println(); }
public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute) { // Remap all local variable table entries. localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); // Remove local variables with empty code blocks. localVariableTableAttribute.u2localVariableTableLength = removeEmptyLocalVariables(localVariableTableAttribute.localVariableTable, localVariableTableAttribute.u2localVariableTableLength, codeAttribute.u2maxLocals); }
public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute) { // Remap all local variable table entries. localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); // Remove local variables with empty code blocks. localVariableTypeTableAttribute.u2localVariableTypeTableLength = removeEmptyLocalVariableTypes(localVariableTypeTableAttribute.localVariableTypeTable, localVariableTypeTableAttribute.u2localVariableTypeTableLength, codeAttribute.u2maxLocals); }
/** * Wraps up the current code fragment, continuing with the previous one on * the stack. */ public CompactCodeAttributeComposer endCodeFragment() { codeAttributeComposer.endCodeFragment(); return this; }
/** * Appends the given label with the given old offset. * @param oldInstructionOffset the old offset of the label, to which * branches and other references in the current * code fragment are pointing. */ public CompactCodeAttributeComposer appendLabel(int oldInstructionOffset) { codeAttributeComposer.appendLabel(oldInstructionOffset); return this; }
/** * Starts a new code fragment. Branch instructions that are added are * assumed to be relative within such code fragments. * @param maximumCodeFragmentLength the maximum length of the code that will * be added as part of this fragment (more * precisely, the maximum old instruction * offset or label that is specified, plus * one). */ public CompactCodeAttributeComposer beginCodeFragment(int maximumCodeFragmentLength) { codeAttributeComposer.beginCodeFragment(maximumCodeFragmentLength); return this; }
/** * Creates a new CompactCodeAttributeComposer. * @param allowExternalBranchTargets specifies whether branch targets * can lie outside the code fragment * of the branch instructions. * @param allowExternalExceptionHandlers specifies whether exception * handlers can lie outside the code * fragment in which exceptions are * defined. * @param shrinkInstructions specifies whether instructions * should automatically be shrunk * before being written. */ public CompactCodeAttributeComposer(ProgramClass targetClass, boolean allowExternalBranchTargets, boolean allowExternalExceptionHandlers, boolean shrinkInstructions) { constantPoolEditor = new ConstantPoolEditor(targetClass); codeAttributeComposer = new CodeAttributeComposer(allowExternalBranchTargets, allowExternalExceptionHandlers, shrinkInstructions); }
new CodeAttribute(constantPoolEditor.addUtf8Constant(ClassConstants.ATTR_Code)); CodeAttributeComposer composer = new CodeAttributeComposer(); composer.reset(); composer.beginCodeFragment(0); composer.appendInstructions(instructions); if (firstInstructions != null) { for (Instruction instruction : firstInstructions) { composer.appendInstruction(instruction); composer.appendInstruction(lastInstruction); composer.endCodeFragment(); composer.visitCodeAttribute(programClass, method, codeAttribute);
EMPTY_ATTRIBUTES); CodeAttributeComposer codeAttributeComposer = new CodeAttributeComposer(); codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength + 32); codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); ":0:0"; codeAttributeComposer.insertLineNumber( new ExtendedLineNumberInfo(0, 0, source)); codeAttributeComposer.endCodeFragment(); codeAttributeComposer.visitCodeAttribute(targetClass, (Method)targetMember, newCodeAttribute);
codeAttributeComposer.reset(); codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength); codeAttributeComposer.appendLabel(codeAttribute.u4codeLength); codeAttributeComposer.endCodeFragment(); codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);