Skip to content

[java] Add nullness for exceptions #15081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions java/src/org/openqa/selenium/NoSuchElementException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 7 additions & 3 deletions java/src/org/openqa/selenium/NotFoundException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 7 additions & 3 deletions java/src/org/openqa/selenium/StaleElementReferenceException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 10 additions & 7 deletions java/src/org/openqa/selenium/WebDriverException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand All @@ -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());
Expand All @@ -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))
Expand Down Expand Up @@ -105,7 +108,7 @@ public static String getHostInformation() {
HostIdentifier.getHostName(), HostIdentifier.getHostAddress());
}

public String getSupportUrl() {
public @Nullable String getSupportUrl() {
return null;
}

Expand Down