Skip to content

Commit 7f1e48d

Browse files
committed
[dotnet] Add nullability annotations to devtools domains
1 parent 1dd967e commit 7f1e48d

13 files changed

+69
-42
lines changed

dotnet/src/webdriver/DevTools/DevToolsDomains.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using System.Reflection;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.DevTools
2527
{
2628
/// <summary>
@@ -30,7 +32,7 @@ public abstract class DevToolsDomains
3032
{
3133
// By default, we will look for a supported version within this
3234
// number of versions, as that will most likely still work.
33-
private static readonly int DefaultVersionRange = 5;
35+
private const int DefaultVersionRange = 5;
3436

3537
// This is the list of known supported DevTools version implementation.
3638
// When new versions are implemented for support, new types must be
@@ -93,23 +95,18 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
9395
throw new ArgumentException("Version range must be positive", nameof(versionRange));
9496
}
9597

96-
DevToolsDomains domains = null;
9798
Type domainType = MatchDomainsVersion(protocolVersion, versionRange);
98-
ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) });
99-
if (constructor != null)
100-
{
101-
domains = constructor.Invoke(new object[] { session }) as DevToolsDomains;
102-
}
10399

104-
return domains;
100+
ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) })!;
101+
return (DevToolsDomains)constructor.Invoke(new object[] { session });
105102
}
106103

107104
private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
108105
{
109106
// Return fast on an exact match
110-
if (SupportedDevToolsVersions.ContainsKey(desiredVersion))
107+
if (SupportedDevToolsVersions.TryGetValue(desiredVersion, out Type? exactMatch))
111108
{
112-
return SupportedDevToolsVersions[desiredVersion];
109+
return exactMatch;
113110
}
114111

115112
// Get the list of supported versions and sort descending

dotnet/src/webdriver/DevTools/DevToolsExtensionMethods.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.DevTools
2123
{
2224
/// <summary>

dotnet/src/webdriver/DevTools/DevToolsOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.DevTools
2123
{
2224
/// <summary>

dotnet/src/webdriver/DevTools/DevToolsSessionDomains.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.DevTools
2123
{
2224
/// <summary>
2325
/// Provides an abstract base class for version-specific domain implementations.
2426
/// </summary>
2527
public abstract class DevToolsSessionDomains
2628
{
27-
private CommandResponseTypeMap responseTypeMap = new CommandResponseTypeMap();
28-
2929
/// <summary>
3030
/// Initializes a new instance of the <see cref="DevToolsSessionDomains"/> class.
3131
/// </summary>
@@ -37,7 +37,7 @@ protected DevToolsSessionDomains()
3737
/// <summary>
3838
/// Gets the <see cref="CommandResponseTypeMap"/> containing information about the types returned by DevTools Protocol commands.,
3939
/// </summary>
40-
internal CommandResponseTypeMap ResponseTypeMap => this.responseTypeMap;
40+
internal CommandResponseTypeMap ResponseTypeMap { get; } = new CommandResponseTypeMap();
4141

4242
/// <summary>
4343
/// Populates the command response type map.

dotnet/src/webdriver/DevTools/DevToolsSessionEventReceivedEventArgs.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Text.Json;
2222
using System.Text.Json.Nodes;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.DevTools
2527
{
2628
/// <summary>
@@ -44,16 +46,16 @@ public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonE
4446
/// <summary>
4547
/// Gets the domain on which the event is to be raised.
4648
/// </summary>
47-
public string DomainName { get; private set; }
49+
public string DomainName { get; }
4850

4951
/// <summary>
5052
/// Gets the name of the event to be raised.
5153
/// </summary>
52-
public string EventName { get; private set; }
54+
public string EventName { get; }
5355

5456
/// <summary>
5557
/// Gets the data with which the event is to be raised.
5658
/// </summary>
57-
public JsonElement EventData { get; private set; }
59+
public JsonElement EventData { get; }
5860
}
5961
}

dotnet/src/webdriver/DevTools/DevToolsSessionLogMessageEventArgs.cs

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

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.DevTools
2325
{
2426
/// <summary>

dotnet/src/webdriver/DevTools/ExceptionThrownEventArgs.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,34 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.DevTools
2325
{
2426
/// <summary>
2527
/// Provides data for events relating to JavaScript exception handling.
2628
/// </summary>
2729
public class ExceptionThrownEventArgs : EventArgs
2830
{
31+
/// <summary>
32+
/// Initializes new instance of the <see cref="ExceptionThrownEventArgs"/> type.
33+
/// </summary>
34+
/// <param name="timestamp">The time stamp of the exception.</param>
35+
/// <param name="message">The text of the exception.</param>
36+
public ExceptionThrownEventArgs(DateTime timestamp, string message)
37+
{
38+
Timestamp = timestamp;
39+
Message = message;
40+
}
41+
2942
/// <summary>
3043
/// Gets the time stamp of the exception.
3144
/// </summary>
32-
public DateTime Timestamp { get; internal set; }
45+
public DateTime Timestamp { get; }
3346

3447
/// <summary>
3548
/// Gets the text of the exception.
3649
/// </summary>
37-
public string Message { get; internal set; }
50+
public string Message { get; }
3851
}
3952
}

dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,28 @@
2424
using System.Text.Json;
2525
using System.Text.Json.Serialization;
2626

27+
#nullable enable
28+
2729
namespace OpenQA.Selenium.DevTools.Json
2830
{
29-
internal class JsonEnumMemberConverter<TEnum> : JsonConverter<TEnum> where TEnum : Enum
31+
internal sealed class JsonEnumMemberConverter<TEnum>
32+
: JsonConverter<TEnum> where TEnum : struct, Enum
3033
{
3134
private readonly Dictionary<TEnum, string> _enumToString = new Dictionary<TEnum, string>();
3235
private readonly Dictionary<string, TEnum> _stringToEnum = new Dictionary<string, TEnum>();
3336

3437
public JsonEnumMemberConverter()
3538
{
3639
var type = typeof(TEnum);
37-
var values = Enum.GetValues(type);
40+
#if NET8_0_OR_GREATER
41+
TEnum[] values = Enum.GetValues<TEnum>();
42+
#else
43+
Array values = Enum.GetValues(type);
44+
#endif
3845

3946
foreach (var value in values)
4047
{
41-
var enumMember = type.GetMember(value.ToString())[0];
48+
var enumMember = type.GetField(value.ToString())!;
4249
var attr = enumMember.GetCustomAttributes(typeof(EnumMemberAttribute), false)
4350
.Cast<EnumMemberAttribute>()
4451
.FirstOrDefault();
@@ -59,7 +66,7 @@ public JsonEnumMemberConverter()
5966

6067
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
6168
{
62-
var stringValue = reader.GetString();
69+
var stringValue = reader.GetString() ?? throw new JsonException("Cannot read an enum string from \"null\"");
6370

6471
if (_stringToEnum.TryGetValue(stringValue, out var enumValue))
6572
{

dotnet/src/webdriver/DevTools/ResponsePausedEventArgs.cs

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

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.DevTools
2325
{
2426
/// <summary>

dotnet/src/webdriver/DevTools/v130/V130JavaScript.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArg
153153
private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
154154
{
155155
// TODO: Collect stack trace elements
156-
var wrapped = new ExceptionThrownEventArgs()
157-
{
158-
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
159-
Message = e.ExceptionDetails.Text
160-
};
156+
var wrapped = new ExceptionThrownEventArgs
157+
(
158+
timestamp: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
159+
message: e.ExceptionDetails.Text
160+
);
161161

162162
this.OnExceptionThrown(wrapped);
163163
}

0 commit comments

Comments
 (0)