Example #1
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (BootstrapMethods.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(BootstrapMethods.Count), $"Number of bootstrap methods is too big: {BootstrapMethods.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)BootstrapMethods.Count);
            foreach (var method in BootstrapMethods)
            {
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(method.BootstrapMethodReference.ToConstantPool()));
                if (method.Arguments.Count > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(method.Arguments.Count),
                                                          $"Number of arguments is too big: {method.Arguments.Count} > {ushort.MaxValue}");
                }
                Binary.BigEndian.Write(attributeDataStream, (ushort)method.Arguments.Count);
                foreach (var argument in method.Arguments)
                {
                    Binary.BigEndian.Write(attributeDataStream, writerState.ConstantPool.Find(argument switch
                    {
                        int integerValue => (Entry) new IntegerEntry(integerValue),
                        float floatValue => new FloatEntry(floatValue),
                        string stringValue => new StringEntry(new Utf8Entry(stringValue)),
                        long longValue => new LongEntry(longValue),
                        double doubleValue => new DoubleEntry(doubleValue),
                        Handle handle => handle.ToConstantPool(),
                        MethodDescriptor methodDescriptor => new MethodTypeEntry(new Utf8Entry(methodDescriptor.ToString())),
                        _ => throw new ArgumentOutOfRangeException(nameof(argument), $"Can't encode value of type {argument.GetType()}")
                    }));
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (Parameters.Count > byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Parameters.Count), $"Number of parameters is too big: {Parameters.Count} > {byte.MaxValue}");
            }
            attributeDataStream.WriteByte((byte)Parameters.Count);
            foreach (var parameter in Parameters)
            {
                if (parameter.Annotations.Count > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(parameter.Annotations.Count),
                                                          $"Number of annotations is too big: {parameter.Annotations.Count} > {ushort.MaxValue}");
                }
                Binary.BigEndian.Write(attributeDataStream, (ushort)parameter.Annotations.Count);
                foreach (var annotation in parameter.Annotations)
                {
                    annotation.Write(attributeDataStream, writerState);
                }
            }

            return(attributeDataStream.ToArray());
        }
Example #3
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            Binary.BigEndian.Write(attributeDataStream, writerState.ConstantPool.Find(new Utf8Entry(Value)));

            return(attributeDataStream.ToArray());
        }
Example #4
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            Value.Write(attributeDataStream, writerState);

            return(attributeDataStream.ToArray());
        }
Example #5
0
 internal void Write(Stream stream, ClassWriterState writerState)
 {
     if (Path.Count > byte.MaxValue)
     {
         throw new ArgumentOutOfRangeException(nameof(Path.Count), $"Path is too big: {Path.Count} > {byte.MaxValue}");
     }
     stream.WriteByte((byte)Path.Count);
     foreach (var part in Path)
     {
         stream.WriteByte((byte)part.TypePathKind);
         stream.WriteByte(part.TypeArgumentIndex);
     }
 }
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            var result = new byte[2];

            Binary.BigEndian.Set(Value switch
            {
                long longValue => writerState.ConstantPool.Find(new LongEntry(longValue)),
                float floatValue => writerState.ConstantPool.Find(new FloatEntry(floatValue)),
                double doubleValue => writerState.ConstantPool.Find(new DoubleEntry(doubleValue)),
                int integerValue => writerState.ConstantPool.Find(new IntegerEntry(integerValue)),
                string stringValue => writerState.ConstantPool.Find(new StringEntry(new Utf8Entry(stringValue))),
                _ => throw new ArgumentOutOfRangeException(nameof(Value), $"Can't encode value of type {Value.GetType()}")
            }, result);
Example #7
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
     if (Table.Count > ushort.MaxValue)
     {
         throw new ArgumentOutOfRangeException(nameof(Table.Count), $"Table is too big: {Table.Count} > {ushort.MaxValue}");
     }
     Binary.BigEndian.Write(stream, (ushort)Table.Count);
     foreach (var entry in Table)
     {
         Binary.BigEndian.Write(stream, entry.StartPc);
         Binary.BigEndian.Write(stream, entry.Length);
         Binary.BigEndian.Write(stream, entry.Index);
     }
 }
