[java] JSpecify annotations for capabilities
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)- nullvalueallowed (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: https://github.com/SeleniumHQ/selenium/issues/14291
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist
- [x] I have read the contributing document.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
PR Type
enhancement
Description
- Added JSpecify nullness annotations (
@NullMarkedand@Nullable) to several interfaces and classes to improve null safety. - Updated
Capabilities,HasCapabilities,ImmutableCapabilities,MutableCapabilities, andPersistentCapabilitiesto include these annotations. - Enhanced code to help developers avoid potential
NullPointerExceptions.
Changes walkthrough 📝
| Relevant files | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Enhancement |
|
💡 PR-Agent usage: Comment
/helpon the PR to get a list of all available PR-Agent tools and their descriptions
PR Reviewer Guide 🔍
| ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪ |
| 🧪 No relevant tests |
| 🔒 No security concerns identified |
| ⚡ Key issues to review Potential Null Dereference |
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Enhancement |
Use Optional
| 8 |
| Performance |
Optimize capability existence check for better performanceConsider using a more efficient approach to check for capability existence. Instead java/src/org/openqa/selenium/PersistentCapabilities.java [68-72]
Suggestion importance[1-10]: 7Why: The suggestion to use | 7 |
| Best practice |
Improve equality check in the equals method for better null handlingConsider using java/src/org/openqa/selenium/ImmutableCapabilities.java [178-182]
Suggestion importance[1-10]: 6Why: The suggestion to use | 6 |
Use a more specific exception type for input validationConsider using a more specific exception type instead of the generic java/src/org/openqa/selenium/MutableCapabilities.java [87-88]
Suggestion importance[1-10]: 5Why: 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 | 5 |
I pushed one more commit to fix two more NullAway errors (related: https://github.com/SeleniumHQ/selenium/pull/14421). At this point, these errors have been resolved:
java/src/org/openqa/selenium/Capabilities.java:36: error: [NullAway] returning @Nullable expression from method with @NonNull return type
return Stream.of("platformName")
^
(see http://t.uber.com/nullaway )
java/src/org/openqa/selenium/ImmutableCapabilities.java:151: error: [NullAway] passing @Nullable parameter 'v' where @NonNull is required
setCapability(delegate, (String) key, v);
^
(see http://t.uber.com/nullaway )
java/src/org/openqa/selenium/ImmutableCapabilities.java:161: error: [NullAway] returning @Nullable expression from method with @NonNull return type
return delegate.get(capabilityName);
^
(see http://t.uber.com/nullaway )
java/src/org/openqa/selenium/MutableCapabilities.java:111: error: [NullAway] returning @Nullable expression from method with @NonNull return type
return caps.get(capabilityName);
^
(see http://t.uber.com/nullaway )
Actually, in multithreaded applications PersistentCapabilities#asMap() and ImmutableCapabilities#ImmutableCapabilities(Map<?, ?>) could cause problems.
@diemol could you look at this?
Can you elaborate?
Oh, I meant to ask for a review, Could you please review this PR?