public void Assert(FieldInfo field) { NAssert.AreEqual(AccessFlags, field.AccessFlags, string.Format("Access flags for field '{0}' ({1}) doesn't match expected ({2})!", Name, field.AccessFlags, AccessFlags)); NAssert.AreEqual(Name, field.Name, string.Format("Field name '{0}' doesn't match expected '{1}'!", Name, field.Name)); NAssert.AreEqual(Descriptor, field.Descriptor, string.Format("Descriptor for field '{0}' ({1}) doesn't match expected ({2})!", Name, field.Descriptor, Descriptor)); NAssert.AreEqual(GenericDescriptor, field.GetSignature(), string.Format("GenericDescriptor for field '{0}' ({1}) doesn't match expected ({2})!", Name, field.GetSignature(), GenericDescriptor)); NAssert.AreEqual(Deprecated, field.Attributes.Get <DeprecatedAttribute>() != null, string.Format("Deprecated for field '{0}' ({1}) doesn't match expected ({2})!", Name, !Deprecated, Deprecated)); if (ConstantValue != null) { ConstantValueAttribute cvalue = (ConstantValueAttribute)field.Attributes.FirstOrDefault(a => a.Name == "ConstantValue"); NAssert.IsNotNull(cvalue, string.Format("No constant found for field '{0}'!", Name)); NAssert.AreEqual(ConstantValue, cvalue.Constant.ToString(), string.Format("ConstantValue for field '{0}' ({1}) doesn't match expected ({2})!", Name, cvalue.Constant.ToString(), ConstantValue)); } }
public FieldInfo(ref ReadOnlySpan <byte> data, CPInfo[] constants) { AccessFlags = data.ReadTwo(); NameIndex = data.ReadTwo(); DescriptorIndex = data.ReadTwo(); AttributesCount = data.ReadTwo(); AttributeInfo = new AttributeInfo[AttributesCount]; for (int i = 0; i < AttributesCount; i++) { ushort nameIndexNonSwapped = MemoryMarshal.Cast <byte, ushort>(data)[0]; ushort nameIndex = nameIndexNonSwapped.SwapEndian(); string name = ((CUtf8Info)constants[nameIndex]).String; switch (name) { case "ConstantValue": AttributeInfo[i] = new ConstantValueAttribute(ref data, constants); break; case "Synthetic": AttributeInfo[i] = new SyntheticAttribute(ref data, constants); break; case "Signature": AttributeInfo[i] = new SignatureAttribute(ref data, constants); break; case "Deprecated": AttributeInfo[i] = new DeprecatedAttribute(ref data, constants); Deprecated = true; break; case "RuntimeVisibleAnnotations": AttributeInfo[i] = new RuntimeVisibleAnnotationsAttribute(ref data, constants); break; case "RuntimeInvisibleAnnotations": throw new NotImplementedException(); default: AttributeInfo[i] = new AttributeInfo(ref data, constants); break; } } Name = ((CUtf8Info)constants[NameIndex]).String; Descriptor = ((CUtf8Info)constants[DescriptorIndex]).String; }
/// <summary> /// Read a list of attributes /// </summary> private void ReadAttributes(ConstantPool cp, IModifiableAttributeProvider provider) { var count = stream.ReadU2(); for (var i = 0; i < count; i++) { var nameIndex = stream.ReadU2(); var name = cp.GetEntry <ConstantPoolUtf8>(nameIndex).Value; var length = stream.ReadU4(); Attribute attr; int tmp; switch (name) { case CodeAttribute.AttributeName: attr = ReadCodeAttribute((MethodDefinition)provider, cp); break; case ConstantValueAttribute.AttributeName: tmp = stream.ReadU2(); attr = new ConstantValueAttribute(((IConstantPoolValue)cp[tmp]).Value); break; case ExceptionsAttribute.AttributeName: attr = ReadExceptionsAttribute(cp); break; case InnerClassesAttribute.AttributeName: attr = ReadInnerClassesAttribute(cp); break; case SyntheticAttribute.AttributeName: attr = new SyntheticAttribute(); break; case SourceFileAttribute.AttributeName: tmp = stream.ReadU2(); attr = new SourceFileAttribute(cp.GetEntry <ConstantPoolUtf8>(tmp).Value); break; case LineNumberTableAttribute.AttributeName: attr = ReadLineNumberTableAttribute(); break; case LocalVariableTableAttribute.AttributeName: attr = ReadLocalVariableTableAttribute(cp); break; case DeprecatedAttribute.AttributeName: attr = new DeprecatedAttribute(); break; case OverrideAttribute.AttributeName: attr = new OverrideAttribute(); break; case SignatureAttribute.AttributeName: tmp = stream.ReadU2(); attr = new SignatureAttribute(cp.GetEntry <ConstantPoolUtf8>(tmp).Value); break; case RuntimeVisibleAnnotationsAttribute.AttributeName: attr = new RuntimeVisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp)); break; case RuntimeInvisibleAnnotationsAttribute.AttributeName: attr = new RuntimeInvisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp)); break; case RuntimeVisibleParameterAnnotationsAttribute.AttributeName: attr = new RuntimeVisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp)); break; case RuntimeInvisibleParameterAnnotationsAttribute.AttributeName: attr = new RuntimeInvisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp)); break; case AnnotationDefaultAttribute.AttributeName: attr = new AnnotationDefaultAttribute(ReadElementValue(cp)); break; default: stream.Skip(length); attr = new UnknownAttribute(name); break; } provider.Add(attr); } provider.AttributesLoaded(); }