Skip to content

Commit fe3a8d3

Browse files
authored
Enable nullable on adapter utilities (#3688)
1 parent bd12521 commit fe3a8d3

File tree

12 files changed

+68
-131
lines changed

12 files changed

+68
-131
lines changed

src/Microsoft.TestPlatform.AdapterUtilities/Helpers/ReflectionHelpers.MethodBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System;
55
using System.Reflection;
66

7-
#nullable disable
8-
97
namespace Microsoft.TestPlatform.AdapterUtilities.Helpers;
108

119
internal static partial class ReflectionHelpers
@@ -33,7 +31,7 @@ internal static bool IsMethod(MethodBase method)
3331
#endif
3432
}
3533

36-
internal static Type GetReflectedType(MethodBase method)
34+
internal static Type? GetReflectedType(MethodBase method)
3735
{
3836
#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP
3937
return method.ReflectedType;

src/Microsoft.TestPlatform.AdapterUtilities/Helpers/ReflectionHelpers.Type.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System;
55
using System.Reflection;
66

7-
#nullable disable
8-
97
namespace Microsoft.TestPlatform.AdapterUtilities.Helpers;
108

119
internal static partial class ReflectionHelpers
@@ -19,7 +17,7 @@ internal static bool IsGenericType(Type type)
1917
#endif
2018
}
2119

22-
internal static MethodBase GetDeclaringMethod(Type type)
20+
internal static MethodBase? GetDeclaringMethod(Type type)
2321
{
2422
#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP
2523
return type.DeclaringMethod;

src/Microsoft.TestPlatform.AdapterUtilities/Helpers/ReflectionHelpers.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.AdapterUtilities.Helpers;
1311

1412
internal static partial class ReflectionHelpers

src/Microsoft.TestPlatform.AdapterUtilities/HierarchyConstants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#nullable disable
5-
64
namespace Microsoft.TestPlatform.AdapterUtilities;
75

86
/// <summary>

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameConstants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#nullable disable
5-
64
namespace Microsoft.TestPlatform.AdapterUtilities;
75

86
/// <summary>

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/InvalidManagedNameException.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using System.Runtime.Serialization;
88
#endif
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1311

1412
#if !NETSTANDARD1_0 && !WINDOWS_UWP

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/ManagedNameHelper.Reflection.cs

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
using System.Reflection;
1212
using System.Text;
1313

14-
#nullable disable
15-
1614
namespace Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1715

1816
public static partial class ManagedNameHelper
@@ -86,10 +84,14 @@ public static void GetManagedName(MethodBase method!!, out string managedTypeNam
8684
{
8785
if (!ReflectionHelpers.IsMethod(method))
8886
{
87+
// TODO: @Haplois, exception expects a message and not a param name.
8988
throw new NotSupportedException(nameof(method));
9089
}
9190

92-
var semanticType = ReflectionHelpers.GetReflectedType(method);
91+
var semanticType = ReflectionHelpers.GetReflectedType(method)
92+
// TODO: @Haplois, exception expects a message and not a param name.
93+
?? throw new NotSupportedException(nameof(method));
94+
9395
if (ReflectionHelpers.IsGenericType(semanticType))
9496
{
9597
// The type might have some of its generic parameters specified, so make
@@ -101,7 +103,7 @@ public static void GetManagedName(MethodBase method!!, out string managedTypeNam
101103
// a new method reference using the open form of the reflected type. The intent is
102104
// to strip all generic type parameters.
103105
var methodHandle = ReflectionHelpers.GetMethodHandle(method);
104-
method = MethodBase.GetMethodFromHandle(methodHandle, semanticType.TypeHandle);
106+
method = MethodBase.GetMethodFromHandle(methodHandle, semanticType.TypeHandle)!;
105107
}
106108

107109
if (method.IsGenericMethod)
@@ -116,6 +118,11 @@ public static void GetManagedName(MethodBase method!!, out string managedTypeNam
116118

117119
// Namespace and Type Name (with arity designation)
118120
var hierarchyPos = AppendTypeString(typeBuilder, semanticType, closedType: false);
121+
if (hierarchyPos is null || hierarchyPos.Length != HierarchyConstants.Levels.TotalLevelCount)
122+
{
123+
// TODO: @Haplois, exception expects a message and not a param name.
124+
throw new NotSupportedException(nameof(method));
125+
}
119126

120127
// Method Name with method arity
121128
var arity = method.GetGenericArguments().Length;
@@ -142,10 +149,11 @@ public static void GetManagedName(MethodBase method!!, out string managedTypeNam
142149

143150
managedTypeName = typeBuilder.ToString();
144151
managedMethodName = methodBuilder.ToString();
145-
hierarchyValues = new[] {
146-
managedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]),
147-
managedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1),
148-
};
152+
hierarchyValues = new[]
153+
{
154+
managedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]),
155+
managedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1),
156+
};
149157
}
150158

