From 3429e367b18371c2c8be020795446cbf4d21a383 Mon Sep 17 00:00:00 2001 From: Maciej Kucharczyk Date: Tue, 14 Jan 2025 19:29:50 +0100 Subject: [PATCH] [java] Add nullness for exceptions --- .../openqa/selenium/NoSuchElementException.java | 10 +++++++--- .../org/openqa/selenium/NotFoundException.java | 10 +++++++--- .../StaleElementReferenceException.java | 10 +++++++--- .../org/openqa/selenium/WebDriverException.java | 17 ++++++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/java/src/org/openqa/selenium/NoSuchElementException.java b/java/src/org/openqa/selenium/NoSuchElementException.java index 6bd0cf79e84f0..0ec0c01558883 100644 --- a/java/src/org/openqa/selenium/NoSuchElementException.java +++ b/java/src/org/openqa/selenium/NoSuchElementException.java @@ -17,24 +17,28 @@ package org.openqa.selenium; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + /** * Thrown by {@link WebDriver#findElement(By) WebDriver.findElement(By by)} and {@link * WebElement#findElement(By by) WebElement.findElement(By by)}. */ +@NullMarked public class NoSuchElementException extends NotFoundException { private static final String SUPPORT_URL = BASE_SUPPORT_URL + "#no-such-element-exception"; - public NoSuchElementException(String reason) { + public NoSuchElementException(@Nullable String reason) { super(reason); } - public NoSuchElementException(String reason, Throwable cause) { + public NoSuchElementException(@Nullable String reason, @Nullable Throwable cause) { super(reason, cause); } @Override - public String getSupportUrl() { + public @Nullable String getSupportUrl() { return SUPPORT_URL; } } diff --git a/java/src/org/openqa/selenium/NotFoundException.java b/java/src/org/openqa/selenium/NotFoundException.java index 77aabd949ba21..5ec34c4832a72 100644 --- a/java/src/org/openqa/selenium/NotFoundException.java +++ b/java/src/org/openqa/selenium/NotFoundException.java @@ -17,19 +17,23 @@ package org.openqa.selenium; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + +@NullMarked public class NotFoundException extends WebDriverException { public NotFoundException() {} - public NotFoundException(String message) { + public NotFoundException(@Nullable String message) { super(message); } - public NotFoundException(String message, Throwable cause) { + public NotFoundException(@Nullable String message, @Nullable Throwable cause) { super(message, cause); } - public NotFoundException(Throwable cause) { + public NotFoundException(@Nullable Throwable cause) { super(cause); } } diff --git a/java/src/org/openqa/selenium/StaleElementReferenceException.java b/java/src/org/openqa/selenium/StaleElementReferenceException.java index 8f8d8ed045448..547293c7cf1a4 100644 --- a/java/src/org/openqa/selenium/StaleElementReferenceException.java +++ b/java/src/org/openqa/selenium/StaleElementReferenceException.java @@ -17,24 +17,28 @@ package org.openqa.selenium; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + /** * Indicates that a reference to an element is now "stale" --- the element no longer appears on the * DOM of the page. */ +@NullMarked public class StaleElementReferenceException extends WebDriverException { private static final String SUPPORT_URL = BASE_SUPPORT_URL + "#stale-element-reference-exception"; - public StaleElementReferenceException(String message) { + public StaleElementReferenceException(@Nullable String message) { super(message); } - public StaleElementReferenceException(String message, Throwable cause) { + public StaleElementReferenceException(@Nullable String message, @Nullable Throwable cause) { super(message, cause); } @Override - public String getSupportUrl() { + public @Nullable String getSupportUrl() { return SUPPORT_URL; } } diff --git a/java/src/org/openqa/selenium/WebDriverException.java b/java/src/org/openqa/selenium/WebDriverException.java index 1ac297425d751..cd113457ec1a6 100644 --- a/java/src/org/openqa/selenium/WebDriverException.java +++ b/java/src/org/openqa/selenium/WebDriverException.java @@ -22,8 +22,11 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; import org.openqa.selenium.net.HostIdentifier; +@NullMarked public class WebDriverException extends RuntimeException { public static final String SESSION_ID = "Session ID"; @@ -37,15 +40,15 @@ public WebDriverException() { super(); } - public WebDriverException(String message) { + public WebDriverException(@Nullable String message) { super(message); } - public WebDriverException(Throwable cause) { + public WebDriverException(@Nullable Throwable cause) { super(cause); } - public WebDriverException(String message, Throwable cause) { + public WebDriverException(@Nullable String message, @Nullable Throwable cause) { super(message, cause); } @@ -58,7 +61,7 @@ public WebDriverException(String message, Throwable cause) { * @return the detail message string of this exception. */ @Override - public String getMessage() { + public @Nullable String getMessage() { return getCause() instanceof WebDriverException ? super.getMessage() : createMessage(super.getMessage()); @@ -70,11 +73,11 @@ public String getMessage() { * @return the simple message string of this exception. * @see #getMessage() */ - public String getRawMessage() { + public @Nullable String getRawMessage() { return super.getMessage(); } - private String createMessage(String originalMessageString) { + private String createMessage(@Nullable String originalMessageString) { String supportMessage = Optional.ofNullable(getSupportUrl()) .map(url -> String.format("For documentation on this error, please visit: %s", url)) @@ -105,7 +108,7 @@ public static String getHostInformation() { HostIdentifier.getHostName(), HostIdentifier.getHostAddress()); } - public String getSupportUrl() { + public @Nullable String getSupportUrl() { return null; }