Пример #1
0
        public override void parse(MSBBinaryReaderWrapper reader, ConstantPoolInfo[] pool)
        {
            //base.parse(reader,pool);

            maxLength = reader.ReadUInt16();
            maxLocals = reader.ReadUInt16();
            codeLength = reader.ReadUInt32();

            int readLength = 0;
            while (readLength < codeLength)
            {
                ByteCode bc = ByteCodeFactory.readByteCode(reader,pool,readLength);
                //code.Add(bc);
                //if (log.IsDebugEnabled) log.DebugFormat("{0}: {1}",readLength,bc);
                code.Add(readLength,bc);
                readLength += bc.getSize();
            }

            exceptionTableLength = reader.ReadUInt16();

            //if (log.IsDebugEnabled) log.DebugFormat("Exception table length: {0}",exceptionTableLength);

            exceptions = new ExceptionTableEntry[exceptionTableLength];

            for (int i = 0; i < exceptionTableLength; i++){
                exceptions[i] = new ExceptionTableEntry(reader);
            }

            attributeCount = reader.ReadUInt16();

            //if (log.IsDebugEnabled) log.DebugFormat("Have {0} code attributes",attributeCount);
            attributes = new AttributeInfo[attributeCount];

            for (int i = 0; i < attributeCount; i++)
            {
                attributes[i] = AttributeInfo.readAttributeInfo(reader,pool);

                if (attributes[i] is LineNumberTableAttribute){
                    lineNumbers = (LineNumberTableAttribute) attributes[i];

                }
            }
        }