151159
/// <summary>
@@ -176,7 +184,7 @@ public static void GetManagedName(MethodBase method!!, out string managedTypeNam
176184
/// </remarks>
177185
public static MethodBase GetMethod(Assembly assembly, string managedTypeName, string managedMethodName)
178186
{
179-
Type type;
187+
Type? type;
180188

181189
var parsedManagedTypeName = ReflectionHelpers.ParseEscapedString(managedTypeName);
182190

@@ -199,14 +207,10 @@ public static MethodBase GetMethod(Assembly assembly, string managedTypeName, st
199207
throw new InvalidManagedNameException(message);
200208
}
201209

202-
MethodInfo method = null;
210+
MethodInfo? method = null;
203211
ManagedNameParser.ParseManagedMethodName(managedMethodName, out var methodName, out var methodArity, out var parameterTypes);
204212

205-
#if NET20 || NET35
206-
if (!IsNullOrWhiteSpace(methodName))
207-
#else
208213
if (!string.IsNullOrWhiteSpace(methodName))
209-
#endif
210214
{
211215
method = FindMethod(type, methodName, methodArity, parameterTypes);
212216
}
@@ -220,12 +224,11 @@ public static MethodBase GetMethod(Assembly assembly, string managedTypeName, st
220224
return method;
221225
}
222226

223-
private static MethodInfo FindMethod(Type type, string methodName, int methodArity, string[] parameterTypes)
227+
private static MethodInfo? FindMethod(Type type, string methodName, int methodArity, string[]? parameterTypes)
224228
{
225-
bool Filter(MemberInfo mbr, object param)
229+
bool Filter(MemberInfo mbr, object? param)
226230
{
227-
var method = mbr as MethodInfo;
228-
if (method.Name != methodName || method.GetGenericArguments().Length != methodArity)
231+
if (mbr is not MethodInfo method || method.Name != methodName || method.GetGenericArguments().Length != methodArity)
229232
{
230233
return false;
231234
}
@@ -260,17 +263,17 @@ bool Filter(MemberInfo mbr, object param)
260263
methods = type.GetRuntimeMethods().Where(m => Filter(m, null)).ToArray();
261264
#endif
262265

263-
#if NET20
264-
return (MethodInfo)SingleOrDefault(methods);
265-
#else
266-
return (MethodInfo)methods.SingleOrDefault();
267-
#endif
266+
return (MethodInfo?)methods.SingleOrDefault();
268267
}
269268

270-
private static int[] AppendTypeString(StringBuilder b, Type type, bool closedType)
269+
private static int[]? AppendTypeString(StringBuilder b, Type? type, bool closedType)
271270
{
272-
int[] hierarchies = null;
271+
if (type is null)
272+
{
273+
return null;
274+
}
273275

276+
int[]? hierarchies = null;
274277
if (type.IsArray)
275278
{
276279
hierarchies = AppendTypeString(b, type.GetElementType(), closedType);
@@ -311,8 +314,13 @@ private static int[] AppendTypeString(StringBuilder b, Type type, bool closedTyp
311314
return hierarchies;
312315
}
313316

314-
private static void AppendNamespace(StringBuilder b, string namespaceString)
317+
private static void AppendNamespace(StringBuilder b, string? namespaceString)
315318
{
319+
if (namespaceString is null)
320+
{
321+
return;
322+
}
323+
316324
int start = 0;
317325
bool shouldEscape = false;
318326

@@ -402,8 +410,13 @@ private static void NormalizeAndAppendString(StringBuilder b, string name)
402410
b.Append('\'');
403411
}
404412

405-
private static int AppendNestedTypeName(StringBuilder b, Type type)
413+
private static int AppendNestedTypeName(StringBuilder b, Type? type)
406414
{
415+
if (type is null)
416+
{
417+
return 0;
418+
}
419+
407420
var outerArity = 0;
408421
if (type.IsNested)
409422
{
@@ -502,62 +515,4 @@ private static string GetTypeString(Type type, bool closedType)
502515
AppendTypeString(builder, type, closedType);
503516
return builder.ToString();
504517
}
505-
506-
#if NET20
507-
// the method is mostly copied from
508-
// https://github.com/dotnet/runtime/blob/c0840723b382bcfa67b35839af8572fcd38f1d13/src/libraries/System.Linq/src/System/Linq/Single.cs#L86
509-
public static TSource SingleOrDefault<TSource>(System.Collections.Generic.IEnumerable<TSource> source)
510-
{
511-
if (source == null)
512-
{
513-
throw new ArgumentNullException(nameof(source));
514-
}
515-
516-
if (source is System.Collections.Generic.IList<TSource> list)
517-
{
518-
switch (list.Count)
519-
{
520-
case 0:
521-
return default;
522-
case 1:
523-
return list[0];
524-
}
525-
}
526-
else
527-
{
528-
using (System.Collections.Generic.IEnumerator<TSource> e = source.GetEnumerator())
529-
{
530-
if (!e.MoveNext())
531-
{
532-
return default;
533-
}
534-
535-
TSource result = e.Current;
536-
if (!e.MoveNext())
537-
{
538-
return result;
539-
}
540-
}
541-
}
542-
543-
throw new InvalidOperationException("MoreThanOneElement");
544-
}
545-
#endif
546-
547-
#if NET20 || NET35
548-
public static bool IsNullOrWhiteSpace(string value)
549-
{
550-
if (value is null) return true;
551-
552-
for (int i = 0; i < value.Length; i++)
553-
{
554-
if (!char.IsWhiteSpace(value[i]))
555-
{
556-
return false;
557-
}
558-
}
559-
560-
return true;
561-
}
562-
#endif
563518
}

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/ManagedNameParser.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
using Microsoft.TestPlatform.AdapterUtilities.Helpers;
99

10-
#nullable disable
11-
1210
namespace Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1311

1412
public class ManagedNameParser
@@ -66,7 +64,7 @@ public static void ParseManagedTypeName(string managedTypeName, out string names
6664
/// <exception cref="InvalidManagedNameException">
6765
/// Thrown if <paramref name="managedMethodName"/> contains spaces, incomplete, or the arity isn't numeric.
6866
/// </exception>
69-
public static void ParseManagedMethodName(string managedMethodName, out string methodName, out int arity, out string[] parameterTypes)
67+
public static void ParseManagedMethodName(string managedMethodName, out string methodName, out int arity, out string[]? parameterTypes)
7068
{
7169
int pos = ParseMethodName(managedMethodName, 0, out var escapedMethodName, out arity);
7270
methodName = ReflectionHelpers.ParseEscapedString(escapedMethodName);
@@ -127,13 +125,12 @@ private static int ParseArity(string managedMethodName, int start, out int arity
127125
}
128126
if (!int.TryParse(Capture(managedMethodName, start + 1, i), out arity))
129127
{
130-
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorMethodArityMustBeNumeric);
131-
throw new InvalidManagedNameException(message);
128+
throw new InvalidManagedNameException(Resources.Resources.ErrorMethodArityMustBeNumeric);
132129
}
133130
return i;
134131
}
135132

136-
private static int ParseParameterTypeList(string managedMethodName, int start, out string[] parameterTypes)
133+
private static int ParseParameterTypeList(string managedMethodName, int start, out string[]? parameterTypes)
137134
{
138135
parameterTypes = null;
139136
if (start == managedMethodName.Length)
@@ -166,8 +163,7 @@ private static int ParseParameterTypeList(string managedMethodName, int start, o
166163
}
167164
}
168165

169-
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorIncompleteManagedName);
170-
throw new InvalidManagedNameException(message);
166+
throw new InvalidManagedNameException(Resources.Resources.ErrorIncompleteManagedName);
171167
}
172168

173169
private static int ParseParameterType(string managedMethodName, int start, out string parameterType)
@@ -229,8 +225,7 @@ private static int ParseArrayBrackets(string managedMethodName, int start)
229225
}
230226
}
231227

232-
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorIncompleteManagedName);
233-
throw new InvalidManagedNameException(message);
228+
throw new InvalidManagedNameException(Resources.Resources.ErrorIncompleteManagedName);
234229
}
235230