Example #8
0
        internal void Write(Stream stream, ClassWriterState writerState, AttributeScope scope)
        {
            stream.WriteByte((byte)TargetType);
            switch (TargetType)
            {
            case TargetType.GenericClassOrInterfaceDeclaration when Target.TargetTypeKind == TargetTypeKind.TypeParameter && scope == AttributeScope.Class:
            case TargetType.GenericMethodOrConstructorDeclaration when Target.TargetTypeKind == TargetTypeKind.TypeParameter && scope == AttributeScope.Method:
            case TargetType.ExtendsOrImplements when Target.TargetTypeKind == TargetTypeKind.Supertype && scope == AttributeScope.Class:
            case TargetType.TypeInBoundInGenericClassOrInterface when Target.TargetTypeKind == TargetTypeKind.TypeParameterBound && scope == AttributeScope.Class:
            case TargetType.TypeInBoundInGenericMethodOrConstructor when Target.TargetTypeKind == TargetTypeKind.TypeParameterBound && scope == AttributeScope.Method:
            case TargetType.FieldDeclaration when Target.TargetTypeKind == TargetTypeKind.Empty && scope == AttributeScope.Field:
            case TargetType.ReturnTypeOrNewObject when Target.TargetTypeKind == TargetTypeKind.Empty && scope == AttributeScope.Method:
            case TargetType.ReceiverTypeOfMethodOrConstructor when Target.TargetTypeKind == TargetTypeKind.Empty && scope == AttributeScope.Method:
            case TargetType.TypeInFormalParameterOfMethodOrConstructorOrLambda when Target.TargetTypeKind == TargetTypeKind.FormalParameter && scope == AttributeScope.Method:
            case TargetType.ThrowsClause when Target.TargetTypeKind == TargetTypeKind.Throws && scope == AttributeScope.Method:
            case TargetType.LocalVariableDeclaration when Target.TargetTypeKind == TargetTypeKind.Localvar && scope == AttributeScope.Code:
            case TargetType.ResourceVariableDeclaration when Target.TargetTypeKind == TargetTypeKind.Localvar && scope == AttributeScope.Code:
            case TargetType.ExceptionParameterDeclaration when Target.TargetTypeKind == TargetTypeKind.Catch && scope == AttributeScope.Code:
            case TargetType.InstanceOfExpression when Target.TargetTypeKind == TargetTypeKind.Offset && scope == AttributeScope.Code:
            case TargetType.NewExpression when Target.TargetTypeKind == TargetTypeKind.Offset && scope == AttributeScope.Code:
            case TargetType.MethodReferenceExpressionNew when Target.TargetTypeKind == TargetTypeKind.Offset && scope == AttributeScope.Code:
            case TargetType.MethodReferenceExpressionIdentifier when Target.TargetTypeKind == TargetTypeKind.Offset && scope == AttributeScope.Code:
            case TargetType.CastExpression when Target.TargetTypeKind == TargetTypeKind.TypeArgument && scope == AttributeScope.Code:
            case TargetType.ArgumentForGenericConstructorInvocation when Target.TargetTypeKind == TargetTypeKind.TypeArgument && scope == AttributeScope.Code:
            case TargetType.ArgumentForGenericMethodInvocation when Target.TargetTypeKind == TargetTypeKind.TypeArgument && scope == AttributeScope.Code:
            case TargetType.ArgumentForGenericMethodReferenceExpressionNew when Target.TargetTypeKind == TargetTypeKind.TypeArgument && scope == AttributeScope.Code:
            case TargetType.ArgumentForGenericMethodReferenceExpressionIdentifier when Target.TargetTypeKind == TargetTypeKind.TypeArgument && scope == AttributeScope.Code:
                Target.Write(stream, writerState);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(TargetType));
            }

            TypePath.Write(stream, writerState);

            if (ElementValuePairs.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(ElementValuePairs.Count),
                                                      $"Too many ElementValues: {ElementValuePairs.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(stream, (ushort)ElementValuePairs.Count);
            foreach (var elementValuePair in ElementValuePairs)
            {
                Binary.BigEndian.Write(stream,
                                       writerState.ConstantPool.Find(new Utf8Entry(elementValuePair.ElementName)));
                elementValuePair.Value.Write(stream, writerState);
            }
        }
