Esempio n. 1
0
        /// <summary>
        /// Reads a single code attribute at the current position of the provided reader.
        /// </summary>
        /// <param name="reader">The reader to use.</param>
        /// <returns>The code attribute that was read.</returns>
        public static CodeAttribute FromReader(IBigEndianReader reader)
        {
            var contents = new CodeAttribute
            {
                MaxStack  = reader.ReadUInt16(),
                MaxLocals = reader.ReadUInt16(),
                Code      = reader.ReadBytes(reader.ReadInt32()),
            };

            ushort handlerCount = reader.ReadUInt16();

            for (int i = 0; i < handlerCount; i++)
            {
                contents.ExceptionHandlers.Add(ExceptionHandlerInfo.FromReader(reader));
            }

            ushort attributeCount = reader.ReadUInt16();

            for (int i = 0; i < attributeCount; i++)
            {
                contents.Attributes.Add(AttributeInfo.FromReader(reader));
            }

            return(contents);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a class file from a binary reader.
        /// </summary>
        /// <param name="reader">The reader to read from.</param>
        /// <returns>The parsed class file.</returns>
        /// <exception cref="BadImageFormatException">Occurs when the file does not provide a valid signature of
        /// a class file.</exception>
        public static JavaClassFile FromReader(IBigEndianReader reader)
        {
            uint magic = reader.ReadUInt32();

            if (magic != Magic)
            {
                throw new BadImageFormatException("Image does not contain a valid class file.");
            }

            var file = new JavaClassFile
            {
                MinorVersion = reader.ReadUInt16(),
                MajorVersion = reader.ReadUInt16(),
                ConstantPool = ConstantPool.FromReader(reader),
                AccessFlags  = (ClassAccessFlags)reader.ReadUInt16(),
                ThisClass    = reader.ReadUInt16(),
                SuperClass   = reader.ReadUInt16(),
            };

            ushort interfaceCount = reader.ReadUInt16();

            for (int i = 0; i < interfaceCount; i++)
            {
                file.Interfaces.Add(reader.ReadUInt16());
            }

            ushort fieldCount = reader.ReadUInt16();

            for (int i = 0; i < fieldCount; i++)
            {
                file.Fields.Add(FieldInfo.FromReader(reader));
            }

            ushort methodCount = reader.ReadUInt16();

            for (int i = 0; i < methodCount; i++)
            {
                file.Methods.Add(MethodInfo.FromReader(reader));
            }

            ushort attributeCount = reader.ReadUInt16();

            for (int i = 0; i < attributeCount; i++)
            {
                file.Attributes.Add(AttributeInfo.FromReader(reader));
            }

            return(file);
        }