IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
PropertyBuilder.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2011 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.Collections.Generic;
26using System.Reflection.Metadata.Ecma335;
27
29
31{
32
33 internal sealed class PropertyBuilder : PropertyInfo
34 {
35
36 readonly TypeBuilder typeBuilder;
37 readonly string name;
38 PropertyAttributes attributes;
40 MethodBuilder getter;
41 MethodBuilder setter;
42 readonly List<Accessor> accessors = new List<Accessor>();
43 int lazyPseudoToken;
44 bool patchCallingConvention;
45
46 struct Accessor
47 {
48
49 internal short Semantics;
50 internal MethodBuilder Method;
51
52 }
53
62 internal PropertyBuilder(TypeBuilder typeBuilder, string name, PropertyAttributes attributes, PropertySignature sig, bool patchCallingConvention)
63 {
64 this.typeBuilder = typeBuilder;
65 this.name = name;
66 this.attributes = attributes;
67 this.sig = sig;
68 this.patchCallingConvention = patchCallingConvention;
69 }
70
71 internal override PropertySignature PropertySignature
72 {
73 get { return sig; }
74 }
75
76 public void SetGetMethod(MethodBuilder mdBuilder)
77 {
78 getter = mdBuilder;
79 Accessor acc;
80 acc.Semantics = MethodSemanticsTable.Getter;
81 acc.Method = mdBuilder;
82 accessors.Add(acc);
83 }
84
85 public void SetSetMethod(MethodBuilder mdBuilder)
86 {
87 setter = mdBuilder;
88 Accessor acc;
89 acc.Semantics = MethodSemanticsTable.Setter;
90 acc.Method = mdBuilder;
91 accessors.Add(acc);
92 }
93
94 public void AddOtherMethod(MethodBuilder mdBuilder)
95 {
96 Accessor acc;
97 acc.Semantics = MethodSemanticsTable.Other;
98 acc.Method = mdBuilder;
99 accessors.Add(acc);
100 }
101
102 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
103 {
104 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
105 }
106
107 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
108 {
109 if (customBuilder.KnownCA == KnownCA.SpecialNameAttribute)
110 {
111 attributes |= PropertyAttributes.SpecialName;
112 }
113 else
114 {
115 if (lazyPseudoToken == 0)
116 lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
117
118 typeBuilder.ModuleBuilder.SetCustomAttribute(lazyPseudoToken, customBuilder);
119 }
120 }
121
122 public override object GetRawConstantValue()
123 {
124 if (lazyPseudoToken != 0)
125 return typeBuilder.ModuleBuilder.ConstantTable.GetRawConstantValue(typeBuilder.ModuleBuilder, lazyPseudoToken);
126
127 throw new InvalidOperationException();
128 }
129
130 public override PropertyAttributes Attributes
131 {
132 get { return attributes; }
133 }
134
135 public override bool CanRead
136 {
137 get { return getter != null; }
138 }
139
140 public override bool CanWrite
141 {
142 get { return setter != null; }
143 }
144
145 public override MethodInfo GetGetMethod(bool nonPublic)
146 {
147 return nonPublic || (getter != null && getter.IsPublic) ? getter : null;
148 }
149
150 public override MethodInfo GetSetMethod(bool nonPublic)
151 {
152 return nonPublic || (setter != null && setter.IsPublic) ? setter : null;
153 }
154
155 public override MethodInfo[] GetAccessors(bool nonPublic)
156 {
157 var list = new List<MethodInfo>();
158 foreach (var acc in accessors)
159 AddAccessor(list, nonPublic, acc.Method);
160
161 return list.ToArray();
162 }
163
164 static void AddAccessor(List<MethodInfo> list, bool nonPublic, MethodInfo method)
165 {
166 if (method != null && (nonPublic || method.IsPublic))
167 list.Add(method);
168 }
169
170 public override Type DeclaringType
171 {
172 get { return typeBuilder; }
173 }
174
175 public override string Name
176 {
177 get { return name; }
178 }
179
180 public override Module Module
181 {
182 get { return typeBuilder.Module; }
183 }
184
185 public void SetConstant(object defaultValue)
186 {
187 if (lazyPseudoToken == 0)
188 lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
189
190 attributes |= PropertyAttributes.HasDefault;
191 typeBuilder.ModuleBuilder.AddConstant(lazyPseudoToken, defaultValue);
192 }
193
194 internal void Bake()
195 {
196 if (patchCallingConvention)
197 sig.HasThis = !this.IsStatic;
198
199 var rec = new PropertyTable.Record();
200 rec.Flags = (short)attributes;
201 rec.Name = typeBuilder.ModuleBuilder.GetOrAddString(name);
202 rec.Type = typeBuilder.ModuleBuilder.GetSignatureBlobIndex(sig);
203 int token = MetadataTokens.GetToken(MetadataTokens.PropertyDefinitionHandle(typeBuilder.ModuleBuilder.PropertyTable.AddRecord(rec)));
204
205 if (lazyPseudoToken == 0)
206 lazyPseudoToken = token;
207 else
208 typeBuilder.ModuleBuilder.RegisterTokenFixup(lazyPseudoToken, token);
209
210 foreach (var acc in accessors)
211 AddMethodSemantics(acc.Semantics, acc.Method.MetadataToken, token);
212 }
213
214 void AddMethodSemantics(short semantics, int methodToken, int propertyToken)
215 {
216 var rec = new MethodSemanticsTable.Record();
217 rec.Semantics = semantics;
218 rec.Method = methodToken;
219 rec.Association = propertyToken;
220 typeBuilder.ModuleBuilder.MethodSemanticsTable.AddRecord(rec);
221 }
222
223 internal override bool IsPublic
224 {
225 get
226 {
227 foreach (var acc in accessors)
228 if (acc.Method.IsPublic)
229 return true;
230
231 return false;
232 }
233 }
234
235 internal override bool IsNonPrivate
236 {
237 get
238 {
239 foreach (var acc in accessors)
240 if ((acc.Method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private)
241 return true;
242
243 return false;
244 }
245 }
246
247 internal override bool IsStatic
248 {
249 get
250 {
251 foreach (var acc in accessors)
252 if (acc.Method.IsStatic)
253 return true;
254
255 return false;
256 }
257 }
258
259 internal override bool IsBaked
260 {
261 get { return typeBuilder.IsBaked; }
262 }
263
264 internal override int GetCurrentToken()
265 {
266 if (typeBuilder.ModuleBuilder.IsSaved && ModuleBuilder.IsPseudoToken(lazyPseudoToken))
267 return typeBuilder.ModuleBuilder.ResolvePseudoToken(lazyPseudoToken);
268 else
269 return lazyPseudoToken;
270 }
271
272 public override int MetadataToken
273 {
274 get
275 {
276 if (lazyPseudoToken == 0)
277 lazyPseudoToken = typeBuilder.ModuleBuilder.AllocPseudoToken();
278
279 return GetCurrentToken();
280 }
281 }
282
283 }
284
285}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.PropertyInfo PropertyInfo
IKVM.Reflection.MethodInfo MethodInfo
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