Example #9
0
 internal void Write(Stream stream, ClassWriterState writerState)
 {
     Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new Utf8Entry(Type.ToString())));
     if (ElementValuePairs.Count > ushort.MaxValue)
     {
         throw new ArgumentOutOfRangeException(nameof(ElementValuePairs.Count),
                                               $"Too many ElementValues: {ElementValuePairs.Count} > {ushort.MaxValue}");
     }
     Binary.BigEndian.Write(stream, (ushort)ElementValuePairs.Count);
     foreach (var elementValuePair in ElementValuePairs)
     {
         Binary.BigEndian.Write(stream,
                                writerState.ConstantPool.Find(new Utf8Entry(elementValuePair.ElementName)));
         elementValuePair.Value.Write(stream, writerState);
     }
 }
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (LineNumberTable.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(LineNumberTable.Count), $"Line number table too big: {LineNumberTable.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)LineNumberTable.Count);
            foreach (var exceptionTableEntry in LineNumberTable)
            {
                Binary.BigEndian.Write(attributeDataStream, exceptionTableEntry.StartPc);
                Binary.BigEndian.Write(attributeDataStream, exceptionTableEntry.LineNumber);
            }

            return(attributeDataStream.ToArray());
        }
Example #11
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (ExceptionTable.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(ExceptionTable.Count),
                                                      $"Exception table size too big: {ExceptionTable.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)ExceptionTable.Count);
            foreach (var exceptionClassName in ExceptionTable)
            {
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new ClassEntry(new Utf8Entry(exceptionClassName.Name))));
            }

            return(attributeDataStream.ToArray());
        }
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (Parameters.Count > byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Parameters.Count), $"Too many parameters: {Parameters.Count} > {byte.MaxValue}");
            }
            attributeDataStream.WriteByte((byte)Parameters.Count);
            foreach (var parameter in Parameters)
            {
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new Utf8Entry(parameter.Name)));
                Binary.BigEndian.Write(attributeDataStream, (ushort)parameter.Access);
            }

            return(attributeDataStream.ToArray());
        }
Example #13
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            Binary.BigEndian.Write(attributeDataStream,
                                   writerState.ConstantPool.Find(new ClassEntry(new Utf8Entry(Class.Name))));

            if (MethodName == null && MethodDescriptor == null)
            {
                Binary.BigEndian.Write(attributeDataStream, (ushort)0);
            }
            else
            {
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new NameAndTypeEntry(new Utf8Entry(MethodName),
                                                                                          new Utf8Entry(MethodDescriptor.ToString()))));
            }

            return(attributeDataStream.ToArray());
        }
Example #14
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());
        }
Example #15
0
        internal static void WriteVerificationElement(Stream stream, ClassWriterState writerState, VerificationElement verificationElement)
        {
            stream.WriteByte((byte)verificationElement.Type);
            switch (verificationElement)
            {
            case ObjectVerificationElement objectVerificationElement:
                Binary.BigEndian.Write(stream,
                                       writerState.ConstantPool.Find(
                                           new ClassEntry(new Utf8Entry(objectVerificationElement.ObjectClass.Name))));
                break;

            case UninitializedVerificationElement uninitializedVerificationElement:
                Binary.BigEndian.Write(stream, uninitializedVerificationElement.NewInstructionOffset);
                break;

            case SimpleVerificationElement _:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(verificationElement));
            }
        }
Example #16
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (Classes.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Classes.Count), $"Too many inner classes: {Classes.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)Classes.Count);
            foreach (var innerClass in Classes)
            {
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new ClassEntry(new Utf8Entry(innerClass.InnerClassName.Name))));
                Binary.BigEndian.Write(attributeDataStream, innerClass.OuterClassName == null ? (ushort)0 :
                                       writerState.ConstantPool.Find(new ClassEntry(new Utf8Entry(innerClass.OuterClassName.Name))));
                Binary.BigEndian.Write(attributeDataStream, innerClass.InnerName == null ? (ushort)0 :
                                       writerState.ConstantPool.Find(new Utf8Entry(innerClass.InnerName)));
                Binary.BigEndian.Write(attributeDataStream, (ushort)innerClass.Access);
            }

            return(attributeDataStream.ToArray());
        }
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (LocalVariableTypeTable.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(LocalVariableTypeTable.Count), $"Local variable type table is too big: {LocalVariableTypeTable.Count} > {ushort.MaxValue}");
            }
            Binary.BigEndian.Write(attributeDataStream, (ushort)LocalVariableTypeTable.Count);
            foreach (var localVariableTypeTableEntry in LocalVariableTypeTable)
            {
                Binary.BigEndian.Write(attributeDataStream, localVariableTypeTableEntry.StartPc);
                Binary.BigEndian.Write(attributeDataStream, localVariableTypeTableEntry.Length);
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new Utf8Entry(localVariableTypeTableEntry.Name)));
                Binary.BigEndian.Write(attributeDataStream,
                                       writerState.ConstantPool.Find(new Utf8Entry(localVariableTypeTableEntry.Signature)));
                Binary.BigEndian.Write(attributeDataStream, localVariableTypeTableEntry.Index);
            }

            return(attributeDataStream.ToArray());
        }
