private static void WriteExceptionHandlers(IBigEndianWriter writer, CodeAttribute attribute) { writer.WriteU2((ushort)attribute.ExceptionHandlers.Count); foreach (var exceptionHandler in attribute.ExceptionHandlers) { writer.WriteU2(exceptionHandler.TryStart); writer.WriteU2(exceptionHandler.TryEnd); writer.WriteU2(exceptionHandler.HandlerStart); writer.WriteU2(exceptionHandler.CatchType); } }
/// <inheritdoc /> protected override void WriteBody(CodeAttribute attribute, IBigEndianWriter writer) { writer.WriteU2(attribute.MaxStack); writer.WriteU2(attribute.MaxLocals); WriteBytecode(writer, attribute); WriteExceptionHandlers(writer, attribute); writer.WriteU2((ushort)attribute.Attributes.Count); foreach (var nestedAttribute in attribute.Attributes) { Director.WriteAttribute(nestedAttribute, writer); } }
/// <inheritdoc /> protected override void WriteBody(StackMapTableAttribute attribute, IBigEndianWriter writer) { var stackMapFrameWriter = new StackMapFrameWriter(writer); writer.WriteU2((ushort)attribute.Entries.Count); foreach (var frame in attribute.Entries) { stackMapFrameWriter.WriteFrame(frame); } }
private static void WriteUtf8Constant(Utf8Constant constant, IBigEndianWriter writer) { var encoder = JavaUtf8.Instance; string value = constant.Value; byte[] array = Pool.Rent(encoder.GetMaxByteCount(value.Length)); int chars = encoder.GetBytes(value, 0, value.Length, array, 0); writer.WriteU2((ushort)chars); writer.Write(array.AsSpan().Slice(0, chars)); Pool.Return(array, true); }
internal void WriteConstantPool() { _writer.WriteU2((ushort)(_pool.Count + 1)); for (int i = 1; i < _pool.Count + 1; i++) { var current = _pool[i]; if (current is LongConstant or DoubleConstant) { i++; Debug.Assert(current == _pool[i], "Long and double constants should take up 2 slots."); } ConstantWriter.BuildConstant(current, _writer); } }
/// <inheritdoc /> public void Dispose() { var temporary = (StreamDataDestination)_segment.Data; var stream = temporary.Stream; long length = stream.Length; if (_isU2) { _destination.WriteU2((ushort)length); } else { _destination.WriteU4((uint)length); } stream.Seek(0, SeekOrigin.Begin); for (int i = 0; i < stream.Length; i++) { _destination.WriteU1((byte)stream.ReadByte()); } temporary.Dispose(); }
internal void WriteFrame(SameLocals1StackItemFrameExtended frame) { _writer.WriteU2(frame.OffsetDelta); _verificationInfoWriter.WriteVerificationInfo(frame.StackTop); }
internal void WriteVerificationInfo(ObjectVariableInfo info) { _writer.WriteU2(info.ConstantPoolIndex); }
/// <inheritdoc /> public void WriteName(JvmAttribute attribute, IBigEndianWriter writer) => writer.WriteU2(attribute.NameIndex);
/// <summary> /// Writes the <see cref="ClassFileHeader"/> to the <paramref name="writer"/>. /// </summary> /// <param name="writer">The <see cref="IBigEndianWriter"/> to write the <see cref="ClassFileHeader"/> to.</param> public void Write(IBigEndianWriter writer) { writer.WriteU4(Magic); writer.WriteU2(Minor); writer.WriteU2(Major); }
/// <summary> /// Writes the <see cref="ClassFileBody"/> to the <paramref name="writer"/>. /// </summary> /// <param name="writer">The <see cref="IBigEndianWriter"/> to write the <see cref="ClassFileBody"/> to.</param> public void Write(IBigEndianWriter writer) { writer.WriteU2((ushort)AccessFlags); writer.WriteU2(ThisClass); writer.WriteU2(SuperClass); }
/// <summary> /// Writes the <paramref name="constant"/> to the given output <paramref name="writer"/>. /// </summary> /// <param name="constant">The <see cref="Constant"/> to serialize.</param> /// <param name="writer">The <see cref="BigEndianWriter"/> to write data to.</param> public static void BuildConstant(Constant constant, IBigEndianWriter writer) { writer.WriteU1((byte)constant.Tag); switch (constant) { case ClassConstant classConstant: writer.WriteU2(classConstant.NameIndex); break; case FieldrefConstant fieldrefConstant: writer.WriteU2(fieldrefConstant.ClassIndex); writer.WriteU2(fieldrefConstant.NameAndTypeIndex); break; case MethodrefConstant methodrefConstant: writer.WriteU2(methodrefConstant.ClassIndex); writer.WriteU2(methodrefConstant.NameAndTypeIndex); break; case InterfaceMethodrefConstant interfaceMethodrefConstant: writer.WriteU2(interfaceMethodrefConstant.ClassIndex); writer.WriteU2(interfaceMethodrefConstant.NameAndTypeIndex); break; case StringConstant stringConstant: writer.WriteU2(stringConstant.StringIndex); break; case IntegerConstant integerConstant: writer.WriteI4(integerConstant.Value); break; case FloatConstant floatConstant: writer.WriteR4(floatConstant.Value); break; case LongConstant longConstant: writer.WriteI8(longConstant.Value); break; case DoubleConstant doubleConstant: writer.WriteR8(doubleConstant.Value); break; case NameAndTypeConstant nameAndTypeConstant: writer.WriteU2(nameAndTypeConstant.NameIndex); writer.WriteU2(nameAndTypeConstant.DescriptorIndex); break; case Utf8Constant utf8Constant: WriteUtf8Constant(utf8Constant, writer); break; case MethodHandleConstant methodHandleConstant: writer.WriteU1((byte)methodHandleConstant.ReferenceKind); writer.WriteU2(methodHandleConstant.ReferenceIndex); break; case MethodTypeConstant methodTypeConstant: writer.WriteU2(methodTypeConstant.DescriptorIndex); break; case InvokeDynamicConstant invokeDynamicConstant: writer.WriteU2(invokeDynamicConstant.BootstrapMethodAttributeIndex); writer.WriteU2(invokeDynamicConstant.NameAndTypeIndex); break; default: throw new NotSupportedException($"Unsupported constant type: {constant.GetType().FullName}"); } }