IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ReflectionModuleSymbol.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Immutable;
3using System.Diagnostics;
4using System.Linq;
5using System.Reflection;
6using System.Reflection.Metadata.Ecma335;
7using System.Threading;
8
10{
11
16 {
17
23 static bool IsTypeDefinition(Type type)
24 {
25#if NET
26 return type.IsTypeDefinition;
27#else
28 return type.HasElementType == false && type.IsConstructedGenericType == false && type.IsGenericParameter == false;
29#endif
30 }
31
32 readonly Module _underlyingModule;
33
34 Type[]? _typesSource;
35 int _typesBaseRow;
36 ReflectionTypeSymbol?[]? _types;
37
44 public ReflectionModuleSymbol(ReflectionSymbolContext context, Module underlyingModule) :
45 base(context)
46 {
47 _underlyingModule = underlyingModule ?? throw new ArgumentNullException(nameof(underlyingModule));
48 }
49
53 internal Module UnderlyingModule => _underlyingModule;
54
61 internal ReflectionTypeSymbol GetOrCreateTypeSymbol(Type type)
62 {
63 if (type is null)
64 throw new ArgumentNullException(nameof(type));
65
66 Debug.Assert(type.Module == _underlyingModule);
67
68 // type is not a definition, but is substituted
69 if (IsTypeDefinition(type) == false)
70 return GetOrCreateTypeSymbolForSpecification(type);
71
72 // look up handle and row
73 var hnd = MetadataTokens.TypeDefinitionHandle(type.MetadataToken);
74 var row = MetadataTokens.GetRowNumber(hnd);
75
76 // initialize source table
77 if (_typesSource == null)
78 {
79 Interlocked.CompareExchange(ref _typesSource, _underlyingModule.GetTypes().OrderBy(i => i.MetadataToken).ToArray(), null);
80 _typesBaseRow = _typesSource.Length != 0 ? MetadataTokens.GetRowNumber(MetadataTokens.MethodDefinitionHandle(_typesSource[0].MetadataToken)) : 0;
81 }
82
83 // initialize cache table
84 if (_types == null)
85 Interlocked.CompareExchange(ref _types, new ReflectionTypeSymbol?[_typesSource.Length], null);
86
87 // index of current record is specified row - base
88 var idx = row - _typesBaseRow;
89 if (idx < 0)
90 throw new Exception();
91
92 Debug.Assert(idx >= 0);
93 Debug.Assert(idx < _typesSource.Length);
94
95 // check that our type list is long enough to contain the entire table
96 if (_types.Length < idx)
97 throw new IndexOutOfRangeException();
98
99 // if not yet created, create, allow multiple instances, but only one is eventually inserted
100 if (_types[idx] == null)
101 Interlocked.CompareExchange(ref _types[idx], new ReflectionTypeSymbol(Context, this, type), null);
102
103 // this should never happen
104 if (_types[idx] is not ReflectionTypeSymbol sym)
105 throw new InvalidOperationException();
106
107 return sym;
108 }
109
115 ReflectionTypeSymbol GetOrCreateTypeSymbolForSpecification(Type type)
116 {
117 if (type is null)
118 throw new ArgumentNullException(nameof(type));
119
120 Debug.Assert(type.Module == _underlyingModule);
121
122 if (type.GetElementType() is { } elementType)
123 {
124 var elementTypeSymbol = GetOrCreateTypeSymbol(elementType);
125
126 // handles both SZ arrays and normal arrays
127 if (type.IsArray)
128 return (ReflectionTypeSymbol)elementTypeSymbol.MakeArrayType(type.GetArrayRank());
129
130 if (type.IsPointer)
131 return (ReflectionTypeSymbol)elementTypeSymbol.MakePointerType();
132
133 if (type.IsByRef)
134 return (ReflectionTypeSymbol)elementTypeSymbol.MakeByRefType();
135
136 throw new InvalidOperationException();
137 }
138
139 if (type.IsGenericType)
140 {
141 var definitionType = type.GetGenericTypeDefinition();
142 var definitionTypeSymbol = GetOrCreateTypeSymbol(definitionType);
143 return definitionTypeSymbol.GetOrCreateGenericTypeSymbol(type.GetGenericArguments());
144 }
145
146 // generic type parameter
147 if (type.IsGenericParameter && type.DeclaringMethod is null && type.DeclaringType is not null)
148 {
149 var declaringType = GetOrCreateTypeSymbol(type.DeclaringType);
150 return declaringType.GetOrCreateGenericParameterSymbol(type);
151 }
152
153 // generic method parameter
154 if (type.IsGenericParameter && type.DeclaringMethod is not null && type.DeclaringMethod.DeclaringType is not null)
155 {
156 var declaringMethod = GetOrCreateTypeSymbol(type.DeclaringMethod.DeclaringType);
157 return declaringMethod.GetOrCreateGenericParameterSymbol(type);
158 }
159
160 throw new InvalidOperationException();
161 }
162
164 public IAssemblySymbol Assembly => Context.GetOrCreateAssemblySymbol(_underlyingModule.Assembly);
165
167 public string FullyQualifiedName => _underlyingModule.FullyQualifiedName;
168
170 public int MetadataToken => _underlyingModule.MetadataToken;
171
173 public Guid ModuleVersionId => _underlyingModule.ModuleVersionId;
174
176 public string Name => _underlyingModule.Name;
177
179 public string ScopeName => _underlyingModule.ScopeName;
180
182 public IFieldSymbol? GetField(string name)
183 {
184 return _underlyingModule.GetField(name) is { } f ? ResolveFieldSymbol(f) : null;
185 }
186
188 public IFieldSymbol? GetField(string name, BindingFlags bindingAttr)
189 {
190 return _underlyingModule.GetField(name, bindingAttr) is { } f ? ResolveFieldSymbol(f) : null;
191 }
192
194 public IFieldSymbol[] GetFields(BindingFlags bindingFlags)
195 {
196 return ResolveFieldSymbols(_underlyingModule.GetFields(bindingFlags));
197 }
198
201 {
202 return ResolveFieldSymbols(_underlyingModule.GetFields());
203 }
204
206 public IMethodSymbol? GetMethod(string name)
207 {
208 return _underlyingModule.GetMethod(name) is { } m ? ResolveMethodSymbol(m) : null;
209 }
210
212 public IMethodSymbol? GetMethod(string name, ITypeSymbol[] types)
213 {
214 return _underlyingModule.GetMethod(name, UnpackTypeSymbols(types)) is { } m ? ResolveMethodSymbol(m) : null;
215 }
216
218 public IMethodSymbol? GetMethod(string name, BindingFlags bindingAttr, CallingConventions callConvention, ITypeSymbol[] types, ParameterModifier[]? modifiers)
219 {
220 return _underlyingModule.GetMethod(name, bindingAttr, null, callConvention, UnpackTypeSymbols(types), modifiers) is { } m ? ResolveMethodSymbol(m) : null;
221 }
222
225 {
226 return ResolveMethodSymbols(_underlyingModule.GetMethods());
227 }
228
230 public IMethodSymbol[] GetMethods(BindingFlags bindingFlags)
231 {
232 return ResolveMethodSymbols(_underlyingModule.GetMethods(bindingFlags));
233 }
234
236 public ITypeSymbol? GetType(string className)
237 {
238 return _underlyingModule.GetType(className) is { } t ? ResolveTypeSymbol(t) : null;
239 }
240
242 public ITypeSymbol? GetType(string className, bool ignoreCase)
243 {
244 return _underlyingModule.GetType(className, ignoreCase) is { } t ? ResolveTypeSymbol(t) : null;
245 }
246
248 public ITypeSymbol? GetType(string className, bool throwOnError, bool ignoreCase)
249 {
250 return _underlyingModule.GetType(className, throwOnError, ignoreCase) is { } t ? ResolveTypeSymbol(t) : null;
251 }
252
255 {
256 return ResolveTypeSymbols(_underlyingModule.GetTypes());
257 }
258
260 public bool IsResource()
261 {
262 return _underlyingModule.IsResource();
263 }
264
266 public IFieldSymbol? ResolveField(int metadataToken)
267 {
268 return _underlyingModule.ResolveField(metadataToken) is { } f ? ResolveFieldSymbol(f) : null;
269 }
270
272 public IFieldSymbol? ResolveField(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
273 {
274 var _genericTypeArguments = genericTypeArguments != null ? UnpackTypeSymbols(genericTypeArguments) : null;
275 var _genericMethodArguments = genericMethodArguments != null ? UnpackTypeSymbols(genericMethodArguments) : null;
276 return _underlyingModule.ResolveField(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } f ? ResolveFieldSymbol(f) : null;
277 }
278
280 public IMemberSymbol? ResolveMember(int metadataToken)
281 {
282 return _underlyingModule.ResolveMember(metadataToken) is { } m ? ResolveMemberSymbol(m) : null;
283 }
284
286 public IMemberSymbol? ResolveMember(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
287 {
288 var _genericTypeArguments = genericTypeArguments != null ? UnpackTypeSymbols(genericTypeArguments) : null;
289 var _genericMethodArguments = genericMethodArguments != null ? UnpackTypeSymbols(genericMethodArguments) : null;
290 return _underlyingModule.ResolveMember(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ? ResolveMemberSymbol(m) : null;
291 }
292
294 public IMethodBaseSymbol? ResolveMethod(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
295 {
296 var _genericTypeArguments = genericTypeArguments != null ? UnpackTypeSymbols(genericTypeArguments) : null;
297 var _genericMethodArguments = genericMethodArguments != null ? UnpackTypeSymbols(genericMethodArguments) : null;
298 return _underlyingModule.ResolveMethod(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ? ResolveMethodBaseSymbol(m) : null;
299 }
300
302 public IMethodBaseSymbol? ResolveMethod(int metadataToken)
303 {
304 return _underlyingModule.ResolveMethod(metadataToken) is { } m ? ResolveMethodBaseSymbol(m) : null;
305 }
306
308 public byte[] ResolveSignature(int metadataToken)
309 {
310 return _underlyingModule.ResolveSignature(metadataToken);
311 }
312
314 public string ResolveString(int metadataToken)
315 {
316 return _underlyingModule.ResolveString(metadataToken);
317 }
318
320 public ITypeSymbol ResolveType(int metadataToken)
321 {
322 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken));
323 }
324
326 public ITypeSymbol ResolveType(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
327 {
328 var _genericTypeArguments = genericTypeArguments != null ? UnpackTypeSymbols(genericTypeArguments) : null;
329 var _genericMethodArguments = genericMethodArguments != null ? UnpackTypeSymbols(genericMethodArguments) : null;
330 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken, _genericTypeArguments, _genericMethodArguments));
331 }
332
334 public ImmutableArray<CustomAttributeSymbol> GetCustomAttributes()
335 {
336 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData());
337 }
338
340 public ImmutableArray<CustomAttributeSymbol> GetCustomAttributes(ITypeSymbol attributeType)
341 {
342 var underlyingAttributeType = ((ReflectionTypeSymbol)attributeType).UnderlyingType;
343 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData(), i => underlyingAttributeType.IsAssignableFrom(i.AttributeType));
344 }
345
347 public bool IsDefined(ITypeSymbol attributeType)
348 {
349 return _underlyingModule.IsDefined(((ReflectionTypeSymbol)attributeType).UnderlyingType);
350 }
351
352 }
353
354}
System.Threading.Interlocked Interlocked
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
global::java.lang.invoke.LambdaForm.Name Name
Implementation of IModuleSymbol derived from System.Reflection.
bool IsResource()
Gets a value indicating whether the object is a resource.
IMethodSymbol? GetMethod(string name, ITypeSymbol[] types)
Returns a method having the specified name and parameter types.
IMethodSymbol[] GetMethods()
Returns the global methods defined on the module.
ITypeSymbol[] GetTypes()
Returns all the types defined within this module.
ITypeSymbol? GetType(string className, bool throwOnError, bool ignoreCase)
Returns the specified type, specifying whether to make a case-sensitive search of the module and whet...
IFieldSymbol? GetField(string name, BindingFlags bindingAttr)
Returns a field having the specified name and binding attributes.
int MetadataToken
Gets a token that identifies the module in metadata.
IFieldSymbol? ResolveField(int metadataToken)
Returns the field identified by a metadata token.
IFieldSymbol? ResolveField(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the field identified by the specified metadata token, in the context defined by the specified...
IFieldSymbol[] GetFields()
Returns the global fields defined on the module.
ITypeSymbol ResolveType(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type identified by the specified metadata token, in the context defined by the specified ...
IMethodBaseSymbol? ResolveMethod(int metadataToken)
Returns the method or constructor identified by the specified metadata token.
IFieldSymbol? GetField(string name)
Returns a field having the specified name.
ReflectionModuleSymbol(ReflectionSymbolContext context, Module underlyingModule)
Initializes a new instance.
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes()
Returns an array of all of the custom attributes defined on this member, excluding named attributes,...
IMemberSymbol? ResolveMember(int metadataToken)
Returns the type or member identified by a metadata token.
string FullyQualifiedName
Gets a string representing the fully qualified name and path to this module.
ITypeSymbol ResolveType(int metadataToken)
Returns the type identified by the specified metadata token.
string ScopeName
Gets a string representing the name of the module.
IMethodSymbol? GetMethod(string name, BindingFlags bindingAttr, CallingConventions callConvention, ITypeSymbol[] types, ParameterModifier[]? modifiers)
Returns a method having the specified name, binding information, calling convention,...
IMethodSymbol[] GetMethods(BindingFlags bindingFlags)
Returns the global methods defined on the module that match the specified binding flags.
bool IsDefined(ITypeSymbol attributeType)
Indicates whether one or more instance of attributeType is defined on this member.
IFieldSymbol[] GetFields(BindingFlags bindingFlags)
Returns the global fields defined on the module that match the specified binding flags.
Guid ModuleVersionId
Gets a universally unique identifier (UUID) that can be used to distinguish between two versions of a...
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes(ITypeSymbol attributeType)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
byte[] ResolveSignature(int metadataToken)
Returns the signature blob identified by a metadata token.
string ResolveString(int metadataToken)
Returns the string identified by the specified metadata token.
IMemberSymbol? ResolveMember(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type or member identified by the specified metadata token, in the context defined by the ...
IMethodBaseSymbol? ResolveMethod(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the method or constructor identified by the specified metadata token, in the context defined ...
IMethodSymbol? GetMethod(string name)
Returns a method having the specified name.
ITypeSymbol? GetType(string className)
Returns the specified type, performing a case-sensitive search.
ITypeSymbol? GetType(string className, bool ignoreCase)
Returns the specified type, searching the module with the specified case sensitivity.
Holds references to symbols derived from System.Reflection.
ReflectionAssemblySymbol GetOrCreateAssemblySymbol(Assembly assembly)
Gets or creates a ReflectionAssemblySymbol for the specified Assembly.
virtual ReflectionMemberSymbol ResolveMemberSymbol(MemberInfo member)
Resolves the symbol for the specified type.
ReflectionSymbolContext Context
Gets the associated ReflectionSymbolContext.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Discovers the attributes of a field and provides access to field metadata.
Obtains information about the attributes of a member and provides access to member metadata.
Provides information about methods and constructors.
Discovers the attributes of a method and provides access to method metadata.
Performs reflection on a module.
Represents type declarations: class types, interface types, array types, value types,...