Beispiel #1
0
        // Nothing to do.
        // -----------------------------------------------------------------------------------------------
        // Utility methods
        // -----------------------------------------------------------------------------------------------
        /// <summary>
        ///     Returns the size of the field_info JVMS structure generated by this FieldWriter.
        /// </summary>
        /// <remarks>
        ///     Returns the size of the field_info JVMS structure generated by this FieldWriter. Also adds the
        ///     names of the attributes of this field in the constant pool.
        /// </remarks>
        /// <returns>the size in bytes of the field_info JVMS structure.</returns>
        internal int ComputeFieldInfoSize()
        {
            // The access_flags, name_index, descriptor_index and attributes_count fields use 8 bytes.
            var size = 8;

            // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
            if (constantValueIndex != 0)
            {
                // ConstantValue attributes always use 8 bytes.
                symbolTable.AddConstantUtf8(Constants.Constant_Value);
                size += 8;
            }

            size += Attribute.ComputeAttributesSize(symbolTable, accessFlags, signatureIndex);
            size += AnnotationWriter.ComputeAnnotationsSize(lastRuntimeVisibleAnnotation, lastRuntimeInvisibleAnnotation
                                                            , lastRuntimeVisibleTypeAnnotation, lastRuntimeInvisibleTypeAnnotation);
            if (firstAttribute != null)
            {
                size += firstAttribute.ComputeAttributesSize(symbolTable);
            }
            return(size);
        }
Beispiel #2
0
 /// <summary>
 ///     Constructs a new
 ///     <see cref="AnnotationWriter" />
 ///     .
 /// </summary>
 /// <param name="symbolTable">
 ///     where the constants used in this AnnotationWriter must be stored.
 /// </param>
 /// <param name="useNamedValues">
 ///     whether values are named or not. AnnotationDefault and annotation arrays
 ///     use unnamed values.
 /// </param>
 /// <param name="annotation">
 ///     where the 'annotation' or 'type_annotation' JVMS structure corresponding to
 ///     the visited content must be stored. This ByteVector must already contain all the fields of
 ///     the structure except the last one (the element_value_pairs array).
 /// </param>
 /// <param name="previousAnnotation">
 ///     the previously visited annotation of the
 ///     Runtime[In]Visible[Type]Annotations attribute to which this annotation belongs, or
 ///     <literal>null</literal>
 ///     in other cases (e.g. nested or array annotations).
 /// </param>
 internal AnnotationWriter(SymbolTable symbolTable, bool useNamedValues, ByteVector
                           annotation, AnnotationWriter previousAnnotation)
     : base(VisitorAsmApiVersion.Asm7)
 {
     // -----------------------------------------------------------------------------------------------
     // Constructors and factories
     // -----------------------------------------------------------------------------------------------
     /* latest api = */
     this.symbolTable    = symbolTable;
     this.useNamedValues = useNamedValues;
     this.annotation     = annotation;
     // By hypothesis, num_element_value_pairs is stored in the last unsigned short of 'annotation'.
     numElementValuePairsOffset = annotation.length == 0
         ? -1
         : annotation.length
                                  - 2;
     this.previousAnnotation = previousAnnotation;
     if (previousAnnotation != null)
     {
         previousAnnotation.nextAnnotation = this;
     }
 }
