Example usage for org.eclipse.jdt.internal.compiler.codegen CodeStream init

List of usage examples for org.eclipse.jdt.internal.compiler.codegen CodeStream init

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.codegen CodeStream init.

Prototype

public void init(ClassFile targetClassFile) 

Source Link

Usage

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.bytecode.BytecodeTransformer.java

License:Open Source License

public void doCopyMethodCode(RoleModel srcRole, MethodBinding srcMethodBinding, SourceTypeBinding dstType,
        AbstractMethodDeclaration dstMethod, byte[] srcConstantPool, int[] constantPoolOffsets, int offset,
        ConstantPoolObjectReader reader, ConstantPoolObjectMapper mapper, ClassFile classFile) {
    this._reader = reader;
    this._mapper = mapper;
    this._writer = new ConstantPoolObjectWriter(classFile);

    int codeAttributeOffset = findCodeAttribute(srcConstantPool, offset);
    int codeLength = OTByteCodes.getInt(srcConstantPool, codeAttributeOffset + 10);
    int tailOffset = codeAttributeOffset + CODE_ATTR_PREFIX_LEN + codeLength;

    // calculate size of attributes:
    int attributesLen = computeAttributesLen(srcConstantPool, offset);

    // copy method code:
    int totalLen = METHOD_PREFIX_LEN + attributesLen;
    byte[] methodCode = new byte[totalLen];
    System.arraycopy(srcConstantPool, offset, methodCode, 0, totalLen);

    // keep this offset for updating the stored value after adjustTail():
    int copyInhSrcLineOffsetOffset;
    // first phase of adjustment (method prefix and attributes except code):
    copyInhSrcLineOffsetOffset = copyAdjustStructure(classFile,
            constantPoolOffsets == null/* www.  ja  v a2  s .c  om*/
                    ? ConstantPoolSimpleConverter.create(srcRole, srcMethodBinding, methodCode, classFile)
                    : new ConstantPoolSimpleConverter(srcConstantPool, constantPoolOffsets, offset, methodCode,
                            classFile),
            srcConstantPool, offset, methodCode, dstMethod.binding, totalLen);

    codeAttributeOffset -= offset; // now relative to methodCode
    tailOffset -= offset;

    if (copyInhSrcLineOffsetOffset == -1) {
        // CopyInheritanceSrc added when copying a method for the first time
        byte[] extraAttr = generateCpInhSrc(dstMethod.model, classFile);

        if (extraAttr != null) {
            System.arraycopy(methodCode, 0, methodCode = new byte[totalLen + extraAttr.length], 0, totalLen);
            System.arraycopy(extraAttr, 0, methodCode, totalLen, extraAttr.length);
            incrementWord(methodCode, 0, 6, 1); // attributes_count++

            copyInhSrcLineOffsetOffset = totalLen + 8; // offset of lineOffset within CopyInheritanceSrc attr.
        }
    }
    if (dstMethod.isTSuper)
        incrementWord(methodCode, codeAttributeOffset, 8, 1); // max_locals++

    CodeStream codeStream = classFile.codeStream;
    codeStream.init(classFile); // a weaker reset (we have no AbstractMethodDeclaration)
    int newMethodOffset = classFile.contentsOffset;

    boolean isCtorAddingMarkArg = srcMethodBinding.isConstructor() && !TSuperHelper.isTSuper(srcMethodBinding)
            && dstMethod.isTSuper;
    try {
        adjustCode(classFile, dstMethod.getModel(), methodCode, codeAttributeOffset, codeLength,
                isCtorAddingMarkArg);
    } catch (IncompatibleBytecodeException ibe) {
        ProblemReporter pr = dstMethod.scope.problemReporter();
        pr.incompatibleBytecode(ibe._offendingName, ibe._problemId);
    } catch (Throwable t) {
        ProblemReporter pr = dstMethod.scope.problemReporter();
        pr.corruptBytecode(dstMethod.binding);
    }

    int lineOffset = adjustTail(dstType, methodCode, tailOffset, dstMethod.binding, srcMethodBinding.model);
    if (copyInhSrcLineOffsetOffset > -1 && lineOffset != 0) {
        // adjustTail has computed a new lineOffset, insert it into the existing attribute now:
        OTByteCodes.setInt(methodCode, copyInhSrcLineOffsetOffset, lineOffset);
        MethodModel.getModel(dstMethod)._lineOffset = lineOffset;
    }
    codeStream.writeBytes(methodCode);
    classFile.contents = codeStream.bCodeStream; // might have grown during adjust
    classFile.contentsOffset += methodCode.length;
    classFile.methodCount++;

    dstMethod.maybeRecordByteCode(classFile, newMethodOffset);
}