IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MethodInfo.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2012 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.Text;
27
28namespace IKVM.Reflection
29{
30
31 internal abstract class MethodInfo : MethodBase, IGenericContext, IGenericBinder
32 {
33 // prevent external subclasses
34 internal MethodInfo()
35 {
36 }
37
38 public sealed override MemberTypes MemberType
39 {
40 get { return MemberTypes.Method; }
41 }
42
43 public abstract Type ReturnType { get; }
44 public abstract ParameterInfo ReturnParameter { get; }
45
46 public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments)
47 {
48 throw new NotSupportedException(this.GetType().FullName);
49 }
50
51 public virtual MethodInfo GetGenericMethodDefinition()
52 {
53 throw new NotSupportedException(this.GetType().FullName);
54 }
55
56 public override string ToString()
57 {
58 var sb = new StringBuilder();
59 sb.Append(ReturnType.Name).Append(' ').Append(this.Name);
60
61 string sep;
62 if (IsGenericMethod)
63 {
64 sb.Append('[');
65 sep = "";
66
67 foreach (var arg in GetGenericArguments())
68 {
69 sb.Append(sep).Append(arg);
70 sep = ", ";
71 }
72
73 sb.Append(']');
74 }
75
76 sb.Append('(');
77 sep = "";
78
79 foreach (var arg in GetParameters())
80 {
81 sb.Append(sep).Append(arg.ParameterType);
82 sep = ", ";
83 }
84
85 sb.Append(')');
86 return sb.ToString();
87 }
88
89 internal bool IsNewSlot
90 {
91 get { return (Attributes & MethodAttributes.NewSlot) != 0; }
92 }
93
94 public MethodInfo GetBaseDefinition()
95 {
96 var match = this;
97 if (match.IsVirtual)
98 {
99 for (var type = DeclaringType.BaseType; type != null && !match.IsNewSlot; type = type.BaseType)
100 {
101 var method = type.FindMethod(Name, MethodSignature) as MethodInfo;
102 if (method != null && method.IsVirtual)
103 match = method;
104 }
105 }
106
107 return match;
108 }
109
110 public virtual MethodInfo[] __GetMethodImpls()
111 {
112 throw new NotSupportedException();
113 }
114
115 public bool __TryGetImplMap(out ImplMapFlags mappingFlags, out string importName, out string importScope)
116 {
117 return Module.__TryGetImplMap(GetCurrentToken(), out mappingFlags, out importName, out importScope);
118 }
119
120 public ConstructorInfo __AsConstructorInfo()
121 {
122 return new ConstructorInfoImpl(this);
123 }
124
125 Type IGenericContext.GetGenericTypeArgument(int index)
126 {
127 return this.DeclaringType.GetGenericTypeArgument(index);
128 }
129
130 Type IGenericContext.GetGenericMethodArgument(int index)
131 {
132 return GetGenericMethodArgument(index);
133 }
134
135 internal virtual Type GetGenericMethodArgument(int index)
136 {
137 throw new InvalidOperationException();
138 }
139
140 internal virtual int GetGenericMethodArgumentCount()
141 {
142 throw new InvalidOperationException();
143 }
144
145 internal override MethodInfo GetMethodOnTypeDefinition()
146 {
147 return this;
148 }
149
150 Type IGenericBinder.BindTypeParameter(Type type)
151 {
152 return this.DeclaringType.GetGenericTypeArgument(type.GenericParameterPosition);
153 }
154
155 Type IGenericBinder.BindMethodParameter(Type type)
156 {
157 return GetGenericMethodArgument(type.GenericParameterPosition);
158 }
159
160 internal override MethodBase BindTypeParameters(Type type)
161 {
162 return new GenericMethodInstance(this.DeclaringType.BindTypeParameters(type), this, null);
163 }
164
165 // This method is used by ILGenerator and exists to allow ArrayMethod to override it,
166 // because ArrayMethod doesn't have a working MethodAttributes property, so it needs
167 // to base the result of this on the CallingConvention.
168 internal virtual bool HasThis
169 {
170 get { return !IsStatic; }
171 }
172
173 internal sealed override MemberInfo SetReflectedType(Type type)
174 {
175 return new MethodInfoWithReflectedType(type, this);
176 }
177
178 internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
179 {
180 var module = Module;
181 var list = new List<CustomAttributeData>();
182
183 if ((Attributes & MethodAttributes.PinvokeImpl) != 0 && (attributeType == null || attributeType.IsAssignableFrom(module.Universe.System_Runtime_InteropServices_DllImportAttribute)))
184 if (__TryGetImplMap(out var flags, out var importName, out var importScope))
185 list.Add(CustomAttributeData.CreateDllImportPseudoCustomAttribute(module, flags, importName, importScope, GetMethodImplementationFlags()));
186
187 if ((GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0 && (attributeType == null || attributeType.IsAssignableFrom(module.Universe.System_Runtime_InteropServices_PreserveSigAttribute)))
188 list.Add(CustomAttributeData.CreatePreserveSigPseudoCustomAttribute(module));
189
190 return list;
191 }
192
193 }
194
195}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.MemberInfo MemberInfo
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Reflection.ParameterInfo ParameterInfo
IKVM.Reflection.MethodBase MethodBase
global::java.lang.invoke.LambdaForm.Name Name