Skip to content

Commit 6520010

Browse files
committed
Add nullable reference type attribute polyfill
1 parent 7222ba7 commit 6520010

File tree

5 files changed

+175
-18
lines changed

5 files changed

+175
-18
lines changed

dotnet/src/webdriver/DefaultFileDetector.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#nullable enable
2020

21+
using System.Diagnostics.CodeAnalysis;
22+
2123
namespace OpenQA.Selenium
2224
{
2325
/// <summary>
@@ -32,11 +34,7 @@ public class DefaultFileDetector : IFileDetector
3234
/// </summary>
3335
/// <param name="keySequence">The sequence to test for file existence.</param>
3436
/// <returns>This method always returns <see langword="false"/> in this implementation.</returns>
35-
public bool IsFile(
36-
#if NET8_0_OR_GREATER
37-
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
38-
#endif
39-
string? keySequence)
37+
public bool IsFile([NotNullWhen(true)] string? keySequence)
4038
{
4139
return false;
4240
}

dotnet/src/webdriver/Firefox/FirefoxDriver.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Collections.ObjectModel;
24+
using System.Diagnostics.CodeAnalysis;
2425
using System.Globalization;
2526
using System.IO;
2627
using System.IO.Compression;
@@ -248,9 +249,7 @@ public override IFileDetector FileDetector
248249
/// <summary>
249250
/// Gets a value indicating whether a DevTools session is active.
250251
/// </summary>
251-
#if NET8_0_OR_GREATER
252-
[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(devToolsSession))]
253-
#endif
252+
[MemberNotNullWhen(true, nameof(devToolsSession))]
254253
public bool HasActiveDevToolsSession
255254
{
256255
get { return this.devToolsSession != null; }

dotnet/src/webdriver/IFileDetector.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#nullable enable
2020

21+
using System.Diagnostics.CodeAnalysis;
22+
2123
namespace OpenQA.Selenium
2224
{
2325
/// <summary>
@@ -32,10 +34,6 @@ public interface IFileDetector
3234
/// </summary>
3335
/// <param name="keySequence">The sequence to test for file existence.</param>
3436
/// <returns><see langword="true"/> if the key sequence represents a file; otherwise, <see langword="false"/>.</returns>
35-
bool IsFile(
36-
#if NET8_0_OR_GREATER
37-
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
38-
#endif
39-
string? keySequence);
37+
bool IsFile([NotNullWhen(true)] string? keySequence);
4038
}
4139
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// <copyright file="IWebElementReference.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
#if !NET8_0_OR_GREATER
20+
21+
// Original code in https://github.com/dotnet/runtime/blob/419e949d258ecee4c40a460fb09c66d974229623/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
22+
23+
namespace System.Diagnostics.CodeAnalysis
24+
{
25+
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
26+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
27+
internal sealed class AllowNullAttribute : Attribute
28+
{ }
29+
30+
/// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
31+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
32+
internal sealed class DisallowNullAttribute : Attribute
33+
{ }
34+
35+
/// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
36+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
37+
internal sealed class MaybeNullAttribute : Attribute
38+
{ }
39+
40+
/// <summary>Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input argument was not null when the call returns.</summary>
41+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
42+
internal sealed class NotNullAttribute : Attribute
43+
{ }
44+
45+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
46+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
47+
internal sealed class MaybeNullWhenAttribute : Attribute
48+
{
49+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
50+
/// <param name="returnValue">
51+
/// The return value condition. If the method returns this value, the associated parameter may be null.
52+
/// </param>
53+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
54+
55+
/// <summary>Gets the return value condition.</summary>
56+
public bool ReturnValue { get; }
57+
}
58+
59+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
60+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
61+
internal sealed class NotNullWhenAttribute : Attribute
62+
{
63+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
64+
/// <param name="returnValue">
65+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
66+
/// </param>
67+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
68+
69+
/// <summary>Gets the return value condition.</summary>
70+
public bool ReturnValue { get; }
71+
}
72+
73+
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
74+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
75+
internal sealed class NotNullIfNotNullAttribute : Attribute
76+
{
77+
/// <summary>Initializes the attribute with the associated parameter name.</summary>
78+
/// <param name="parameterName">
79+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
80+
/// </param>
81+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
82+
83+
/// <summary>Gets the associated parameter name.</summary>
84+
public string ParameterName { get; }
85+
}
86+
87+
/// <summary>Applied to a method that will never return under any circumstance.</summary>
88+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
89+
internal sealed class DoesNotReturnAttribute : Attribute
90+
{ }
91+
92+
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
93+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
94+
internal sealed class DoesNotReturnIfAttribute : Attribute
95+
{
96+
/// <summary>Initializes the attribute with the specified parameter value.</summary>
97+
/// <param name="parameterValue">
98+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
99+
/// the associated parameter matches this value.
100+
/// </param>
101+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
102+
103+
/// <summary>Gets the condition parameter value.</summary>
104+
public bool ParameterValue { get; }
105+
}
106+
107+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
108+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
109+
internal sealed class MemberNotNullAttribute : Attribute
110+
{
111+
/// <summary>Initializes the attribute with a field or property member.</summary>
112+
/// <param name="member">
113+
/// The field or property member that is promised to be not-null.
114+
/// </param>
115+
public MemberNotNullAttribute(string member) => Members = new[] { member };
116+
117+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
118+
/// <param name="members">
119+
/// The list of field and property members that are promised to be not-null.
120+
/// </param>
121+
public MemberNotNullAttribute(params string[] members) => Members = members;
122+
123+
/// <summary>Gets field or property member names.</summary>
124+
public string[] Members { get; }
125+
}
126+
127+
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
128+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
129+
internal sealed class MemberNotNullWhenAttribute : Attribute
130+
{
131+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
132+
/// <param name="returnValue">
133+
/// The return value condition. If the method returns this value, the associated field or property member will not be null.
134+
/// </param>
135+
/// <param name="member">
136+
/// The field or property member that is promised to be not-null.
137+
/// </param>
138+
public MemberNotNullWhenAttribute(bool returnValue, string member)
139+
{
140+
ReturnValue = returnValue;
141+
Members = new[] { member };
142+
}
143+
144+
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
145+
/// <param name="returnValue">
146+
/// The return value condition. If the method returns this value, the associated field and property members will not be null.
147+
/// </param>
148+
/// <param name="members">
149+
/// The list of field and property members that are promised to be not-null.
150+
/// </param>
151+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
152+
{
153+
ReturnValue = returnValue;
154+
Members = members;
155+
}
156+
157+
/// <summary>Gets the return value condition.</summary>
158+
public bool ReturnValue { get; }
159+
160+
/// <summary>Gets field or property member names.</summary>
161+
public string[] Members { get; }
162+
}
163+
}
164+
165+
#endif

dotnet/src/webdriver/Remote/LocalFileDetector.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using System.IO;
2021

2122
#nullable enable
@@ -34,11 +35,7 @@ public class LocalFileDetector : IFileDetector
3435
/// </summary>
3536
/// <param name="keySequence">The sequence to test for file existence.</param>
3637
/// <returns><see langword="true"/> if the key sequence represents a file; otherwise, <see langword="false"/>.</returns>
37-
public bool IsFile(
38-
#if NET8_0_OR_GREATER
39-
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
40-
#endif
41-
string? keySequence)
38+
public bool IsFile([NotNullWhen(true)] string? keySequence)
4239
{
4340
return File.Exists(keySequence);
4441
}

0 commit comments

Comments
 (0)