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