Пример #2
0
        /**
         * Constructs new ClassFile from the given reader_
         */
        public ClassFile(BinaryReader reader_)
        {
            MSBBinaryReaderWrapper reader = new MSBBinaryReaderWrapper(reader_);
            System.UInt32 magic = reader.ReadUInt32();

            // if it doesn't start with CLASS_FILE_MAGIC, it ain't even valid!
            if (magic != CLASS_FILE_MAGIC)
            {
                if (log.IsDebugEnabled) log.DebugFormat("Did not get proper magic...");
                if (log.IsDebugEnabled) log.DebugFormat("{0:X}",CLASS_FILE_MAGIC);
                if (log.IsDebugEnabled) log.DebugFormat("{0:X}",magic);
            }
            else
            {
                minor = reader.ReadUInt16();
                major = reader.ReadUInt16();
                if (log.IsDebugEnabled) log.DebugFormat("{0}.{1}",major,minor);

                /**
                 * The constant pool is the set of Strings, method names, class names and numbers
                 * that will be referenced throughout the class
                 */
                constantPoolCount = (UInt16)(reader.ReadUInt16() - 1); // the value here is the maximum 1-indexed entry
                if (log.IsDebugEnabled) log.DebugFormat("{0} constant pool entries",constantPoolCount);
                constantPool = new ConstantPoolInfo[constantPoolCount];

                /**
                 * Pass 1 - Read in the actual entries
                 */
                for (int i = 0; i < constantPoolCount; i++)
                {
                    constantPool[i] = ConstantPoolInfoFactory.readConstantPoolEntry(reader);
                    // Long entries take up 2 constants
                    if (constantPool[i] is ConstantPoolInfo_Long || constantPool[i] is ConstantPoolInfo_Double){
                        constantPool[i+1] = constantPool[i];
                        i++;
                    }

                    if (constantPool[i] is ConstantPoolInfo_UTF8){
                        //ConstantPoolInfo_UTF8 utf8 = (ConstantPoolInfo_UTF8) constantPool[i];

                        //if (log.IsDebugEnabled) log.DebugFormat("{0} Read {1}:{2}",i+1,constantPool[i].getName(),utf8.isUnicode ?  "(UNICODE STRING)" : constantPool[i].ToString());
                    }
                    else {
                        //if (log.IsDebugEnabled) log.DebugFormat("{0} Read {1}:{2}",i+1,constantPool[i].getName(),constantPool[i].ToString());
                    }
                }

                /**
                 * Pass 2 - Resolve constant pool references
                 * /
                 */
                for (int i = 0; i < constantPool.Length; i++)
                {
                    constantPool[i].resolve(constantPool);
                    //if (log.IsDebugEnabled) log.DebugFormat("{0} Read {1}:{2}",i+1,constantPool[i].getName(),constantPool[i].ToString());
                }

                accessFlags = reader.ReadUInt16();

                UInt16 thisClassIndex = reader.ReadUInt16();
                thisClass = (ConstantPoolInfo_Class)constantPool[thisClassIndex - 1];

                if (log.IsDebugEnabled) log.DebugFormat("This class: {0}",constantPool[thisClass.getNameIndex()-1]);

                UInt16 superClassIndex = reader.ReadUInt16();
                if (superClassIndex != 0){
                    superClass = (ConstantPoolInfo_Class)constantPool[superClassIndex - 1];
                    if (log.IsDebugEnabled) log.DebugFormat("Super class: {0}",constantPool[superClass.getNameIndex()-1]);
                }
                else if (! thisClass.getClassName().Equals("java/lang/Object")){
                    throw new Exception("Only Object can have no superclasses");
                }
                interfaceCount = reader.ReadUInt16();
                if (log.IsDebugEnabled) log.DebugFormat("{0} interfaces",interfaceCount);

                if (interfaceCount > 0)
                {
                    interfaces = new ConstantPoolInfo_Class[interfaceCount];
                    for (int i = 0; i < interfaceCount; i++)
                    {
                        UInt16 ifaceIdx = reader.ReadUInt16();
                        interfaces[i] = (ConstantPoolInfo_Class) constantPool[ifaceIdx -1];
                    }
                }

                fieldCount = reader.ReadUInt16();
                if (log.IsDebugEnabled) log.DebugFormat("{0} fields",fieldCount);

                if (fieldCount > 0){
                    fields = new FieldInfo[fieldCount];

                    for (int i = 0; i < fieldCount; i++){
                        fields[i] = new FieldInfo(reader,constantPool);

                        if (fields[i].isStatic()){
                            staticFieldValues.Add(fields[i].GetFieldName(),NullValue.INSTANCE);
                        }
                    }
                }

                methodCount = reader.ReadUInt16();
                if (log.IsDebugEnabled) log.DebugFormat("{0} methods",methodCount);
                if (methodCount > 0)
                {
                    methods = new MethodInfo[methodCount];

                    for (int i = 0; i < methodCount; i++)
                    {
                        methods[i] = new MethodInfo(reader,constantPool);
                        //methods[i].resolve(constantPool);
                        //if (log.IsDebugEnabled) log.DebugFormat("Method: {0}",methods[i]);
                        //if (log.IsDebugEnabled) log.DebugFormat("Storing under {0}",methods[i].getMethodName()+methods[i].getDescriptor());
                        methodsByName[methods[i].getMethodName()+methods[i].getDescriptor()] = methods[i];
                        methods[i].SetClassFile(this);
                    }
                }

                attributeCount = reader.ReadUInt16();
                if (log.IsDebugEnabled) log.DebugFormat("{0} attributes",attributeCount);
                attributes = new AttributeInfo[attributeCount];
                for (int i = 0; i < attributeCount; i++)
                {
                    attributes[i] = AttributeInfo.readAttributeInfo(reader,constantPool);

                    if (log.IsDebugEnabled) log.DebugFormat(attributes[i].ToString());
                }

            }
        }
Пример #3
0
 public AttributeInfo(ConstantPoolInfo_UTF8 name, MSBBinaryReaderWrapper reader,ConstantPoolInfo[] pool)
 {
     this.name = name;
     length = reader.ReadUInt32();
     parse(reader,pool);
 }