Example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getConstantPoolOffsets

List of usage examples for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getConstantPoolOffsets

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getConstantPoolOffsets.

Prototype

public int[] getConstantPoolOffsets() 

Source Link

Document

Answer the int array that corresponds to all the offsets of each entry in the constant pool

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

public ImportReference[] buildImports(ClassFileReader reader) {
    // add remaining references to the list of type names
    // (code extracted from BinaryIndexer#extractReferenceFromConstantPool(...))
    int[] constantPoolOffsets = reader.getConstantPoolOffsets();
    int constantPoolCount = constantPoolOffsets.length;
    for (int i = 0; i < constantPoolCount; i++) {
        int tag = reader.u1At(constantPoolOffsets[i]);
        char[] name = null;
        switch (tag) {
        case ClassFileConstants.MethodRefTag:
        case ClassFileConstants.InterfaceMethodRefTag:
            int constantPoolIndex = reader.u2At(constantPoolOffsets[i] + 3);
            int utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[constantPoolIndex] + 3)];
            name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
            break;
        case ClassFileConstants.ClassTag:
            utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[i] + 1)];
            name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
            break;
        }/*from   www . ja  va 2 s  .c o m*/
        if (name == null || (name.length > 0 && name[0] == '['))
            break; // skip over array references
        this.typeNames.add(CharOperation.splitOn('/', name));
    }

    // convert type names into import references
    int typeNamesLength = this.typeNames.size();
    ImportReference[] imports = new ImportReference[typeNamesLength];
    char[][][] set = this.typeNames.set;
    int index = 0;
    for (int i = 0, length = set.length; i < length; i++) {
        char[][] typeName = set[i];
        if (typeName != null) {
            imports[index++] = new ImportReference(typeName, new long[typeName.length]/*dummy positions*/,
                    false/*not on demand*/, 0);
        }
    }
    return imports;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.BinaryIndexer.java

License:Open Source License

/**
 * Extract all type, method, field and interface method references from the constant pool
 *//*from   w  w  w  . ja v a  2  s  . co m*/
private void extractReferenceFromConstantPool(byte[] contents, ClassFileReader reader)
        throws ClassFormatException {
    int[] constantPoolOffsets = reader.getConstantPoolOffsets();
    int constantPoolCount = constantPoolOffsets.length;
    for (int i = 1; i < constantPoolCount; i++) {
        int tag = reader.u1At(constantPoolOffsets[i]);
        /**
         * u1 tag
         * u2 class_index
         * u2 name_and_type_index
         */
        char[] name = null;
        char[] type = null;
        switch (tag) {
        case ClassFileConstants.FieldRefTag:
            // add reference to the class/interface and field name and type
            name = extractName(constantPoolOffsets, reader, i);
            addFieldReference(name);
            break;
        case ClassFileConstants.MethodRefTag:
            // add reference to the class and method name and type
        case ClassFileConstants.InterfaceMethodRefTag:
            // add reference to the interface and method name and type
            name = extractName(constantPoolOffsets, reader, i);
            type = extractType(constantPoolOffsets, reader, i);
            if (CharOperation.equals(INIT, name)) {
                // get class name and see if it's a local type or not
                char[] className = extractClassName(constantPoolOffsets, reader, i);
                boolean localType = false;
                if (className != null) {
                    for (int c = 0, max = className.length; c < max; c++) {
                        switch (className[c]) {
                        case '/':
                            className[c] = '.';
                            break;
                        case '$':
                            localType = true;
                            break;
                        }
                    }
                }
                // add a constructor reference, use class name to extract arg count if it's a local type to remove synthetic parameter
                addConstructorReference(className, extractArgCount(type, localType ? className : null));
            } else {
                // add a method reference
                addMethodReference(name, extractArgCount(type, null));
            }
            break;
        case ClassFileConstants.ClassTag:
            // add a type reference
            name = extractClassReference(constantPoolOffsets, reader, i);
            if (name.length > 0 && name[0] == '[')
                break; // skip over array references
            name = replace('/', '.', name); // so that it looks like java.lang.String
            addTypeReference(name);

            // also add a simple reference on each segment of the qualification (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=24741)
            char[][] qualification = CharOperation.splitOn('.', name);
            for (int j = 0, length = qualification.length; j < length; j++) {
                addNameReference(qualification[j]);
            }
            break;
        }
    }
}

From source file:org.codehaus.jdt.groovy.integration.internal.BinaryGroovySupplementalIndexer.java

License:Apache License