Example #18
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
 }
Example #19
0
 internal override byte[] Save(ClassWriterState writerState, AttributeScope scope) => new byte[0];
Example #20
0
        internal void Save(ClassWriterState writerState)
        {
            if (SourceFile != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.SourceFile))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.SourceFile} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.SourceFile,
                    ParsedAttribute = new SourceFileAttribute
                    {
                        Value = SourceFile
                    }
                });
            }

            if (SourceDebugExtension != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.SourceDebugExtension))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.SourceDebugExtension} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.SourceDebugExtension,
                    ParsedAttribute = new SourceDebugExtensionAttribute
                    {
                        Value = SourceDebugExtension
                    }
                });
            }

            if (Signature != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Signature))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Signature} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Signature,
                    ParsedAttribute = new SignatureAttribute
                    {
                        Value = Signature
                    }
                });
            }

            if (InvisibleAnnotations != null && InvisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeInvisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeInvisibleAnnotations} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeInvisibleAnnotations,
                    ParsedAttribute = new RuntimeInvisibleAnnotationsAttribute
                    {
                        Annotations = InvisibleAnnotations
                    }
                });
            }

            if (VisibleAnnotations != null && VisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeVisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeVisibleAnnotations} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeVisibleAnnotations,
                    ParsedAttribute = new RuntimeVisibleAnnotationsAttribute
                    {
                        Annotations = VisibleAnnotations
                    }
                });
            }

            if (IsDeprecated)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Deprecated))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Deprecated} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Deprecated,
                    ParsedAttribute = new DeprecatedAttribute()
                });
            }

            if (EnclosingMethod != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.EnclosingMethod))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.EnclosingMethod} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.EnclosingMethod,
                    ParsedAttribute = EnclosingMethod
                });
            }

            if (InnerClasses != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.InnerClasses))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.InnerClasses} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.InnerClasses,
                    ParsedAttribute = new InnerClassesAttribute
                    {
                        Classes = InnerClasses
                    }
                });
            }

            foreach (var method in Methods)
            {
                method.Save(writerState);
            }

            foreach (var field in Fields)
            {
                field.Save(writerState);
            }
        }