Beispiel #3
0
        /// <summary>
        ///     Puts the content of the field_info JVMS structure generated by this FieldWriter into the given
        ///     ByteVector.
        /// </summary>
        /// <param name="output">where the field_info structure must be put.</param>
        internal void PutFieldInfo(ByteVector output)
        {
            var useSyntheticAttribute = symbolTable.GetMajorVersion() < OpcodesConstants.V1_5;
            // Put the access_flags, name_index and descriptor_index fields.
            var mask = useSyntheticAttribute ? AccessFlags.Synthetic : 0;

            output.PutShort((int)(accessFlags & ~mask)).PutShort(nameIndex).PutShort(descriptorIndex
                                                                                     );
            // Compute and put the attributes_count field.
            // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
            var attributesCount = 0;

            if (constantValueIndex != 0)
            {
                ++attributesCount;
            }
            if (accessFlags.HasFlagFast(AccessFlags.Synthetic) && useSyntheticAttribute)
            {
                ++attributesCount;
            }
            if (signatureIndex != 0)
            {
                ++attributesCount;
            }
            if (accessFlags.HasFlagFast(AccessFlags.Deprecated))
            {
                ++attributesCount;
            }
            if (lastRuntimeVisibleAnnotation != null)
            {
                ++attributesCount;
            }
            if (lastRuntimeInvisibleAnnotation != null)
            {
                ++attributesCount;
            }
            if (lastRuntimeVisibleTypeAnnotation != null)
            {
                ++attributesCount;
            }
            if (lastRuntimeInvisibleTypeAnnotation != null)
            {
                ++attributesCount;
            }
            if (firstAttribute != null)
            {
                attributesCount += firstAttribute.GetAttributeCount();
            }
            output.PutShort(attributesCount);
            // Put the field_info attributes.
            // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
            if (constantValueIndex != 0)
            {
                output.PutShort(symbolTable.AddConstantUtf8(Constants.Constant_Value)).PutInt(2)
                .PutShort(constantValueIndex);
            }
            Attribute.PutAttributes(symbolTable, accessFlags, signatureIndex, output);
            AnnotationWriter.PutAnnotations(symbolTable, lastRuntimeVisibleAnnotation, lastRuntimeInvisibleAnnotation
                                            , lastRuntimeVisibleTypeAnnotation, lastRuntimeInvisibleTypeAnnotation, output);
            if (firstAttribute != null)
            {
                firstAttribute.PutAttributes(symbolTable, output);
            }
        }
Beispiel #4
0
 /// <summary>
 ///     Puts the Runtime[In]Visible[Type]Annotations attributes containing the given annotations and
 ///     all their <i>predecessors</i> (see
 ///     <see cref="previousAnnotation" />
 ///     in the given ByteVector.
 ///     Annotations are put in the same order they have been visited.
 /// </summary>
 /// <param name="symbolTable">
 ///     where the constants used in the AnnotationWriter instances are stored.
 /// </param>
 /// <param name="lastRuntimeVisibleAnnotation">
 ///     The last runtime visible annotation of a field, method or
 ///     class. The previous ones can be accessed with the
 ///     <see cref="previousAnnotation" />
 ///     field. May be
 ///     <literal>null</literal>
 ///     .
 /// </param>
 /// <param name="lastRuntimeInvisibleAnnotation">
 ///     The last runtime invisible annotation of this a field,
 ///     method or class. The previous ones can be accessed with the
 ///     <see cref="previousAnnotation" />
 ///     field. May be
 ///     <literal>null</literal>
 ///     .
 /// </param>
 /// <param name="lastRuntimeVisibleTypeAnnotation">
 ///     The last runtime visible type annotation of this a
 ///     field, method or class. The previous ones can be accessed with the
 ///     <see cref="previousAnnotation" />
 ///     field. May be
 ///     <literal>null</literal>
 ///     .
 /// </param>
 /// <param name="lastRuntimeInvisibleTypeAnnotation">
 ///     The last runtime invisible type annotation of a
 ///     field, method or class field. The previous ones can be accessed with the
 ///     <see cref="previousAnnotation" />
 ///     field. May be
 ///     <literal>null</literal>
 ///     .
 /// </param>
 /// <param name="output">where the attributes must be put.</param>
 internal static void PutAnnotations(SymbolTable symbolTable, AnnotationWriter lastRuntimeVisibleAnnotation
                                     , AnnotationWriter lastRuntimeInvisibleAnnotation, AnnotationWriter lastRuntimeVisibleTypeAnnotation
                                     , AnnotationWriter lastRuntimeInvisibleTypeAnnotation, ByteVector output)
 {
     if (lastRuntimeVisibleAnnotation != null)
     {
         lastRuntimeVisibleAnnotation.PutAnnotations(symbolTable.AddConstantUtf8(Constants
                                                                                 .Runtime_Visible_Annotations), output);
     }
     if (lastRuntimeInvisibleAnnotation != null)
     {
         lastRuntimeInvisibleAnnotation.PutAnnotations(symbolTable.AddConstantUtf8(Constants
                                                                                   .Runtime_Invisible_Annotations), output);
     }
     if (lastRuntimeVisibleTypeAnnotation != null)
     {
         lastRuntimeVisibleTypeAnnotation.PutAnnotations(symbolTable.AddConstantUtf8(Constants
                                                                                     .Runtime_Visible_Type_Annotations), output);
     }
     if (lastRuntimeInvisibleTypeAnnotation != null)
     {
         lastRuntimeInvisibleTypeAnnotation.PutAnnotations(symbolTable.AddConstantUtf8(Constants
                                                                                       .Runtime_Invisible_Type_Annotations), output);
     }
 }
