You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this PR I'm adding nullness annotations for interfaces and classes:
Capabilities
HasCapabilities
ImmutableCapabilities
MutableCapabilities
PersistentCapabilities
Capabilities
getPlatformName() - can return null value -> marked with @Nullable
getCapability(String capabilityName) - can return null value when capability with specified name does not exist -> marked with @Nullable
HasCapabilities
No null values
ImmutableCapabilities
getCapability(String capabilityName) - follows from Capabilities#getCapability(String capabilityName) -> marked with @Nullable
equals(Object o) - null parameter allowed -> marked with @Nullable
MutableCapabilities
setCapability(String key, Object value) - null value allowed (then capability with specified key will be removed) -> marked with @Nullable
getCapability(String capabilityName) - follows from Capabilities#getCapability(String capabilityName) -> marked with @Nullable
equals(Object o) - null parameter allowed -> marked with @Nullable
PersistentCapabilities
getCapability(String capabilityName) - follows from Capabilities#getCapability(String capabilityName) -> marked with @Nullable
equals(Object o) - null parameter allowed -> marked with @Nullable
Motivation and Context
The JSpecify nullness annotations will give developers better exposure to potential problems with their code to avoid NullPointerExceptions.
Related issue: #14291
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
Potential Null Dereference The getPlatformName() method now returns @Nullable Platform, but the stream operations that follow don't handle the potential null value explicitly.
Use Optional instead of @nullable Platform for better null handling
Consider using Optional as the return type for getPlatformName() instead of @Nullable Platform. This would make it clearer that the method might not return a platform and would allow for more idiomatic handling of the potential absence of a platform.
Why: Using Optional<Platform> is a more idiomatic way to handle the potential absence of a value in Java, providing clearer semantics and better null handling compared to @Nullable. This change improves code readability and maintainability.
8
Performance
Optimize capability existence check for better performance
Consider using a more efficient approach to check for capability existence. Instead of getting the capability and then checking for null, you could use the containsKey method of the underlying map if available.
public @Nullable Object getCapability(String capabilityName) {
Require.nonNull("Capability name", capabilityName);
- Object capability = overrides.getCapability(capabilityName);- if (capability != null) {- return capability;+ if (overrides.asMap().containsKey(capabilityName)) {+ return overrides.getCapability(capabilityName);+ }+ return caps.getCapability(capabilityName);
Apply this suggestion
Suggestion importance[1-10]: 7
Why: The suggestion to use containsKey can improve performance by avoiding unnecessary retrieval of map values. This change optimizes the code by reducing the number of operations needed to check for capability existence.
7
Best practice
Improve equality check in the equals method for better null handling
Consider using Objects.equals() for the equality check in the equals method instead of the instanceof operator. This would provide a more robust equality check that handles null values correctly.
public boolean equals(@Nullable Object o) {
- if (!(o instanceof Capabilities)) {- return false;- }+ if (this == o) return true;+ if (!(o instanceof Capabilities)) return false;
return SharedCapabilitiesMethods.equals(this, (Capabilities) o);
Apply this suggestion
Suggestion importance[1-10]: 6
Why: The suggestion to use Objects.equals() is not implemented in the improved code. However, adding a self-check (this == o) is a good practice for performance optimization. The suggestion partially addresses best practices for equality checks.
6
Use a more specific exception type for input validation
Consider using a more specific exception type instead of the generic IllegalArgumentException when validating the capability name. This would provide more precise error information and improve error handling.
public void setCapability(String key, @Nullable Object value) {
- Require.nonNull("Capability name", key);+ if (key == null) {+ throw new IllegalArgumentException("Capability name cannot be null");+ }
Apply this suggestion
Suggestion importance[1-10]: 5
Why: While using a more specific exception type can improve error handling, the suggestion does not actually implement this change, and the existing code already uses Require.nonNull for validation. The suggestion is not directly applicable to the current code.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Description
In this PR I'm adding nullness annotations for interfaces and classes:
Capabilities
HasCapabilities
ImmutableCapabilities
MutableCapabilities
PersistentCapabilities
Capabilities
getPlatformName()
- can return null value -> marked with@Nullable
getCapability(String capabilityName)
- can return null value when capability with specified name does not exist -> marked with@Nullable
HasCapabilities
No null values
ImmutableCapabilities
getCapability(String capabilityName)
- follows fromCapabilities#getCapability(String capabilityName)
-> marked with@Nullable
equals(Object o)
- null parameter allowed -> marked with@Nullable
MutableCapabilities
setCapability(String key, Object value)
- nullvalue
allowed (then capability with specified key will be removed) -> marked with@Nullable
getCapability(String capabilityName)
- follows fromCapabilities#getCapability(String capabilityName)
-> marked with@Nullable
equals(Object o)
- null parameter allowed -> marked with@Nullable
PersistentCapabilities
getCapability(String capabilityName)
- follows fromCapabilities#getCapability(String capabilityName)
-> marked with@Nullable
equals(Object o)
- null parameter allowed -> marked with@Nullable
Motivation and Context
The JSpecify nullness annotations will give developers better exposure to potential problems with their code to avoid NullPointerExceptions.
Related issue: #14291
Types of changes
Checklist
PR Type
enhancement
Description
@NullMarked
and@Nullable
) to several interfaces and classes to improve null safety.Capabilities
,HasCapabilities
,ImmutableCapabilities
,MutableCapabilities
, andPersistentCapabilities
to include these annotations.NullPointerExceptions
.Changes walkthrough 📝
Capabilities.java
Add nullness annotations to Capabilities interface
java/src/org/openqa/selenium/Capabilities.java
@NullMarked
annotation to the interface.getPlatformName
andgetCapability
methods with@Nullable
.HasCapabilities.java
Add nullness annotations to HasCapabilities interface
java/src/org/openqa/selenium/HasCapabilities.java
@NullMarked
annotation to the interface.ImmutableCapabilities.java
Add nullness annotations to ImmutableCapabilities class
java/src/org/openqa/selenium/ImmutableCapabilities.java
@NullMarked
annotation to the class.getCapability
andequals
methods with@Nullable
.MutableCapabilities.java
Add nullness annotations to MutableCapabilities class
java/src/org/openqa/selenium/MutableCapabilities.java
@NullMarked
annotation to the class.setCapability
,getCapability
, andequals
methods with@Nullable
.PersistentCapabilities.java
Add nullness annotations to PersistentCapabilities class
java/src/org/openqa/selenium/PersistentCapabilities.java
@NullMarked
annotation to the class.getCapability
andequals
methods with@Nullable
.