IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
FieldBuilder.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2012 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Reflection.Metadata;
26
29
31{
32
33 internal sealed class FieldBuilder : FieldInfo
34 {
35
36 readonly TypeBuilder type;
37 readonly string name;
38 readonly int pseudoToken;
39 FieldAttributes attributes;
40 readonly FieldSignature signature;
41 int offset = -1;
42
43 BlobHandle signatureBlobHandle;
44
53 internal FieldBuilder(TypeBuilder type, string name, Type fieldType, CustomModifiers customModifiers, FieldAttributes attributes)
54 {
55 this.type = type;
56 this.name = name;
57 this.attributes = attributes;
58 this.pseudoToken = type.ModuleBuilder.AllocPseudoToken();
59 this.signature = FieldSignature.Create(fieldType, customModifiers);
60 this.type.ModuleBuilder.FieldTable.AddVirtualRecord();
61
62 // create signature blob
63 var buf = new ByteBuffer(5);
64 signature.Write(type.ModuleBuilder, buf);
65 signatureBlobHandle = type.ModuleBuilder.GetOrAddBlob(buf.ToArray());
66 }
67
68 public void SetConstant(object defaultValue)
69 {
70 attributes |= FieldAttributes.HasDefault;
71 type.ModuleBuilder.AddConstant(pseudoToken, defaultValue);
72 }
73
74 public override object GetRawConstantValue()
75 {
76 if (!type.IsCreated())
77 {
78 // the .NET FieldBuilder doesn't support this method
79 // (since we dont' have a different FieldInfo object after baking, we will support it once we're baked)
80 throw new NotSupportedException();
81 }
82
83 return type.Module.ConstantTable.GetRawConstantValue(type.Module, GetCurrentToken());
84 }
85
86 public override bool __TryGetFieldOffset(out int offset)
87 {
88 return (offset = this.offset) != -1;
89 }
90
91 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
92 {
93 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
94 }
95
96 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
97 {
98 switch (customBuilder.KnownCA)
99 {
100 case KnownCA.FieldOffsetAttribute:
101 SetOffset((int)customBuilder.DecodeBlob(Module.Assembly).GetConstructorArgument(0));
102 break;
103 case KnownCA.MarshalAsAttribute:
104 FieldMarshal.SetMarshalAsAttribute(type.ModuleBuilder, pseudoToken, customBuilder);
105 attributes |= FieldAttributes.HasFieldMarshal;
106 break;
107 case KnownCA.NonSerializedAttribute:
108 attributes |= FieldAttributes.NotSerialized;
109 break;
110 case KnownCA.SpecialNameAttribute:
111 attributes |= FieldAttributes.SpecialName;
112 break;
113 default:
114 type.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
115 break;
116 }
117 }
118
119 public void SetOffset(int iOffset)
120 {
121 offset = iOffset;
122 }
123
124 public override FieldAttributes Attributes
125 {
126 get { return attributes; }
127 }
128
129 public override Type DeclaringType
130 {
131 get { return type.IsModulePseudoType ? null : type; }
132 }
133
134 public override string Name
135 {
136 get { return name; }
137 }
138
139 public override int MetadataToken
140 {
141 get { return pseudoToken; }
142 }
143
144 public override Module Module
145 {
146 get { return type.Module; }
147 }
148
149 public FieldToken GetToken()
150 {
151 return new FieldToken(pseudoToken);
152 }
153
154 internal void WriteFieldRecords()
155 {
156 type.ModuleBuilder.Metadata.AddFieldDefinition(
157 (System.Reflection.FieldAttributes)attributes,
158 type.ModuleBuilder.GetOrAddString(name),
159 signatureBlobHandle);
160 }
161
162 internal void FixupToken(int token)
163 {
164 if (offset > -1)
165 {
166 var rec = new FieldLayoutTable.Record();
167 rec.Offset = offset;
168 rec.Field = pseudoToken;
169 type.ModuleBuilder.FieldLayoutTable.AddRecord(rec);
170 }
171
172 type.ModuleBuilder.RegisterTokenFixup(pseudoToken, token);
173 }
174
175 internal override FieldSignature FieldSignature
176 {
177 get { return signature; }
178 }
179
180 internal override int ImportTo(ModuleBuilder other)
181 {
182 return other.ImportMethodOrField(type, name, signature);
183 }
184
185 internal override int GetCurrentToken()
186 {
187 if (type.ModuleBuilder.IsSaved)
188 return type.ModuleBuilder.ResolvePseudoToken(pseudoToken);
189 else
190 return pseudoToken;
191 }
192
193 internal override bool IsBaked
194 {
195 get { return type.IsBaked; }
196 }
197
198 }
199
200}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.FieldInfo FieldInfo
global::java.lang.invoke.LambdaForm.Name Name
KnownCA
These are the pseudo-custom attributes that are recognized by name by the runtime (i....
Definition KnownCA.cs:35