IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Module.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2013 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;
27
30
31namespace IKVM.Reflection
32{
33
34 internal abstract class Module : ICustomAttributeProvider
35 {
36
37 readonly Universe universe;
38 internal readonly ModuleTable ModuleTable = new ModuleTable();
39 internal readonly TypeRefTable TypeRefTable = new TypeRefTable();
40 internal readonly TypeDefTable TypeDefTable = new TypeDefTable();
41 internal readonly FieldPtrTable FieldPtrTable = new FieldPtrTable();
42 internal readonly FieldTable FieldTable = new FieldTable();
43 internal readonly MemberRefTable MemberRefTable = new MemberRefTable();
44 internal readonly ConstantTable ConstantTable = new ConstantTable();
48 internal readonly ClassLayoutTable ClassLayoutTable = new ClassLayoutTable();
49 internal readonly FieldLayoutTable FieldLayoutTable = new FieldLayoutTable();
50 internal readonly ParamPtrTable ParamPtrTable = new ParamPtrTable();
51 internal readonly ParamTable ParamTable = new ParamTable();
54 internal readonly EventMapTable EventMapTable = new EventMapTable();
55 internal readonly EventPtrTable EventPtrTable = new EventPtrTable();
56 internal readonly EventTable EventTable = new EventTable();
57 internal readonly PropertyMapTable PropertyMapTable = new PropertyMapTable();
58 internal readonly PropertyPtrTable PropertyPtrTable = new PropertyPtrTable();
59 internal readonly PropertyTable PropertyTable = new PropertyTable();
61 internal readonly MethodImplTable MethodImplTable = new MethodImplTable();
62 internal readonly ModuleRefTable ModuleRefTable = new ModuleRefTable();
63 internal readonly TypeSpecTable TypeSpecTable = new TypeSpecTable();
64 internal readonly ImplMapTable ImplMapTable = new ImplMapTable();
65 internal readonly FieldRVATable FieldRVATable = new FieldRVATable();
66 internal readonly AssemblyTable AssemblyTable = new AssemblyTable();
67 internal readonly AssemblyRefTable AssemblyRefTable = new AssemblyRefTable();
68 internal readonly MethodPtrTable MethodPtrTable = new MethodPtrTable();
69 internal readonly MethodDefTable MethodDefTable = new MethodDefTable();
70 internal readonly NestedClassTable NestedClassTable = new NestedClassTable();
71 internal readonly FileTable FileTable = new FileTable();
75 internal readonly MethodSpecTable MethodSpecTable = new MethodSpecTable();
76 internal readonly GenericParamConstraintTable GenericParamConstraint = new GenericParamConstraintTable();
77
82 internal Module(Universe universe)
83 {
84 this.universe = universe;
85 }
86
90 public Universe Universe => universe;
91
92 internal Table[] GetTables()
93 {
94 var tables = new Table[64];
95 tables[ModuleTable.Index] = ModuleTable;
96 tables[TypeRefTable.Index] = TypeRefTable;
97 tables[TypeDefTable.Index] = TypeDefTable;
98 tables[FieldPtrTable.Index] = FieldPtrTable;
99 tables[FieldTable.Index] = FieldTable;
100 tables[MemberRefTable.Index] = MemberRefTable;
101 tables[ConstantTable.Index] = ConstantTable;
103 tables[FieldMarshalTable.Index] = FieldMarshalTable;
104 tables[DeclSecurityTable.Index] = DeclSecurityTable;
105 tables[ClassLayoutTable.Index] = ClassLayoutTable;
106 tables[FieldLayoutTable.Index] = FieldLayoutTable;
107 tables[ParamPtrTable.Index] = ParamPtrTable;
108 tables[ParamTable.Index] = ParamTable;
111 tables[EventMapTable.Index] = EventMapTable;
112 tables[EventPtrTable.Index] = EventPtrTable;
113 tables[EventTable.Index] = EventTable;
114 tables[PropertyMapTable.Index] = PropertyMapTable;
115 tables[PropertyPtrTable.Index] = PropertyPtrTable;
116 tables[PropertyTable.Index] = PropertyTable;
118 tables[MethodImplTable.Index] = MethodImplTable;
119 tables[ModuleRefTable.Index] = ModuleRefTable;
120 tables[TypeSpecTable.Index] = TypeSpecTable;
121 tables[ImplMapTable.Index] = ImplMapTable;
122 tables[FieldRVATable.Index] = FieldRVATable;
123 tables[AssemblyTable.Index] = AssemblyTable;
124 tables[AssemblyRefTable.Index] = AssemblyRefTable;
125 tables[MethodPtrTable.Index] = MethodPtrTable;
126 tables[MethodDefTable.Index] = MethodDefTable;
127 tables[NestedClassTable.Index] = NestedClassTable;
128 tables[FileTable.Index] = FileTable;
129 tables[ExportedTypeTable.Index] = ExportedTypeTable;
131 tables[GenericParamTable.Index] = GenericParamTable;
132 tables[MethodSpecTable.Index] = MethodSpecTable;
133 tables[GenericParamConstraintTable.Index] = GenericParamConstraint;
134 return tables;
135 }
136
137 public virtual void __GetDataDirectoryEntry(int index, out int rva, out int length)
138 {
139 throw new NotSupportedException();
140 }
141
142 public virtual long __RelativeVirtualAddressToFileOffset(int rva)
143 {
144 throw new NotSupportedException();
145 }
146
147 public bool __GetSectionInfo(int rva, out string name, out int characteristics)
148 {
149 return __GetSectionInfo(rva, out name, out characteristics, out _, out _, out _, out _);
150 }
151
152 public virtual bool __GetSectionInfo(int rva, out string name, out int characteristics, out int virtualAddress, out int virtualSize, out int pointerToRawData, out int sizeOfRawData)
153 {
154 throw new NotSupportedException();
155 }
156
157 public virtual int __ReadDataFromRVA(int rva, byte[] data, int offset, int length)
158 {
159 throw new NotSupportedException();
160 }
161
162 public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
163 {
164 throw new NotSupportedException();
165 }
166
167 public virtual int __Subsystem
168 {
169 get { throw new NotSupportedException(); }
170 }
171
172 public FieldInfo GetField(string name)
173 {
174 return GetField(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
175 }
176
177 public FieldInfo GetField(string name, BindingFlags bindingFlags)
178 {
179 return IsResource() ? null : GetModuleType().GetField(name, bindingFlags | BindingFlags.DeclaredOnly);
180 }
181
182 public FieldInfo[] GetFields()
183 {
184 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
185 }
186
187 public FieldInfo[] GetFields(BindingFlags bindingFlags)
188 {
189 return IsResource() ? Array.Empty<FieldInfo>() : GetModuleType().GetFields(bindingFlags | BindingFlags.DeclaredOnly);
190 }
191
192 public MethodInfo GetMethod(string name)
193 {
194 return IsResource() ? null : GetModuleType().GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
195 }
196
197 public MethodInfo GetMethod(string name, Type[] types)
198 {
199 return IsResource() ? null : GetModuleType().GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, types, null);
200 }
201
202 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
203 {
204 return IsResource() ? null : GetModuleType().GetMethod(name, bindingAttr | BindingFlags.DeclaredOnly, binder, callConv, types, modifiers);
205 }
206
207 public MethodInfo[] GetMethods()
208 {
209 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
210 }
211
212 public MethodInfo[] GetMethods(BindingFlags bindingFlags)
213 {
214 return IsResource() ? Array.Empty<MethodInfo>() : GetModuleType().GetMethods(bindingFlags | BindingFlags.DeclaredOnly);
215 }
216
217 public ConstructorInfo __ModuleInitializer
218 {
219 get { return IsResource() ? null : GetModuleType().TypeInitializer; }
220 }
221
222 public virtual byte[] ResolveSignature(int metadataToken)
223 {
224 throw new NotSupportedException();
225 }
226
227 public virtual __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
228 {
229 throw new NotSupportedException();
230 }
231
232 public virtual CustomModifiers __ResolveTypeSpecCustomModifiers(int typeSpecToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
233 {
234 throw new NotSupportedException();
235 }
236
237 public int MetadataToken
238 {
239 get { return IsResource() ? 0 : 1; }
240 }
241
242 public abstract int MDStreamVersion { get; }
243
244 public abstract Assembly Assembly { get; }
245
246 public abstract string FullyQualifiedName { get; }
247
248 public abstract string Name { get; }
249
250 public abstract Guid ModuleVersionId { get; }
251
252 public abstract MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
253
254 public abstract FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
255
256 public abstract MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
257
258
259 public abstract string ResolveString(int metadataToken);
260
261 public abstract Type[] __ResolveOptionalParameterTypes(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments, out CustomModifiers[] customModifiers);
262
263 public abstract string ScopeName { get; }
264
265 internal abstract void GetTypesImpl(List<Type> list);
266
267 internal abstract Type FindType(TypeName name);
268
269 internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
270
271 [Obsolete("Please use __ResolveOptionalParameterTypes(int, Type[], Type[], out CustomModifiers[]) instead.")]
272 public Type[] __ResolveOptionalParameterTypes(int metadataToken)
273 {
274 CustomModifiers[] dummy;
275 return __ResolveOptionalParameterTypes(metadataToken, null, null, out dummy);
276 }
277
278 public Type GetType(string className)
279 {
280 return GetType(className, false, false);
281 }
282
283 public Type GetType(string className, bool ignoreCase)
284 {
285 return GetType(className, false, ignoreCase);
286 }
287
288 public Type GetType(string className, bool throwOnError, bool ignoreCase)
289 {
290 var parser = TypeNameParser.Parse(className, throwOnError);
291 if (parser.Error)
292 return null;
293
294 if (parser.AssemblyName != null)
295 {
296 if (throwOnError)
297 throw new ArgumentException("Type names passed to Module.GetType() must not specify an assembly.");
298 else
299 return null;
300 }
301
302 var typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
303 var type = ignoreCase ? FindTypeIgnoreCase(typeName.ToLowerInvariant()) : FindType(typeName);
304 if (type == null && __IsMissing)
305 throw new MissingModuleException((MissingModule)this);
306
307 return parser.Expand(type, this, throwOnError, className, false, ignoreCase);
308 }
309
310 public Type[] GetTypes()
311 {
312 var list = new List<Type>();
313 GetTypesImpl(list);
314 return list.ToArray();
315 }
316
317 public Type[] FindTypes(TypeFilter filter, object filterCriteria)
318 {
319 var list = new List<Type>();
320 foreach (var type in GetTypes())
321 if (filter(type, filterCriteria))
322 list.Add(type);
323
324 return list.ToArray();
325 }
326
327 public virtual bool IsResource()
328 {
329 return false;
330 }
331
332 public Type ResolveType(int metadataToken)
333 {
334 return ResolveType(metadataToken, null, null);
335 }
336
337 internal sealed class GenericContext : IGenericContext
338 {
339
340 readonly Type[] genericTypeArguments;
341 readonly Type[] genericMethodArguments;
342
348 internal GenericContext(Type[] genericTypeArguments, Type[] genericMethodArguments)
349 {
350 this.genericTypeArguments = genericTypeArguments;
351 this.genericMethodArguments = genericMethodArguments;
352 }
353
354 public Type GetGenericTypeArgument(int index)
355 {
356 return genericTypeArguments[index];
357 }
358
359 public Type GetGenericMethodArgument(int index)
360 {
361 return genericMethodArguments[index];
362 }
363
364 }
365
366 public Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
367 {
368 if ((metadataToken >> 24) == TypeSpecTable.Index)
369 return ResolveType(metadataToken, new GenericContext(genericTypeArguments, genericMethodArguments));
370 else
371 return ResolveType(metadataToken, null);
372 }
373
374 internal abstract Type ResolveType(int metadataToken, IGenericContext context);
375
376 public MethodBase ResolveMethod(int metadataToken)
377 {
378 return ResolveMethod(metadataToken, null, null);
379 }
380
381 public FieldInfo ResolveField(int metadataToken)
382 {
383 return ResolveField(metadataToken, null, null);
384 }
385
386 public MemberInfo ResolveMember(int metadataToken)
387 {
388 return ResolveMember(metadataToken, null, null);
389 }
390
391 public bool IsDefined(Type attributeType, bool inherit)
392 {
393 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
394 }
395
396 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
397 {
398 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
399 }
400
401 public IList<CustomAttributeData> GetCustomAttributesData()
402 {
403 return CustomAttributeData.GetCustomAttributes(this);
404 }
405
406 public IEnumerable<CustomAttributeData> CustomAttributes
407 {
408 get { return GetCustomAttributesData(); }
409 }
410
411 public virtual IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
412 {
413 return Array.Empty<CustomAttributeData>();
414 }
415
416 public abstract AssemblyName[] __GetReferencedAssemblies();
417
418 public virtual void __ResolveReferencedAssemblies(Assembly[] assemblies)
419 {
420 throw new NotSupportedException();
421 }
422
423 public abstract string[] __GetReferencedModules();
424
425 public abstract Type[] __GetReferencedTypes();
426
427 public abstract Type[] __GetExportedTypes();
428
429 public virtual bool __IsMissing => false;
430
431 public ulong __ImageBase => GetImageBaseImpl();
432
433 protected abstract ulong GetImageBaseImpl();
434
435 public ulong __StackReserve => GetStackReserveImpl();
436
437 protected abstract ulong GetStackReserveImpl();
438
439 public uint __FileAlignment => GetFileAlignmentImpl();
440
441 protected abstract uint GetFileAlignmentImpl();
442
443 public DllCharacteristics __DllCharacteristics => GetDllCharacteristicsImpl();
444
445 protected abstract DllCharacteristics GetDllCharacteristicsImpl();
446
447 public virtual byte[] __ModuleHash => throw new NotSupportedException();
448
449 public virtual int __EntryPointRVA => throw new NotSupportedException();
450
451 public virtual int __EntryPointToken => throw new NotSupportedException();
452
453 public virtual string __ImageRuntimeVersion => throw new NotSupportedException();
454
455 [Obsolete]
456 public List<CustomAttributeData> __GetCustomAttributesFor(int token)
457 {
458 return CustomAttributeData.GetCustomAttributesImpl(new List<CustomAttributeData>(), this, token, null);
459 }
460
461 public bool __TryGetImplMap(int token, out ImplMapFlags mappingFlags, out string importName, out string importScope)
462 {
463 foreach (int i in ImplMapTable.Filter(token))
464 {
465 mappingFlags = (ImplMapFlags)(ushort)ImplMapTable.records[i].MappingFlags;
466 importName = GetString(ImplMapTable.records[i].ImportName);
467 importScope = GetString(ModuleRefTable.records[(ImplMapTable.records[i].ImportScope & 0xFFFFFF) - 1]);
468 return true;
469 }
470
471 mappingFlags = 0;
472 importName = null;
473 importScope = null;
474 return false;
475 }
476
477 public virtual System.Security.Cryptography.X509Certificates.X509Certificate GetSignerCertificate()
478 {
479 return null;
480 }
481
482 internal abstract Type GetModuleType();
483
484 internal IList<CustomAttributeData> GetDeclarativeSecurity(int metadataToken)
485 {
486 var list = new List<CustomAttributeData>();
487 foreach (var i in DeclSecurityTable.Filter(metadataToken))
488 CustomAttributeData.ReadDeclarativeSecurity(this, i, list);
489
490 return list;
491 }
492
493 internal virtual void Dispose()
494 {
495
496 }
497
498 internal virtual void ExportTypes(AssemblyFileHandle handle, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
499 {
500
501 }
502
503 internal virtual string GetString(StringHandle handle)
504 {
505 throw new NotImplementedException();
506 }
507
508 internal virtual ByteReader GetBlobReader(BlobHandle handle)
509 {
510 throw new NotImplementedException();
511 }
512
513 }
514
515 internal delegate bool TypeFilter(Type m, object filterCriteria);
516
517 internal delegate bool MemberFilter(MemberInfo m, object filterCriteria);
518
519}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.FieldInfo FieldInfo
IKVM.Reflection.MemberInfo MemberInfo
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Reflection.MethodBase MethodBase
global::java.lang.invoke.LambdaForm.Name Name