Ejemplo n.º 1
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            Binary.BigEndian.Write(attributeDataStream, MaxStack);
            Binary.BigEndian.Write(attributeDataStream, MaxLocals);

            if (Code.LongLength > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Code.LongLength), $"Code length too big: {Code.LongLength} > {uint.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (uint)Code.LongLength);
            attributeDataStream.Write(Code);

            if (ExceptionTable.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(ExceptionTable.Count), $"Exception table too big: {ExceptionTable.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)ExceptionTable.Count);
            foreach (var exceptionTableEntry in ExceptionTable)
            {
                Binary.BigEndian.Write(attributeDataStream, exceptionTableEntry.StartPc);
                Binary.BigEndian.Write(attributeDataStream, exceptionTableEntry.EndPc);
                Binary.BigEndian.Write(attributeDataStream, exceptionTableEntry.HandlerPc);
                Binary.BigEndian.Write(attributeDataStream, (ushort)(exceptionTableEntry.CatchType == null ? 0 :
                                                                     writerState.ConstantPool.Find(new ClassEntry(new Utf8Entry(exceptionTableEntry.CatchType.Name)))));
            }

            if (Attributes.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Attributes.Count), $"Too many attributes: {Attributes.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)Attributes.Count);
            foreach (var attriute in Attributes)
            {
                ClassFile.WriteAttribute(attributeDataStream, attriute, writerState, AttributeScope.Code);
            }

            return(attributeDataStream.ToArray());
        }