Example #21
0
        internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
        {
            using var attributeDataStream = new MemoryStream();

            if (Entries.Count > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(Entries.Count), $"Too many entries for StackMapTable: {Entries.Count} > {ushort.MaxValue}");
            }

            Binary.BigEndian.Write(attributeDataStream, (ushort)Entries.Count);

            foreach (var entry in Entries)
            {
                switch (entry.Type)
                {
                case FrameType.Same:
                    if (entry.OffsetDelta < 64)
                    {
                        attributeDataStream.WriteByte((byte)entry.OffsetDelta);
                    }
                    else
                    {
                        attributeDataStream.WriteByte(251);
                        Binary.BigEndian.Write(attributeDataStream, entry.OffsetDelta);
                    }
                    break;

                case FrameType.SameLocals1StackItem:
                    if (entry.OffsetDelta < 64)
                    {
                        attributeDataStream.WriteByte((byte)(entry.OffsetDelta + 64));
                    }
                    else
                    {
                        attributeDataStream.WriteByte(247);
                        Binary.BigEndian.Write(attributeDataStream, entry.OffsetDelta);
                    }

                    WriteVerificationElement(attributeDataStream, writerState, entry.Stack[0]);
                    break;

                case FrameType.Chop:
                    if (entry.ChopK == null)
                    {
                        throw new ArgumentNullException(nameof(entry.ChopK));
                    }
                    if (entry.ChopK < 1 || entry.ChopK > 3)
                    {
                        throw new ArgumentOutOfRangeException(nameof(entry.ChopK),
                                                              $"Chop K was < 1 || > 3: {entry.ChopK}");
                    }

                    attributeDataStream.WriteByte((byte)(251 - entry.ChopK));
                    Binary.BigEndian.Write(attributeDataStream, entry.OffsetDelta);
                    break;

                case FrameType.Append:
                    if (entry.Locals.Count < 1 || entry.Locals.Count > 3)
                    {
                        throw new ArgumentOutOfRangeException(nameof(entry.Locals),
                                                              $"Number of locals was < 1 || > 3: {entry.Locals}");
                    }
                    attributeDataStream.WriteByte((byte)(251 + entry.Locals.Count));
                    Binary.BigEndian.Write(attributeDataStream, entry.OffsetDelta);
                    foreach (var verificationElement in entry.Locals)
                    {
                        WriteVerificationElement(attributeDataStream, writerState, verificationElement);
                    }
                    break;

                case FrameType.Full:
                    attributeDataStream.WriteByte(255);
                    Binary.BigEndian.Write(attributeDataStream, entry.OffsetDelta);

                    if (entry.Locals.Count > ushort.MaxValue)
                    {
                        throw new ArgumentOutOfRangeException(nameof(entry.Locals.Count),
                                                              $"Too many entries in frame's locals: {entry.Locals.Count} > {ushort.MaxValue}");
                    }
                    Binary.BigEndian.Write(attributeDataStream, (ushort)entry.Locals.Count);
                    foreach (var verificationElement in entry.Locals)
                    {
                        WriteVerificationElement(attributeDataStream, writerState, verificationElement);
                    }

                    if (entry.Stack.Count > ushort.MaxValue)
                    {
                        throw new ArgumentOutOfRangeException(nameof(entry.Stack.Count),
                                                              $"Too many entries in frame's stack: {entry.Stack.Count} > {ushort.MaxValue}");
                    }
                    Binary.BigEndian.Write(attributeDataStream, (ushort)entry.Stack.Count);
                    foreach (var verificationElement in entry.Stack)
                    {
                        WriteVerificationElement(attributeDataStream, writerState, verificationElement);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(entry.Type));
                }
            }

            return(attributeDataStream.ToArray());
        }
Example #22
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
     Binary.BigEndian.Write(stream, SupertypeIndex);
 }
Example #23
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
     Binary.BigEndian.Write(stream, Offset);
     stream.WriteByte(TypeArgumentIndex);
 }
Example #24
0
        internal void Write(Stream stream, ClassWriterState writerState)
        {
            stream.WriteByte((byte)Tag);
            switch (Tag)
            {
            case ElementValueTag.Byte:
            case ElementValueTag.Character:
            case ElementValueTag.Integer:
            case ElementValueTag.Short:
            case ElementValueTag.Boolean:
                Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new IntegerEntry((int)ConstValue)));
                break;

            case ElementValueTag.Double:
                Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new DoubleEntry((double)ConstValue)));
                break;

            case ElementValueTag.Float:
                Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new FloatEntry((float)ConstValue)));
                break;

            case ElementValueTag.Long:
                Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new LongEntry((long)ConstValue)));
                break;

            case ElementValueTag.String:
                Binary.BigEndian.Write(stream, writerState.ConstantPool.Find(new Utf8Entry((string)ConstValue)));
                break;

            case ElementValueTag.Enum:
                Binary.BigEndian.Write(stream,
                                       writerState.ConstantPool.Find(new Utf8Entry(EnumConstValue.TypeName.ToString())));
                Binary.BigEndian.Write(stream,
                                       writerState.ConstantPool.Find(new Utf8Entry(EnumConstValue.ConstName)));
                break;

            case ElementValueTag.Class:
                Binary.BigEndian.Write(stream,
                                       writerState.ConstantPool.Find(new Utf8Entry(Class.ToString())));
                break;

            case ElementValueTag.Annotation:
                AnnotationNode.Write(stream, writerState);
                break;

            case ElementValueTag.Array:
                if (ArrayValue.Count > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(ArrayValue.Count), $"Array size is too big: {ArrayValue.Count} > {ushort.MaxValue}");
                }
                Binary.BigEndian.Write(stream, (ushort)ArrayValue.Count);
                foreach (var element in ArrayValue)
                {
                    element.Write(stream, writerState);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(Tag));
            }
        }