public List<char[]> extractNamedReferences(byte[] contents, ClassFileReader reader) {
    int[] constantPoolOffsets = reader.getConstantPoolOffsets();
    int constantPoolCount = constantPoolOffsets.length;
    List<char[]> refs = new ArrayList<char[]>();
    for (int i = 1; i < constantPoolCount; i++) {
        int tag = reader.u1At(constantPoolOffsets[i]);
        switch (tag) {
        case ClassFileConstants.Utf8Tag:
            char[] strConst = extractStringConstant(constantPoolOffsets, reader, i);
            if (isValidId(strConst)) {
                char[][] splits = CharOperation.splitOn('.', strConst);
                for (char[] split : splits) {
                    refs.add(split);/*from w  ww.  j  a  v a 2 s  . com*/
                }
            }
        }
    }
    return refs;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.model.MethodModel.java

License:Open Source License

/**
 * Ensure we have bytes and constantPoolOffsets ready to use.
 *///from  w  w  w .j  av a2 s.c o  m
private void setupByteCode(boolean bytesRequired) {
    if (this._bytes == null || this._constantPoolOffsets == null) {
        try {
            MethodBinding binding = (this._binding == null) ? this._decl.binding : this._binding;
            assert binding.declaringClass.isTeam();

            ClassFileReader reader = null;
            if (this._bytes == null) {
                if (this._classFile == null && this._decl != null) {
                    char[] className = binding.declaringClass.constantPoolName();
                    this._classFile = (ClassFile) this._decl.compilationResult.compiledTypes.get(className);
                    if (this._classFile != null && !this._classFile.isForType(binding.declaringClass)) {
                        this._classFile = null; // has been reset thus cannot be used for this type any more.
                    }
                }
                // here we made the best attempt to obtain a classfile, use it if possible:
                if (this._classFile != null && this._classFile.isForType(this._binding.declaringClass)) {
                    this._bytes = this._classFile.getBytes();
                    this._structOffset += this._classFile.headerOffset; // structOffset did not yet include the headerOffset
                    int olen = this._classFile.constantPool.currentIndex;
                    System.arraycopy(this._classFile.constantPool.offsets, 0,
                            this._constantPoolOffsets = new int[olen], 0, olen);
                    this._classFile = null; // don't use any more
                    return;
                }
            }
            if (this._bytes != null) {
                // create a reader for in-memory bytes in order to recalculate constant pool offsets
                reader = new ClassFileReader(this._bytes, RoleModel.NO_SOURCE_FILE); // STATE_BYTECODE_PREPARED
            } else {
                // Currently only team-ctors use a MethodModel for byte code retrieval.
                // Use the stored file name for reading the byte code from disc:
                if (binding.declaringClass.isTeam())
                    reader = binding.declaringClass.getTeamModel().read();
                if (reader == null) {
                    if (bytesRequired)
                        throw new InternalCompilerError(
                                "No byte code available for " + new String(binding.readableName())); //$NON-NLS-1$
                    return;
                }
                this._bytes = reader.getBytes();
            }
            this._classFile = null; // don't use any more
            // now we have both a reader and bytes
            this._constantPoolOffsets = reader.getConstantPoolOffsets();
            // find bytecode offset of this method:
            char[] mySignature = this._binding.signature();
            for (IBinaryMethod m : reader.getMethods()) {
                if (CharOperation.equals(m.getSelector(), this._binding.selector)
                        && CharOperation.equals(m.getMethodDescriptor(), mySignature)) {
                    this._structOffset = ((MethodInfo) m).getStructOffset();
                    return;
                }
            }
            if (bytesRequired)
                throw new InternalCompilerError("Method " + String.valueOf(this._binding.readableName()) //$NON-NLS-1$
                        + "not found in class file " + String.valueOf(reader.getFileName())); //$NON-NLS-1$
        } catch (ClassFormatException ex) {
            throw new InternalCompilerError(ex.getMessage());
        } catch (IOException ex) {
            throw new InternalCompilerError(ex.getMessage());
        }
    }

}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.model.RoleModel.java

License:Open Source License

/** Get the byte code of this role class.
 *  Must be registered with (maybe)recordByteCode
 *//*from w ww  .j av a 2 s  .  co m*/
public synchronized byte[] getByteCode() {
    if (this._classByteCode == null) {
        if (this._classFile == null && this._ast != null)
            this._classFile = this._ast.compilationResult.findClassFile(this._binding);
        if (this._classFile != null) // nullified once a class file is re-used for a different type
        {
            this._classByteCode = this._classFile.getBytes();
            this._headerOffset = this._classFile.headerOffset;
        } else {
            // restore bytes from class file on disk:
            try {
                ClassFileReader reader = read();
                if (reader == null) {
                    if (Config.getConfig().ignoreMissingBytecode)
                        return null;
                    throw new InternalCompilerError("Class file was not yet written to disk"); //$NON-NLS-1$
                }
                this._classByteCode = reader.getBytes();
                this._headerOffset = reader.getHeaderOffset();
                this._constantPoolOffsets = reader.getConstantPoolOffsets();
            } catch (Exception e) {
                throw new InternalCompilerError("cannot retrieve generated class file: " + e); //$NON-NLS-1$
            }
        }
    }
    assert (this._classByteCode != null);
    return this._classByteCode;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.model.RoleModel.java

License:Open Source License

private void restoreCPOffsets() {
    try {//from w  w  w  .  j  a va 2  s  .c  o  m
        byte[] code = getByteCode();
        if (code != null) {
            ClassFileReader reader = new ClassFileReader(code, NO_SOURCE_FILE); // not recording OT-attributes
            this._constantPoolOffsets = reader.getConstantPoolOffsets();
        }
    } catch (ClassFormatException ex) {
        throw new InternalCompilerError(ex.toString());
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.TeamMethodGenerator.java

License:Open Source License

/** When o.o.Team is read from .class file, record the byte code here. */
public synchronized void maybeRegisterTeamClassBytes(ClassFileReader teamClass,
        ReferenceBinding teamClassBinding) {
    if (this.classBytes != null)
        return;/*from w  ww .  j a v a  2 s .c  o m*/
    this.classBytes = teamClass.getBytes();
    this.constantPoolOffsets = teamClass.getConstantPoolOffsets();
    for (IBinaryMethod method : teamClass.getMethods()) {
        if (this.classBytes == null && method instanceof MethodInfo) {
            // repair if class already nulled its byte reference:
            this.classBytes = ((MethodInfo) method).reference;
            this.constantPoolOffsets = ((MethodInfo) method).constantPoolOffsets;
        }
        String selector = String.valueOf(method.getSelector());
        String descriptor = String.valueOf(method.getMethodDescriptor());
        int structOffset = ((MethodInfo) method).getStructOffset();
        // relevant new info is structOffset, everything has already  been registered from registerTeamMethod(IBinaryMethod,MethodBinding)
        registerTeamMethod(teamClassBinding, null, selector, descriptor, structOffset);
    }
}