Skip to content

Commit 3f1d167

Browse files
mk868diemolVietND96
authored
[java] JSpecify annotations for By locators (#14372)
[java] Add JSpecify annotations for By locators Co-authored-by: Diego Molina <[email protected]> Co-authored-by: Viet Nguyen Duc <[email protected]>
1 parent 1fd8b11 commit 3f1d167

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

java/src/org/openqa/selenium/By.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Objects;
2525
import java.util.regex.Pattern;
26+
import org.jspecify.annotations.NullMarked;
27+
import org.jspecify.annotations.Nullable;
2628
import org.openqa.selenium.internal.Require;
2729

2830
/**
@@ -40,6 +42,7 @@
4042
* }
4143
* </code></pre>
4244
*/
45+
@NullMarked
4346
public abstract class By {
4447
/**
4548
* @param id The value of the "id" attribute to search for.
@@ -158,7 +161,7 @@ protected JavascriptExecutor getJavascriptExecutor(SearchContext context) {
158161
}
159162

160163
@Override
161-
public boolean equals(Object o) {
164+
public boolean equals(@Nullable Object o) {
162165
if (!(o instanceof By)) {
163166
return false;
164167
}
@@ -342,9 +345,9 @@ public interface Remotable {
342345

343346
class Parameters {
344347
private final String using;
345-
private final Object value;
348+
private final @Nullable Object value;
346349

347-
public Parameters(String using, Object value) {
350+
public Parameters(String using, @Nullable Object value) {
348351
this.using = Require.nonNull("Search mechanism", using);
349352
// There may be subclasses where the value is optional. Allow for this.
350353
this.value = value;
@@ -354,7 +357,7 @@ public String using() {
354357
return using;
355358
}
356359

357-
public Object value() {
360+
public @Nullable Object value() {
358361
return value;
359362
}
360363

@@ -364,7 +367,7 @@ public String toString() {
364367
}
365368

366369
@Override
367-
public boolean equals(Object o) {
370+
public boolean equals(@Nullable Object o) {
368371
if (!(o instanceof Parameters)) {
369372
return false;
370373
}
@@ -377,8 +380,8 @@ public int hashCode() {
377380
return Objects.hash(using, value);
378381
}
379382

380-
private Map<String, Object> toJson() {
381-
Map<String, Object> params = new HashMap<>();
383+
private Map<String, @Nullable Object> toJson() {
384+
Map<String, @Nullable Object> params = new HashMap<>();
382385
params.put("using", using);
383386
params.put("value", value);
384387
return Collections.unmodifiableMap(params);
@@ -410,7 +413,7 @@ public final Parameters getRemoteParameters() {
410413
return params;
411414
}
412415

413-
protected final Map<String, Object> toJson() {
416+
protected final Map<String, @Nullable Object> toJson() {
414417
return getRemoteParameters().toJson();
415418
}
416419
}
@@ -441,7 +444,7 @@ public final Parameters getRemoteParameters() {
441444
return remoteParams;
442445
}
443446

444-
protected final Map<String, Object> toJson() {
447+
protected final Map<String, @Nullable Object> toJson() {
445448
return fallback.toJson();
446449
}
447450

java/src/org/openqa/selenium/support/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//java:defs.bzl", "java_export", "java_library")
1+
load("//java:defs.bzl", "artifact", "java_export", "java_library")
22
load("//java:version.bzl", "SE_VERSION")
33

44
java_export(
@@ -57,5 +57,6 @@ java_library(
5757
deps = [
5858
"//java/src/org/openqa/selenium:core",
5959
"//java/src/org/openqa/selenium/support/ui:components",
60+
artifact("org.jspecify:jspecify"),
6061
],
6162
)

java/src/org/openqa/selenium/support/ByIdOrName.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.io.Serializable;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import org.jspecify.annotations.NullMarked;
2324
import org.openqa.selenium.By;
2425
import org.openqa.selenium.NoSuchElementException;
2526
import org.openqa.selenium.SearchContext;
2627
import org.openqa.selenium.WebElement;
2728

29+
@NullMarked
2830
public class ByIdOrName extends By implements Serializable {
2931

3032
private static final long serialVersionUID = 3986638402799576701L;

java/src/org/openqa/selenium/support/pagefactory/ByAll.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.Serializable;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import org.jspecify.annotations.NullMarked;
2324
import org.openqa.selenium.By;
2425
import org.openqa.selenium.NoSuchElementException;
2526
import org.openqa.selenium.SearchContext;
@@ -36,6 +37,7 @@
3637
* will find all elements that match <var>by1</var> and then all elements that match <var>by2</var>.
3738
* This means that the list of elements returned may not be in document order.
3839
*/
40+
@NullMarked
3941
public class ByAll extends By implements Serializable {
4042

4143
private static final long serialVersionUID = 4573668832699497306L;

java/src/org/openqa/selenium/support/pagefactory/ByChained.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.stream.Collectors;
2424
import java.util.stream.Stream;
25+
import org.jspecify.annotations.NullMarked;
2526
import org.openqa.selenium.By;
2627
import org.openqa.selenium.NoSuchElementException;
2728
import org.openqa.selenium.SearchContext;
@@ -38,6 +39,7 @@
3839
* will find all elements that match <var>by2</var> and appear under an element that matches
3940
* <var>by1</var>.
4041
*/
42+
@NullMarked
4143
public class ByChained extends By implements Serializable {
4244

4345
private static final long serialVersionUID = 1563769051170172451L;

0 commit comments

Comments
 (0)