Example #25
0
        /// <summary>
        /// Saves method information to annotations
        /// </summary>
        /// <param name="writerState">Class writer state</param>
        internal void Save(ClassWriterState writerState)
        {
            if (Signature != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Signature))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Signature} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Signature,
                    ParsedAttribute = new SignatureAttribute
                    {
                        Value = Signature
                    }
                });
            }

            if (InvisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeInvisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeInvisibleAnnotations} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeInvisibleAnnotations,
                    ParsedAttribute = new RuntimeInvisibleAnnotationsAttribute
                    {
                        Annotations = InvisibleAnnotations
                    }
                });
            }

            if (VisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeVisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeVisibleAnnotations} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeVisibleAnnotations,
                    ParsedAttribute = new RuntimeVisibleAnnotationsAttribute
                    {
                        Annotations = VisibleAnnotations
                    }
                });
            }

            if (ConstantValue != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.ConstantValue))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.ConstantValue} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.ConstantValue,
                    ParsedAttribute = new ConstantValueAttribute
                    {
                        Value = ConstantValue
                    }
                });
            }

            // ReSharper disable once InvertIf
            if (IsDeprecated)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Deprecated))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Deprecated} attribute is already presented on field");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Deprecated,
                    ParsedAttribute = new DeprecatedAttribute()
                });
            }
        }
Example #26
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
     stream.WriteByte(TypeParameterIndex);
 }
Example #27
0
 internal abstract byte[] Save(ClassWriterState writerState, AttributeScope scope);
Example #28
0
 internal override void Write(Stream stream, ClassWriterState writerState)
 {
     Binary.BigEndian.Write(stream, Offset);
 }
Example #29
0
        /// <summary>
        /// Saves method information to annotations
        /// </summary>
        /// <param name="writerState">Class writer state</param>
        internal void Save(ClassWriterState writerState)
        {
            if (Signature != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Signature))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Signature} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Signature,
                    ParsedAttribute = new SignatureAttribute
                    {
                        Value = Signature
                    }
                });
            }

            if (!Access.HasFlag(MethodAccessModifiers.Abstract) && !Access.HasFlag(MethodAccessModifiers.Native) && Instructions != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Code))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Code} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Code,
                    ParsedAttribute = InstructionListConverter.SaveCodeAttribute(this, writerState)
                });
            }

            if (InvisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeInvisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeInvisibleAnnotations} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeInvisibleAnnotations,
                    ParsedAttribute = new RuntimeInvisibleAnnotationsAttribute
                    {
                        Annotations = InvisibleAnnotations
                    }
                });
            }

            if (VisibleAnnotations.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.RuntimeVisibleAnnotations))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.RuntimeVisibleAnnotations} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.RuntimeVisibleAnnotations,
                    ParsedAttribute = new RuntimeVisibleAnnotationsAttribute
                    {
                        Annotations = VisibleAnnotations
                    }
                });
            }

            if (Throws.Count > 0)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Exceptions))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Exceptions} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Exceptions,
                    ParsedAttribute = new ExceptionsAttribute
                    {
                        ExceptionTable = Throws
                    }
                });
            }

            if (AnnotationDefaultValue != null)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.AnnotationDefault))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.AnnotationDefault} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.AnnotationDefault,
                    ParsedAttribute = new AnnotationDefaultAttribute
                    {
                        Value = AnnotationDefaultValue
                    }
                });
            }

            // ReSharper disable once InvertIf
            if (IsDeprecated)
            {
                if (Attributes.Any(x => x.Name == PredefinedAttributeNames.Deprecated))
                {
                    throw new Exception(
                              $"{PredefinedAttributeNames.Deprecated} attribute is already presented on method");
                }
                Attributes.Add(new AttributeNode
                {
                    Name            = PredefinedAttributeNames.Deprecated,
                    ParsedAttribute = new DeprecatedAttribute()
                });
            }
        }
Example #30
0
 internal override byte[] Save(ClassWriterState writerState, AttributeScope scope)
 {
     return(ModifiedUtf8Helper.Encode(Value));
 }