Skip to content

Commit 10a7219

Browse files
committed
[java] Add JSpecify annotations for By locators
1 parent 3bad4c7 commit 10a7219

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
}
@@ -341,9 +344,9 @@ public interface Remotable {
341344

342345
class Parameters {
343346
private final String using;
344-
private final Object value;
347+
private final @Nullable Object value;
345348

346-
public Parameters(String using, Object value) {
349+
public Parameters(String using, @Nullable Object value) {
347350
this.using = Require.nonNull("Search mechanism", using);
348351
// There may be subclasses where the value is optional. Allow for this.
349352
this.value = value;
@@ -353,7 +356,7 @@ public String using() {
353356
return using;
354357
}
355358

356-
public Object value() {
359+
public @Nullable Object value() {
357360
return value;
358361
}
359362

@@ -363,7 +366,7 @@ public String toString() {
363366
}
364367

365368
@Override
366-
public boolean equals(Object o) {
369+
public boolean equals(@Nullable Object o) {
367370
if (!(o instanceof Parameters)) {
368371
return false;
369372
}
@@ -376,8 +379,8 @@ public int hashCode() {
376379
return Objects.hash(using, value);
377380
}
378381

379-
private Map<String, Object> toJson() {
380-
Map<String, Object> params = new HashMap<>();
382+
private Map<String, @Nullable Object> toJson() {
383+
Map<String, @Nullable Object> params = new HashMap<>();
381384
params.put("using", using);
382385
params.put("value", value);
383386
return Collections.unmodifiableMap(params);
@@ -409,7 +412,7 @@ public final Parameters getRemoteParameters() {
409412
return params;
410413
}
411414

412-
protected final Map<String, Object> toJson() {
415+
protected final Map<String, @Nullable Object> toJson() {
413416
return getRemoteParameters().toJson();
414417
}
415418
}
@@ -440,7 +443,7 @@ public final Parameters getRemoteParameters() {
440443
return remoteParams;
441444
}
442445

443-
protected final Map<String, Object> toJson() {
446+
protected final Map<String, @Nullable Object> toJson() {
444447
return fallback.toJson();
445448
}
446449

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(
@@ -54,5 +54,6 @@ java_library(
5454
deps = [
5555
"//java/src/org/openqa/selenium:core",
5656
"//java/src/org/openqa/selenium/support/ui:components",
57+
artifact("org.jspecify:jspecify"),
5758
],
5859
)

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)