Example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants FieldRefTag

List of usage examples for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants FieldRefTag

Introduction

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

Prototype

int FieldRefTag

To view the source code for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants FieldRefTag.

Click Source Link

Usage

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  ww . ja  v a 2s. c  om
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.springframework.ide.eclipse.core.java.typehierarchy.BytecodeTypeHierarchyClassReader.java

License:Open Source License

public TypeHierarchyElement readTypeHierarchy(InputStream stream) {
    try {//www  .  j  a va  2  s .co m
        DataInputStream dis = new DataInputStream(new BufferedInputStream(stream));
        int magic = dis.readInt(); // magic 0xCAFEBABE
        if (magic != 0xCAFEBABE) {
            throw new IllegalStateException("not bytecode, magic was 0x" + Integer.toString(magic, 16));
        }
        skip(dis, 4);

        int constantPoolCount = dis.readShort();
        Object[] constantPoolData = new Object[constantPoolCount];
        for (int i = 1; i < constantPoolCount; i++) {
            int tag = dis.readByte();
            switch (tag) {
            case ClassFileConstants.Utf8Tag:
                constantPoolData[i] = dis.readUTF();
                break;
            case ClassFileConstants.IntegerTag:
                skip(dis, 4);
                break;
            case ClassFileConstants.FloatTag:
                skip(dis, 4);
                break;
            case ClassFileConstants.LongTag:
                skip(dis, 8);
                i++;
                break;
            case ClassFileConstants.DoubleTag:
                skip(dis, 8);
                i++;
                break;
            case ClassFileConstants.ClassTag:
                constantPoolData[i] = dis.readShort();
                break;
            case ClassFileConstants.StringTag:
                skip(dis, 2);
                break;
            case ClassFileConstants.FieldRefTag:
                skip(dis, 4);
                break;
            case ClassFileConstants.MethodRefTag:
                skip(dis, 4);
                break;
            case ClassFileConstants.InterfaceMethodRefTag:
                skip(dis, 4);
                break;
            case ClassFileConstants.NameAndTypeTag:
                skip(dis, 4);
                break;
            case 15: // ClassFileConstants.MethodHandleTag
                skip(dis, 3);
                break;
            case 16: // ClassFileConstants.MethodTypeTag
                skip(dis, 2);
                break;
            case 18: // ClassFileConstants.InvokeDynamicTag
                skip(dis, 4);
                break;
            }
        }

        skip(dis, 2);

        // classname
        short classNameIndex = dis.readShort();
        short classNameUTF8index = (Short) constantPoolData[classNameIndex];
        char[] className = ((String) constantPoolData[classNameUTF8index]).toCharArray();

        // superclass name
        short superclassNameIndex = dis.readShort();
        char[] superclassName = null;
        if (superclassNameIndex != 0) {
            short superclassNameUTF8index = (Short) constantPoolData[superclassNameIndex];
            superclassName = ((String) constantPoolData[superclassNameUTF8index]).toCharArray();
        }

        // interfaces
        short interfacesCount = dis.readShort();
        char[][] interfaceNames = null;
        if (interfacesCount != 0) {
            interfaceNames = new char[interfacesCount][];
            for (int i = 0; i < interfacesCount; i++) {
                short interfaceNameIndex = dis.readShort();
                short interfaceNameUTF8index = (Short) constantPoolData[interfaceNameIndex];
                interfaceNames[i] = ((String) constantPoolData[interfaceNameUTF8index]).toCharArray();
            }
        }

        return new TypeHierarchyElement(className, superclassName, interfaceNames);
    } catch (Exception e) {
        SpringCore.log(e);
    }

    return null;
}