-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathManagedNameHelper.Reflection.cs
More file actions
625 lines (553 loc) · 23.5 KB
/
ManagedNameHelper.Reflection.cs
File metadata and controls
625 lines (553 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.TestPlatform.AdapterUtilities.Helpers;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
public static partial class ManagedNameHelper
{
private readonly struct AssemblyNameCache
{
public AssemblyNameCache(Assembly? assembly, string simpleName)
{
Assembly = assembly;
SimpleName = simpleName;
}
public Assembly? Assembly { get; }
public string SimpleName { get; }
}
private static AssemblyNameCache? s_lastAssemblyNameCache;
/// <summary>
/// Gets fully qualified managed type and method name from given <see href="MethodBase" /> instance.
/// </summary>
/// <param name="method">
/// A <see cref="MethodBase" /> instance to get fully qualified managed type and method name.
/// </param>
/// <param name="managedTypeName">
/// When this method returns, contains the fully qualified managed type name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedtype-property">the RFC</see>.
/// </param>
/// <param name="managedMethodName">
/// When this method returns, contains the fully qualified managed method name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedmethod-property">the RFC</see>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="method"/> is null.
/// </exception>
/// <exception cref="NotSupportedException">
/// <paramref name="method"/> must describe a method.
/// </exception>
/// <exception cref="NotImplementedException">
/// Required functionality on <paramref name="method"/> is missing on the current platform.
/// </exception>
/// <remarks>
/// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in
/// <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
/// </remarks>
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName)
=> GetManagedNameAndHierarchy(method, false, out managedTypeName, out managedMethodName, out _);
/// <summary>
/// Gets fully qualified managed type and method name from given <see href="MethodBase" /> instance.
/// </summary>
/// <param name="method">
/// A <see cref="MethodBase" /> instance to get fully qualified managed type and method name.
/// </param>
/// <param name="managedTypeName">
/// When this method returns, contains the fully qualified managed type name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedtype-property">the RFC</see>.
/// </param>
/// <param name="managedMethodName">
/// When this method returns, contains the fully qualified managed method name of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedmethod-property">the RFC</see>.
/// </param>
/// <param name="hierarchyValues">
/// When this method returns, contains the default test hierarchy values of the <paramref name="method"/>.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="method"/> is null.
/// </exception>
/// <exception cref="NotSupportedException">
/// <paramref name="method"/> must describe a method.
/// </exception>
/// <exception cref="NotImplementedException">
/// Required functionality on <paramref name="method"/> is missing on the current platform.
/// </exception>
/// <remarks>
/// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in
/// <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
/// </remarks>
public static void GetManagedName(MethodBase method, out string managedTypeName, out string managedMethodName, out string?[] hierarchyValues)
{
if (!method.IsGenericMethod && ReflectionHelpers.GetReflectedType(method) is { } semanticType && !ReflectionHelpers.IsGenericType(semanticType))
{
// We are dealing with non-generic method in non-generic type.
// So, it doesn't matter what we pass as "useClosedTypes".
// Instead of calling GetManagedNameAndHierarchy that does repeated work, we call it only once.
GetManagedNameAndHierarchy(method, false, out managedTypeName, out managedMethodName, out hierarchyValues);
}
else
{
GetManagedNameAndHierarchy(method, false, out managedTypeName, out managedMethodName, out _);
GetManagedNameAndHierarchy(method, true, out _, out _, out hierarchyValues);
}
}
/// <summary>
/// Gets default hierarchy values for a given <paramref name="method"/>.
/// </summary>
/// <param name="method">
/// A <see cref="MethodBase" /> instance to get default hierarchy values.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="method"/> is null.
/// </exception>
/// <exception cref="NotSupportedException">
/// <paramref name="method"/> must describe a method.
/// </exception>
/// <exception cref="NotImplementedException">
/// Required functionality on <paramref name="method"/> is missing on the current platform.
/// </exception>
/// <returns>
/// The hierarchy values.
/// </returns>
public static string?[] GetManagedHierarchy(MethodBase method)
{
GetManagedNameAndHierarchy(method, true, out _, out _, out var hierarchyValues);
return hierarchyValues;
}
private static void GetManagedNameAndHierarchy(MethodBase method, bool useClosedTypes, out string managedTypeName, out string managedMethodName, out string?[] hierarchyValues)
{
_ = method ?? throw new ArgumentNullException(nameof(method));
if (!ReflectionHelpers.IsMethod(method))
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorMethodExpectedAsAnArgument, nameof(method)));
}
var semanticType = ReflectionHelpers.GetReflectedType(method)
// TODO: @Haplois, exception expects a message and not a param name.
?? throw new NotSupportedException(nameof(method));
if (ReflectionHelpers.IsGenericType(semanticType) && !useClosedTypes)
{
// The type might have some of its generic parameters specified, so make
// sure we are working with the open form of the generic type.
semanticType = semanticType.GetGenericTypeDefinition();
// The method might have some of its parameters specified by the original closed type
// declaration. Here we use the method handle (basically metadata token) to create
// a new method reference using the open form of the reflected type. The intent is
// to strip all generic type parameters.
var methodHandle = ReflectionHelpers.GetMethodHandle(method);
method = MethodBase.GetMethodFromHandle(methodHandle, semanticType.TypeHandle)!;
}
managedTypeName = semanticType.FullName!;
if (method.IsGenericMethod && !useClosedTypes)
{
// If this method is generic, then convert to the generic method definition
// so that we get the open generic type definitions for parameters.
method = ((MethodInfo)method).GetGenericMethodDefinition();
}
var typeBuilder = new StringBuilder();
var methodBuilder = new StringBuilder();
// Namespace and Type Name (with arity designation)
// hierarchyPos contains [startIndexOfNamespace, endIndexOfNameSpace, endIndexOfTypeName]
var hierarchyPos = AppendTypeString(typeBuilder, semanticType, closedType: useClosedTypes);
if (hierarchyPos is null || hierarchyPos.Length != 3)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorMethodExpectedAsAnArgument, nameof(method)));
}
// Method Name with method arity
var arity = method.GetGenericArguments().Length;
AppendMethodString(methodBuilder, method.Name, arity);
if (arity > 0)
{
if (useClosedTypes)
{
AppendGenericMethodParameters(methodBuilder, method);
}
else
{
methodBuilder.Append('`');
methodBuilder.Append(arity);
}
}
// Type Parameters
var paramList = method.GetParameters();
if (paramList.Length != 0)
{
methodBuilder.Append('(');
foreach (var p in paramList)
{
// closedType is always true here by RFC
AppendTypeString(methodBuilder, p.ParameterType, closedType: true);
methodBuilder.Append(',');
}
// Replace the last ',' with ')'
methodBuilder[methodBuilder.Length - 1] = ')';
}
var methodNameEndIndex = methodBuilder.Length;
var escapedManagedTypeName = typeBuilder.ToString();
managedMethodName = methodBuilder.ToString();
hierarchyValues = new string[HierarchyConstants.Levels.TotalLevelCount];
hierarchyValues[HierarchyConstants.Levels.TestGroupIndex] = managedMethodName.Substring(0, methodNameEndIndex);
if (hierarchyPos[1] == hierarchyPos[0]) // No namespace
{
hierarchyValues[HierarchyConstants.Levels.ClassIndex] = escapedManagedTypeName.Substring(0, hierarchyPos[2]);
hierarchyValues[HierarchyConstants.Levels.NamespaceIndex] = null;
}
else
{
hierarchyValues[HierarchyConstants.Levels.ClassIndex] = escapedManagedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1);
hierarchyValues[HierarchyConstants.Levels.NamespaceIndex] = escapedManagedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]);
}
var assembly = method.DeclaringType?.Assembly;
if (s_lastAssemblyNameCache is { } cache &&
cache.Assembly == assembly)
{
hierarchyValues[HierarchyConstants.Levels.ContainerIndex] = cache.SimpleName;
}
else
{
var assemblyName = assembly?.GetName()?.Name ?? string.Empty;
hierarchyValues[HierarchyConstants.Levels.ContainerIndex] = assemblyName;
s_lastAssemblyNameCache = new AssemblyNameCache(assembly, assemblyName);
}
}
/// <summary>
/// Gets the <see cref="MethodBase"/> object with the specified <paramref name="managedTypeName"/>
/// and <paramref name="managedMethodName"/> in the <paramref name="assembly"/> instance.
/// </summary>
/// <param name="assembly">
/// An <see cref="Assembly" /> instance to search in.
/// </param>
/// <param name="managedTypeName">
/// The fully qualified managed name of the type.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedtype-property">the RFC</see>.
/// </param>
/// <param name="managedMethodName">
/// The fully qualified managed name of the method.
/// The format is defined in <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md#managedmethod-property">the RFC</see>.
/// </param>
/// <returns>
/// A <see cref="MethodBase" /> object that represents specified parameters, throws if null.
/// </returns>
/// <exception cref="InvalidManagedNameException">
/// Values specified with <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/>
/// does not correspond to a method in the <paramref name="assembly"/> instance, or malformed.
/// </exception>
/// <remarks>
/// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in
/// <see href="https://github.com/microsoft/vstest/blob/main/docs/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.
/// </remarks>
public static MethodBase GetMethod(Assembly assembly, string managedTypeName, string managedMethodName)
{
var type = assembly.GetType(managedTypeName, throwOnError: false, ignoreCase: false);
if (type == null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorTypeNotFound, managedTypeName);
throw new InvalidManagedNameException(message);
}
MethodInfo? method = null;
ManagedNameParser.ParseManagedMethodName(managedMethodName, out var methodName, out var methodArity, out var parameterTypes);
if (!string.IsNullOrWhiteSpace(methodName))
{
method = FindMethod(type, methodName, methodArity, parameterTypes);
}
if (method == null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorMethodNotFound, methodName, managedTypeName);
throw new InvalidManagedNameException(message);
}
return method;
}
private static MethodInfo? FindMethod(Type type, string methodName, int methodArity, string[]? parameterTypes)
{
bool Filter(MemberInfo mbr, object? param)
{
if (mbr is not MethodInfo method || method.Name != methodName || method.GetGenericArguments().Length != methodArity)
{
return false;
}
var paramList = method.GetParameters();
if (paramList.Length == 0 && parameterTypes == null)
{
return true;
}
else if (parameterTypes == null || paramList.Length != parameterTypes.Length)
{
return false;
}
for (int i = 0; i < paramList.Length; i++)
{
var parameterType = GetTypeString(paramList[i].ParameterType, closedType: true);
if (parameterType != parameterTypes[i])
{
return false;
}
}
return true;
}
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
var methods = type.FindMembers(MemberTypes.Method, bindingFlags, Filter, null);
return (MethodInfo?)(methods.Length switch
{
1 => methods[0],
> 1 => methods.SingleOrDefault(i => i.DeclaringType == type),
_ => null
});
}
private static int[]? AppendTypeString(StringBuilder b, Type? type, bool closedType)
{
if (type is null)
{
return null;
}
int[]? hierarchies = null;
if (type.IsArray)
{
hierarchies = AppendTypeString(b, type.GetElementType(), closedType);
b.Append('[');
for (int i = 0; i < type.GetArrayRank() - 1; i++)
{
b.Append(',');
}
b.Append(']');
}
else if (type.IsGenericParameter)
{
if (ReflectionHelpers.GetDeclaringMethod(type) != null)
{
b.Append('!');
}
b.Append('!');
b.Append(type.GenericParameterPosition);
}
else
{
hierarchies = new int[3];
hierarchies[0] = b.Length;
if (type.Namespace != null)
{
AppendNamespace(b, type.Namespace);
hierarchies[1] = b.Length;
b.Append('.');
}
else
{
hierarchies[1] = hierarchies[0];
}
AppendNestedTypeName(b, type);
if (closedType)
{
AppendGenericTypeParameters(b, type);
}
hierarchies[2] = b.Length;
}
return hierarchies;
}
private static void AppendNamespace(StringBuilder b, string? namespaceString)
{
if (namespaceString is null)
{
return;
}
int start = 0;
bool shouldEscape = false;
for (int i = 0; i <= namespaceString.Length; i++)
{
if (i == namespaceString.Length || namespaceString[i] == '.')
{
if (start != 0)
{
b.Append('.');
}
var part = namespaceString.Substring(start, i - start);
if (shouldEscape)
{
NormalizeAndAppendString(b, part);
shouldEscape = false;
}
else
{
b.Append(part);
}
start = i + 1;
continue;
}
shouldEscape = shouldEscape || NeedsEscaping(namespaceString[i], i - start);
}
}
private static void AppendMethodString(StringBuilder methodBuilder, string name, int methodArity)
{
var arityStart = name.LastIndexOf('`');
var arity = 0;
if (arityStart > 0)
{
arityStart++;
var arityString = name.Substring(arityStart, name.Length - arityStart);
if (int.TryParse(arityString, out arity))
{
if (arity == methodArity)
{
name = name.Substring(0, arityStart - 1);
}
}
}
if (IsNormalized(name))
{
methodBuilder.Append(name);
}
else
{
NormalizeAndAppendString(methodBuilder, name);
}
if (arity > 0 && methodArity == arity)
{
methodBuilder.Append(
#if NET6_0_OR_GREATER
System.Globalization.CultureInfo.InvariantCulture,
#endif
$"`{arity}");
}
}
private static void NormalizeAndAppendString(StringBuilder b, string name)
{
b.Append('\'');
for (int i = 0; i < name.Length; i++)
{
char c = name[i];
if (NeedsEscaping(c, i))
{
if (c is '\\' or '\'')
{
// var encoded = Convert.ToString(((uint)c), 16);
// b.Append("\\u");
// b.Append('0', 4 - encoded.Length);
// b.Append(encoded);
b.Append('\\');
b.Append(c);
continue;
}
}
b.Append(c);
}
b.Append('\'');
}
private static int AppendNestedTypeName(StringBuilder b, Type? type)
{
if (type is null)
{
return 0;
}
var outerArity = 0;
if (type.IsNested)
{
outerArity = AppendNestedTypeName(b, type.DeclaringType);
b.Append('+');
}
var typeName = type.Name;
var stars = 0;
if (type.IsPointer)
{
for (int i = typeName.Length - 1; i > 0; i--)
{
if (typeName[i] != '*')
{
stars = typeName.Length - i - 1;
typeName = typeName.Substring(0, i + 1);
break;
}
}
}
var info = type.GetTypeInfo();
var arity = !info.IsGenericType
? 0
: info.GenericTypeParameters.Length > 0
? info.GenericTypeParameters.Length
: info.GenericTypeArguments.Length;
AppendMethodString(b, typeName, arity - outerArity);
b.Append('*', stars);
return arity;
}
private static void AppendGenericMethodParameters(StringBuilder methodBuilder, MethodBase method)
{
Type[] genericArguments = method.GetGenericArguments();
AppendGenericArguments(methodBuilder, genericArguments);
}
private static void AppendGenericTypeParameters(StringBuilder b, Type type)
{
Type[] genericArguments = type.GetGenericArguments();
AppendGenericArguments(b, genericArguments);
}
private static void AppendGenericArguments(StringBuilder b, Type[] genericArguments)
{
if (genericArguments.Length != 0)
{
b.Append('<');
foreach (var argType in genericArguments)
{
AppendTypeString(b, argType, closedType: true);
b.Append(',');
}
// Replace the last ',' with '>'
b[b.Length - 1] = '>';
}
}
private static bool IsNormalized(string s)
{
var brackets = 0;
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
if (NeedsEscaping(c, i) && c != '.')
{
if (i != 0)
{
if (c == '<')
{
brackets++;
continue;
}
if (c == '>' && s[i - 1] != '<' && brackets > 0)
{
brackets--;
continue;
}
}
return false;
}
}
return brackets == 0;
}
private static bool NeedsEscaping(char c, int pos)
{
if (pos == 0 && char.IsDigit(c))
{
return true;
}
if (c == '_'
// 'Digit' does not include letter numbers, which are valid identifiers as per docs https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/identifier-names'.
|| char.IsLetterOrDigit(c) // Lu, Ll, Lt, Lm, Lo, or Nd
)
{
return false;
}
var category = CharUnicodeInfo.GetUnicodeCategory(c);
return category
is not UnicodeCategory.LetterNumber // Nl
and not UnicodeCategory.NonSpacingMark // Mn
and not UnicodeCategory.SpacingCombiningMark // Mc
and not UnicodeCategory.ConnectorPunctuation // Pc
and not UnicodeCategory.Format; // Cf
}
private static string GetTypeString(Type type, bool closedType)
{
var builder = new StringBuilder();
AppendTypeString(builder, type, closedType);
return builder.ToString();
}
}