236231
private static int ParseGenericBrackets(string managedMethodName, int start)
@@ -260,7 +255,6 @@ private static int ParseGenericBrackets(string managedMethodName, int start)
260255
}
261256
}
262257

263-
string message = string.Format(CultureInfo.CurrentCulture, Resources.Resources.ErrorIncompleteManagedName);
264-
throw new InvalidManagedNameException(message);
258+
throw new InvalidManagedNameException(Resources.Resources.ErrorIncompleteManagedName);
265259
}
266260
}
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.HierarchyLabel = "Hierarchy" -> string
2-
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.HierarchyPropertyId = "TestCase.Hierarchy" -> string
1+
#nullable enable
2+
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.HierarchyLabel = "Hierarchy" -> string!
3+
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.HierarchyPropertyId = "TestCase.Hierarchy" -> string!
34
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.Levels.ClassIndex = 1 -> int
45
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.Levels.NamespaceIndex = 0 -> int
56
const Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.Levels.TotalLevelCount = 2 -> int
6-
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedMethodLabel = "ManagedMethod" -> string
7-
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedMethodPropertyId = "TestCase.ManagedMethod" -> string
8-
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedTypeLabel = "ManagedType" -> string
9-
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedTypePropertyId = "TestCase.ManagedType" -> string
7+
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedMethodLabel = "ManagedMethod" -> string!
8+
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedMethodPropertyId = "TestCase.ManagedMethod" -> string!
9+
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedTypeLabel = "ManagedType" -> string!
10+
const Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants.ManagedTypePropertyId = "TestCase.ManagedType" -> string!
1011
Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants
1112
Microsoft.TestPlatform.AdapterUtilities.HierarchyConstants.Levels
1213
Microsoft.TestPlatform.AdapterUtilities.ManagedNameConstants
1314
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.InvalidManagedNameException
14-
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.InvalidManagedNameException.InvalidManagedNameException(string message) -> void
15+
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.InvalidManagedNameException.InvalidManagedNameException(string! message) -> void
1516
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper
1617
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser
1718
Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser.ManagedNameParser() -> void
1819
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider
19-
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.AppendString(string str) -> void
20-
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.GetHash() -> byte[]
20+
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.AppendString(string! str) -> void
21+
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.GetHash() -> byte[]!
2122
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.GetId() -> System.Guid
2223
Microsoft.TestPlatform.AdapterUtilities.TestIdProvider.TestIdProvider() -> void
23-
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetManagedName(System.Reflection.MethodBase method, out string managedTypeName, out string managedMethodName) -> void
24-
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetManagedName(System.Reflection.MethodBase method, out string managedTypeName, out string managedMethodName, out string[] hierarchyValues) -> void
25-
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetMethod(System.Reflection.Assembly assembly, string managedTypeName, string managedMethodName) -> System.Reflection.MethodBase
26-
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser.ParseManagedMethodName(string managedMethodName, out string methodName, out int arity, out string[] parameterTypes) -> void
27-
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser.ParseManagedTypeName(string managedTypeName, out string namespaceName, out string typeName) -> void
24+
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetManagedName(System.Reflection.MethodBase! method, out string! managedTypeName, out string! managedMethodName) -> void
25+
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetManagedName(System.Reflection.MethodBase! method, out string! managedTypeName, out string! managedMethodName, out string![]! hierarchyValues) -> void
26+
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.GetMethod(System.Reflection.Assembly! assembly, string! managedTypeName, string! managedMethodName) -> System.Reflection.MethodBase!
27+
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser.ParseManagedMethodName(string! managedMethodName, out string! methodName, out int arity, out string![]? parameterTypes) -> void
28+
static Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameParser.ParseManagedTypeName(string! managedTypeName, out string! namespaceName, out string! typeName) -> void

0 commit comments

Comments
 (0)