From 10a72199c915a3acc2123678ded139d1e5763cfd Mon Sep 17 00:00:00 2001 From: Maciej Kucharczyk Date: Sat, 10 Aug 2024 14:58:20 +0200 Subject: [PATCH] [java] Add JSpecify annotations for By locators --- java/src/org/openqa/selenium/By.java | 21 +++++++++++-------- .../org/openqa/selenium/support/BUILD.bazel | 3 ++- .../openqa/selenium/support/ByIdOrName.java | 2 ++ .../selenium/support/pagefactory/ByAll.java | 2 ++ .../support/pagefactory/ByChained.java | 2 ++ 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/java/src/org/openqa/selenium/By.java b/java/src/org/openqa/selenium/By.java index 7a03e34bf5b0b..333c1434de065 100644 --- a/java/src/org/openqa/selenium/By.java +++ b/java/src/org/openqa/selenium/By.java @@ -23,6 +23,8 @@ import java.util.Map; import java.util.Objects; import java.util.regex.Pattern; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; import org.openqa.selenium.internal.Require; /** @@ -40,6 +42,7 @@ * } * */ +@NullMarked public abstract class By { /** * @param id The value of the "id" attribute to search for. @@ -158,7 +161,7 @@ protected JavascriptExecutor getJavascriptExecutor(SearchContext context) { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (!(o instanceof By)) { return false; } @@ -341,9 +344,9 @@ public interface Remotable { class Parameters { private final String using; - private final Object value; + private final @Nullable Object value; - public Parameters(String using, Object value) { + public Parameters(String using, @Nullable Object value) { this.using = Require.nonNull("Search mechanism", using); // There may be subclasses where the value is optional. Allow for this. this.value = value; @@ -353,7 +356,7 @@ public String using() { return using; } - public Object value() { + public @Nullable Object value() { return value; } @@ -363,7 +366,7 @@ public String toString() { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (!(o instanceof Parameters)) { return false; } @@ -376,8 +379,8 @@ public int hashCode() { return Objects.hash(using, value); } - private Map toJson() { - Map params = new HashMap<>(); + private Map toJson() { + Map params = new HashMap<>(); params.put("using", using); params.put("value", value); return Collections.unmodifiableMap(params); @@ -409,7 +412,7 @@ public final Parameters getRemoteParameters() { return params; } - protected final Map toJson() { + protected final Map toJson() { return getRemoteParameters().toJson(); } } @@ -440,7 +443,7 @@ public final Parameters getRemoteParameters() { return remoteParams; } - protected final Map toJson() { + protected final Map toJson() { return fallback.toJson(); } diff --git a/java/src/org/openqa/selenium/support/BUILD.bazel b/java/src/org/openqa/selenium/support/BUILD.bazel index 071272e394af4..eba6564ad01d2 100644 --- a/java/src/org/openqa/selenium/support/BUILD.bazel +++ b/java/src/org/openqa/selenium/support/BUILD.bazel @@ -1,4 +1,4 @@ -load("//java:defs.bzl", "java_export", "java_library") +load("//java:defs.bzl", "artifact", "java_export", "java_library") load("//java:version.bzl", "SE_VERSION") java_export( @@ -54,5 +54,6 @@ java_library( deps = [ "//java/src/org/openqa/selenium:core", "//java/src/org/openqa/selenium/support/ui:components", + artifact("org.jspecify:jspecify"), ], ) diff --git a/java/src/org/openqa/selenium/support/ByIdOrName.java b/java/src/org/openqa/selenium/support/ByIdOrName.java index e83d8b38c5122..fd4ffccdf4a9b 100644 --- a/java/src/org/openqa/selenium/support/ByIdOrName.java +++ b/java/src/org/openqa/selenium/support/ByIdOrName.java @@ -20,11 +20,13 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.jspecify.annotations.NullMarked; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.SearchContext; import org.openqa.selenium.WebElement; +@NullMarked public class ByIdOrName extends By implements Serializable { private static final long serialVersionUID = 3986638402799576701L; diff --git a/java/src/org/openqa/selenium/support/pagefactory/ByAll.java b/java/src/org/openqa/selenium/support/pagefactory/ByAll.java index 0a9e0988bc0fc..403dc359f0354 100644 --- a/java/src/org/openqa/selenium/support/pagefactory/ByAll.java +++ b/java/src/org/openqa/selenium/support/pagefactory/ByAll.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.jspecify.annotations.NullMarked; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.SearchContext; @@ -36,6 +37,7 @@ * will find all elements that match by1 and then all elements that match by2. * This means that the list of elements returned may not be in document order. */ +@NullMarked public class ByAll extends By implements Serializable { private static final long serialVersionUID = 4573668832699497306L; diff --git a/java/src/org/openqa/selenium/support/pagefactory/ByChained.java b/java/src/org/openqa/selenium/support/pagefactory/ByChained.java index 5d631cc254c24..f95f139b428de 100644 --- a/java/src/org/openqa/selenium/support/pagefactory/ByChained.java +++ b/java/src/org/openqa/selenium/support/pagefactory/ByChained.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jspecify.annotations.NullMarked; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.SearchContext; @@ -38,6 +39,7 @@ * will find all elements that match by2 and appear under an element that matches * by1. */ +@NullMarked public class ByChained extends By implements Serializable { private static final long serialVersionUID = 1563769051170172451L;