예제 #1
0
        /// <summary>
        /// Creates a <see cref="JavaClass"/> instance from bytes.
        /// </summary>
        /// <param name="stream">The stream to create the class file from.</param>
        /// <returns>A new <see cref="JavaClass"/> instance.</returns>
        public static JavaClass Create(Stream stream)
        {
            Guard.NotNull(ref stream, nameof(stream));

            JavaClass result = new JavaClass();

            result.Magic             = stream.ReadInteger();
            result.MinorVersion      = stream.ReadShort();
            result.MajorVersion      = stream.ReadShort();
            result.ConstantPoolCount = stream.ReadShort();
            result.ConstantPool      = ReadConstants(stream, result.ConstantPoolCount);
            result.AccessFlags       = (JavaClassAccessFlags)stream.ReadShort();
            result.ThisClassIndex    = stream.ReadShort();
            result.SuperClassIndex   = stream.ReadShort();
            result.InterfacesCount   = stream.ReadShort();
            result.Interfaces        = stream.ReadShorts(result.InterfacesCount);
            result.FieldsCount       = stream.ReadShort();
            result.Fields            = ReadFields(stream, result.FieldsCount, result.ConstantPool);
            result.MethodsCount      = stream.ReadShort();
            result.Methods           = ReadMethods(stream, result.MethodsCount, result.ConstantPool);
            result.AttributesCount   = stream.ReadShort();
            result.Attributes        = JavaAttribute.ReadFromStream(stream, result.AttributesCount, result.ConstantPool);

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Reads a method from the stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="constantPool">The constant pool used for finding the attribute names.</param>
        /// <returns>The method read from the stream.</returns>
        private static JavaMethod ReadMethod(Stream stream, JavaConstantPool constantPool)
        {
            JavaMethodAccessFlags flags = (JavaMethodAccessFlags)stream.ReadShort();
            ushort nameIndex            = stream.ReadShort();
            ushort descriptorIndex      = stream.ReadShort();
            ushort attributesCount      = stream.ReadShort();

            IJavaAttribute[] attributes = JavaAttribute.ReadFromStream(stream, attributesCount, constantPool);
            return(new JavaMethod(flags, nameIndex, descriptorIndex, attributesCount, attributes));
        }