IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
GenericTypeInstance.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2015 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.Text;
26
28
29namespace IKVM.Reflection
30{
31
32 sealed class GenericTypeInstance : TypeInfo
33 {
34
35 private readonly Type type;
36 private readonly Type[] args;
37 private readonly CustomModifiers[] mods;
38 private Type baseType;
39 private int token;
40
41 internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
42 {
43 bool identity = true;
44 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
45 {
46 // a TypeBuiler identity must be instantiated
47 identity = false;
48 }
49 else
50 {
51 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
52 for (int i = 0; i < typeArguments.Length; i++)
53 {
54 if (typeArguments[i] != type.GetGenericTypeArgument(i)
55 || !IsEmpty(mods, i))
56 {
57 identity = false;
58 break;
59 }
60 }
61 }
62 if (identity)
63 {
64 return type;
65 }
66 else
67 {
68 return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
69 }
70 }
71
72 private static bool IsEmpty(CustomModifiers[] mods, int i)
73 {
74 // we need to be extra careful, because mods doesn't not need to be in canonical format
75 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
76 return mods == null || mods[i].IsEmpty;
77 }
78
79 private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
80 {
81 this.type = type;
82 this.args = args;
83 this.mods = mods;
84 }
85
86 public override bool Equals(object o)
87 {
89 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
90 && Util.ArrayEquals(gt.mods, mods);
91 }
92
93 public override int GetHashCode()
94 {
95 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
96 }
97
98 public override string AssemblyQualifiedName
99 {
100 get
101 {
102 string fn = FullName;
103 return fn == null ? null : fn + ", " + type.Assembly.FullName;
104 }
105 }
106
107 public override Type BaseType
108 {
109 get
110 {
111 if (baseType == null)
112 {
113 Type rawBaseType = type.BaseType;
114 if (rawBaseType == null)
115 {
116 baseType = rawBaseType;
117 }
118 else
119 {
120 baseType = rawBaseType.BindTypeParameters(this);
121 }
122 }
123 return baseType;
124 }
125 }
126
127 protected override bool IsValueTypeImpl
128 {
129 get { return type.IsValueType; }
130 }
131
132 public override bool IsVisible
133 {
134 get
135 {
136 if (base.IsVisible)
137 {
138 foreach (Type arg in args)
139 {
140 if (!arg.IsVisible)
141 {
142 return false;
143 }
144 }
145 return true;
146 }
147 return false;
148 }
149 }
150
151 public override Type DeclaringType
152 {
153 get { return type.DeclaringType; }
154 }
155
156 public override TypeAttributes Attributes
157 {
158 get { return type.Attributes; }
159 }
160
161 internal override void CheckBaked()
162 {
163 type.CheckBaked();
164 }
165
166 public override FieldInfo[] __GetDeclaredFields()
167 {
168 FieldInfo[] fields = type.__GetDeclaredFields();
169 for (int i = 0; i < fields.Length; i++)
170 {
171 fields[i] = fields[i].BindTypeParameters(this);
172 }
173 return fields;
174 }
175
176 public override Type[] __GetDeclaredInterfaces()
177 {
178 Type[] interfaces = type.__GetDeclaredInterfaces();
179 for (int i = 0; i < interfaces.Length; i++)
180 {
181 interfaces[i] = interfaces[i].BindTypeParameters(this);
182 }
183 return interfaces;
184 }
185
186 public override MethodBase[] __GetDeclaredMethods()
187 {
188 MethodBase[] methods = type.__GetDeclaredMethods();
189 for (int i = 0; i < methods.Length; i++)
190 {
191 methods[i] = methods[i].BindTypeParameters(this);
192 }
193 return methods;
194 }
195
196 public override Type[] __GetDeclaredTypes()
197 {
198 return type.__GetDeclaredTypes();
199 }
200
201 public override EventInfo[] __GetDeclaredEvents()
202 {
203 EventInfo[] events = type.__GetDeclaredEvents();
204 for (int i = 0; i < events.Length; i++)
205 {
206 events[i] = events[i].BindTypeParameters(this);
207 }
208 return events;
209 }
210
211 public override PropertyInfo[] __GetDeclaredProperties()
212 {
213 PropertyInfo[] properties = type.__GetDeclaredProperties();
214 for (int i = 0; i < properties.Length; i++)
215 {
216 properties[i] = properties[i].BindTypeParameters(this);
217 }
218 return properties;
219 }
220
221 public override __MethodImplMap __GetMethodImplMap()
222 {
223 __MethodImplMap map = type.__GetMethodImplMap();
224 map.TargetType = this;
225 for (int i = 0; i < map.MethodBodies.Length; i++)
226 {
227 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
228 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
229 {
230 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
231 if (interfaceType.IsGenericType)
232 {
233 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
234 }
235 }
236 }
237 return map;
238 }
239
240 public override string Namespace
241 {
242 get { return type.Namespace; }
243 }
244
245 public override string Name
246 {
247 get { return type.Name; }
248 }
249
250 public override string FullName
251 {
252 get
253 {
254 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
255 {
256 return null;
257 }
258 StringBuilder sb = new StringBuilder(this.type.FullName);
259 sb.Append('[');
260 string sep = "";
261 foreach (Type type in args)
262 {
263 sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
264 sep = ",";
265 }
266 sb.Append(']');
267 return sb.ToString();
268 }
269 }
270
271 public override string ToString()
272 {
273 StringBuilder sb = new StringBuilder(type.FullName);
274 sb.Append('[');
275 string sep = "";
276 foreach (Type arg in args)
277 {
278 sb.Append(sep);
279 sb.Append(arg);
280 sep = ",";
281 }
282 sb.Append(']');
283 return sb.ToString();
284 }
285
286 public override Module Module
287 {
288 get { return type.Module; }
289 }
290
291 public override bool IsGenericType
292 {
293 get { return true; }
294 }
295
296 public override bool IsConstructedGenericType
297 {
298 get { return true; }
299 }
300
301 public override Type GetGenericTypeDefinition()
302 {
303 return type;
304 }
305
306 public override Type[] GetGenericArguments()
307 {
308 return Util.Copy(args);
309 }
310
311 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
312 {
313 return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
314 }
315
316 internal override Type GetGenericTypeArgument(int index)
317 {
318 return args[index];
319 }
320
321 public override bool ContainsGenericParameters
322 {
323 get
324 {
325 foreach (Type type in args)
326 {
327 if (type.ContainsGenericParameters)
328 {
329 return true;
330 }
331 }
332 return false;
333 }
334 }
335
336 protected override bool ContainsMissingTypeImpl
337 {
338 get { return type.__ContainsMissingType || ContainsMissingType(args); }
339 }
340
341 public override bool __GetLayout(out int packingSize, out int typeSize)
342 {
343 return type.__GetLayout(out packingSize, out typeSize);
344 }
345
346 internal override int GetModuleBuilderToken()
347 {
348 if (token == 0)
349 {
350 token = ((ModuleBuilder)type.Module).ImportType(this);
351 }
352 return token;
353 }
354
355 internal override Type BindTypeParameters(IGenericBinder binder)
356 {
357 for (int i = 0; i < args.Length; i++)
358 {
359 Type xarg = args[i].BindTypeParameters(binder);
360 if (!ReferenceEquals(xarg, args[i]))
361 {
362 Type[] xargs = new Type[args.Length];
363 Array.Copy(args, xargs, i);
364 xargs[i++] = xarg;
365 for (; i < args.Length; i++)
366 {
367 xargs[i] = args[i].BindTypeParameters(binder);
368 }
369 return Make(type, xargs, null);
370 }
371 }
372 return this;
373 }
374
375 internal override int GetCurrentToken()
376 {
377 return type.GetCurrentToken();
378 }
379
380 internal override bool IsBaked
381 {
382 get { return type.IsBaked; }
383 }
384
385 }
386
387}
IKVM.Reflection.Type Type
IKVM.Reflection.MethodInfo MethodInfo
global::java.lang.invoke.LambdaForm.Name Name
override PropertyInfo[] __GetDeclaredProperties()
override MethodBase[] __GetDeclaredMethods()
override __MethodImplMap __GetMethodImplMap()
override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
override bool __GetLayout(out int packingSize, out int typeSize)