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