IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IkvmReflectionMethodSymbol.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Threading;
6
9
11{
12
14 {
15
16 readonly MethodInfo _underlyingMethod;
17
18 Type[]? _genericParametersSource;
19 IkvmReflectionTypeSymbol?[]? _genericParameters;
20 List<(Type[] Arguments, IkvmReflectionMethodSymbol Symbol)>? _genericTypes;
21
30 base(context, module, type, underlyingMethod)
31 {
32 _underlyingMethod = underlyingMethod ?? throw new ArgumentNullException(nameof(underlyingMethod));
33 }
34
41 internal IkvmReflectionTypeSymbol GetOrCreateGenericParameterSymbol(Type genericParameterType)
42 {
43 if (genericParameterType is null)
44 throw new ArgumentNullException(nameof(genericParameterType));
45
46 Debug.Assert(genericParameterType.DeclaringMethod == _underlyingMethod);
47
48 // initialize tables
49 _genericParametersSource ??= _underlyingMethod.GetGenericArguments();
50 _genericParameters ??= new IkvmReflectionTypeSymbol?[_genericParametersSource.Length];
51
52 // position of the parameter in the list
53 var idx = genericParameterType.GenericParameterPosition;
54
55 // check that our list is long enough to contain the entire table
56 if (_genericParameters.Length < idx)
57 throw new IndexOutOfRangeException();
58
59 // if not yet created, create, allow multiple instances, but only one is eventually inserted
60 ref var rec = ref _genericParameters[idx];
61 if (rec == null)
62 Interlocked.CompareExchange(ref rec, new IkvmReflectionTypeSymbol(Context, ContainingModule, genericParameterType), null);
63
64 // this should never happen
65 if (rec is not IkvmReflectionTypeSymbol sym)
66 throw new InvalidOperationException();
67
68 return sym;
69 }
70
77 internal IkvmReflectionMethodSymbol GetOrCreateGenericTypeSymbol(Type[] genericMethodArguments)
78 {
79 if (genericMethodArguments is null)
80 throw new ArgumentNullException(nameof(genericMethodArguments));
81
82 if (_underlyingMethod.IsGenericMethodDefinition == false)
83 throw new InvalidOperationException();
84
85 // initialize the available parameters
86 _genericParametersSource ??= _underlyingMethod.GetGenericArguments();
87 if (_genericParametersSource.Length != genericMethodArguments.Length)
88 throw new InvalidOperationException();
89
90 // initialize generic type map, and lock on it since we're potentially adding items
91 _genericTypes ??= [];
92 lock (_genericTypes)
93 {
94 // find existing entry
95 foreach (var i in _genericTypes)
96 if (i.Arguments.SequenceEqual(genericMethodArguments))
97 return i.Symbol;
98
99 // generate new symbol
100 var sym = new IkvmReflectionMethodSymbol(Context, ContainingModule, ContainingType, _underlyingMethod.MakeGenericMethod(genericMethodArguments));
101 _genericTypes.Add((genericMethodArguments, sym));
102 return sym;
103 }
104 }
105
106 internal MethodInfo UnderlyingMethod => _underlyingMethod;
107
108 public IParameterSymbol ReturnParameter => ResolveParameterSymbol(_underlyingMethod.ReturnParameter);
109
110 public ITypeSymbol ReturnType => ResolveTypeSymbol(_underlyingMethod.ReturnType);
111
112 public ICustomAttributeSymbolProvider ReturnTypeCustomAttributes => throw new NotImplementedException();
113
115 {
116 return ResolveMethodSymbol(_underlyingMethod.GetBaseDefinition());
117 }
118
120 {
121 return ResolveMethodSymbol(_underlyingMethod.GetGenericMethodDefinition());
122 }
123
124 public IMethodSymbol MakeGenericMethod(params ITypeSymbol[] typeArguments)
125 {
126 return ResolveMethodSymbol(_underlyingMethod.MakeGenericMethod(UnpackTypeSymbols(typeArguments)));
127 }
128
129 }
130
131}
System.Threading.Interlocked Interlocked
IKVM.Reflection.Type Type
IKVM.Reflection.MethodInfo MethodInfo
IParameterSymbol ReturnParameter
Gets a IParameterSymbol object that contains information about the return type of the method,...
ICustomAttributeSymbolProvider ReturnTypeCustomAttributes
Gets the custom attributes for the return type.
IkvmReflectionMethodSymbol(IkvmReflectionSymbolContext context, IkvmReflectionModuleSymbol module, IkvmReflectionTypeSymbol? type, MethodInfo underlyingMethod)
Initializes a new instance.
IMethodSymbol GetGenericMethodDefinition()
Returns a object that represents a generic method definition from which the current method can be con...
IMethodSymbol MakeGenericMethod(params ITypeSymbol[] typeArguments)
Substitutes the elements of an array of types for the type parameters of the current generic method d...
IMethodSymbol GetBaseDefinition()
When overridden in a derived class, returns the object for the method on the direct or indirect base ...
Implementation of IModuleSymbol derived from System.Reflection.
Holds references to symbols derived from System.Reflection.
IkvmReflectionSymbolContext Context
Gets the associated IkvmReflectionSymbolContext.
int GenericParameterPosition
Gets the position of the type parameter in the type parameter list of the generic type or method that...
Provides custom attributes for reflection objects that support them.
Discovers the attributes of a method and provides access to method metadata.
Discovers the attributes of a parameter and provides access to parameter metadata.
Represents type declarations: class types, interface types, array types, value types,...