Beispiel #5
0
        /// <summary>
        ///     Returns the size of the Runtime[In]Visible[Type]Annotations attributes containing the given
        ///     annotations and all their <i>predecessors</i> (see
        ///     <see cref="previousAnnotation" />
        ///     . Also adds the
        ///     attribute names to the constant pool of the class (if not null).
        /// </summary>
        /// <param name="lastRuntimeVisibleAnnotation">
        ///     The last runtime visible annotation of a field, method or
        ///     class. The previous ones can be accessed with the
        ///     <see cref="previousAnnotation" />
        ///     field. May be
        ///     <literal>null</literal>
        ///     .
        /// </param>
        /// <param name="lastRuntimeInvisibleAnnotation">
        ///     The last runtime invisible annotation of this a field,
        ///     method or class. The previous ones can be accessed with the
        ///     <see cref="previousAnnotation" />
        ///     field. May be
        ///     <literal>null</literal>
        ///     .
        /// </param>
        /// <param name="lastRuntimeVisibleTypeAnnotation">
        ///     The last runtime visible type annotation of this a
        ///     field, method or class. The previous ones can be accessed with the
        ///     <see cref="previousAnnotation" />
        ///     field. May be
        ///     <literal>null</literal>
        ///     .
        /// </param>
        /// <param name="lastRuntimeInvisibleTypeAnnotation">
        ///     The last runtime invisible type annotation of a
        ///     field, method or class field. The previous ones can be accessed with the
        ///     <see cref="previousAnnotation" />
        ///     field. May be
        ///     <literal>null</literal>
        ///     .
        /// </param>
        /// <returns>
        ///     the size in bytes of a Runtime[In]Visible[Type]Annotations attribute containing the
        ///     given annotations and all their predecessors. This includes the size of the
        ///     attribute_name_index and attribute_length fields.
        /// </returns>
        internal static int ComputeAnnotationsSize(AnnotationWriter lastRuntimeVisibleAnnotation
                                                   , AnnotationWriter lastRuntimeInvisibleAnnotation, AnnotationWriter lastRuntimeVisibleTypeAnnotation
                                                   , AnnotationWriter lastRuntimeInvisibleTypeAnnotation)
        {
            var size = 0;

            if (lastRuntimeVisibleAnnotation != null)
            {
                size += lastRuntimeVisibleAnnotation.ComputeAnnotationsSize(Constants.Runtime_Visible_Annotations
                                                                            );
            }
            if (lastRuntimeInvisibleAnnotation != null)
            {
                size += lastRuntimeInvisibleAnnotation.ComputeAnnotationsSize(Constants.Runtime_Invisible_Annotations
                                                                              );
            }
            if (lastRuntimeVisibleTypeAnnotation != null)
            {
                size += lastRuntimeVisibleTypeAnnotation.ComputeAnnotationsSize(
                    Constants.Runtime_Visible_Type_Annotations
                    );
            }
            if (lastRuntimeInvisibleTypeAnnotation != null)
            {
                size += lastRuntimeInvisibleTypeAnnotation.ComputeAnnotationsSize(
                    Constants.Runtime_Invisible_Type_Annotations
                    );
            }
            return(size);
        }