[java] JSpecify annotations for By locators
User description
Description
In this PR I'm adding nullness annotations for classes
-
By -
ByIdOrName -
ByAll -
ByChained
Common
-
boolean equals(Object o)- accepts null -> an argument marked with@Nullable -
Map<String, Object> toJson()- the value in map can be null (basis on theBy.Remotable.Parameters#toJson()-> value type marked with@Nullable
By
-
Remotable.Parameterscan contain null value - based on the comment "There may be subclasses where the value is optional. Allow for this." in theParametersconstructor -> value field marked with@Nullable
ByIdOrName
No null values
ByAll
No null values
ByChained
No null values
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) toBy,ByIdOrName,ByAll, andByChainedclasses to improve null safety. - Updated
toJsonmethods to handle nullable values. - Added
org.jspecify:jspecifydependency to the Bazel build file.
Changes walkthrough 📝
| Relevant files | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Enhancement |
| ||||||||
| Dependencies |
|
💡 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 Annotation Consistency Map Value Annotation |
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Possible bug |
Add a null check in the equals method to prevent NullPointerExceptionConsider checking for java/src/org/openqa/selenium/By.java [164]
Suggestion importance[1-10]: 9Why: Adding a null check in the equals method is a good practice to prevent potential NullPointerExceptions, which enhances the robustness of the code. | 9 |
| Enhancement |
Use
| 8 |
| Possible issue |
Add null check for
| 7 |
Change the map to not explicitly allow nullable values to avoid handling issuesUse java/src/org/openqa/selenium/By.java [382]
Suggestion importance[1-10]: 5Why: While this suggestion could simplify handling null values, it contradicts the purpose of using | 5 |
/help
PR Agent Walkthrough 🤖
Welcome to the PR Agent, an AI-powered tool for automated pull request analysis, feedback, suggestions and more.
Here is a list of tools you can use to interact with the PR Agent:
| Tool | Description | Trigger Interactively :gem: |
|---|---|---|
| Generates PR description - title, type, summary, code walkthrough and labels |
| |
| Adjustable feedback about the PR, possible issues, security concerns, review effort and more |
| |
| Code suggestions for improving the PR |
| |
| Automatically updates the changelog |
| |
|
ADD DOCS 💎 |
Generates documentation to methods/functions/classes that changed in the PR |
|
|
TEST 💎 |
Generates unit tests for a specific component, based on the PR code change |
|
| Code suggestions for a specific component that changed in the PR |
| |
|
ANALYZE 💎 |
Identifies code components that changed in the PR, and enables to interactively generate tests, docs, and code suggestions for each component |
|
| Answering free-text questions about the PR |
[*] | |
| Generates custom labels for the PR, based on specific guidelines defined by the user |
[*] | |
| Generates feedback and analysis for a failed CI job |
[*] | |
| Generates custom suggestions for improving the PR code, derived only from a specific guidelines prompt defined by the user |
[*] | |
| Automatically retrieves and presents similar issues |
[*] |
(1) Note that each tool be triggered automatically when a new PR is opened, or called manually by commenting on a PR.
(2) Tools marked with [*] require additional parameters to be passed. For example, to invoke the /ask tool, you need to comment on a PR: /ask "<question content>". See the relevant documentation for each tool for more details.
/describe
PR Description updated to latest commit (https://github.com/SeleniumHQ/selenium/commit/10a72199c915a3acc2123678ded139d1e5763cfd)
- [ ] Copy walkthrough table to "Files Changed" Tab
Is there a way to check in the CI that the annotations are correct?
Yes, adding a null checker plugin to the CI building is a natural follow-up for nullness annotations. Currently, I've started trying to use NullAway with Bazel, for this moment I don't see any blockers, but I don't have anything ready to present yet
Could we add a check in the CI to verify the annotations are correct before merging more PRs?
Sure! I'll play with it and share the results, I think I'll have something to show and merge next week
@mk868, will you add unit test for this?
@VietND96 the plan is to test nullness annotations using the NullAway. This tool will highlight where null values are passed to NonNull fields. PR with NullAway is here: https://github.com/SeleniumHQ/selenium/pull/14421, but it's blocked by:
- https://github.com/SeleniumHQ/selenium/pull/14768
- PR with the inclusion of SpotBugs analysis in all locations (I'll create PR when PR#14768 merged)
@mk868, the dependent PR is merged, you can go ahead with others
@VietND96 PR created: https://github.com/SeleniumHQ/selenium/pull/14882
@diemol, do you think this is good to merge?