[java] JSpecify annotations for immutable models and enums
User description
Description
In this PR I'm adding nullness annotations for classes
-
Dimension -
Keys -
Point -
Rectangle -
UsernameAndPassword
Dimension
-
equalsaccepts null argument -> marked with@Nullable
Keys
-
getKeyFromUnicodecan return null value -> return type marked with@Nullable
Point
-
equalsaccepts null argument -> marked with@Nullable
Rectangle
-
equalsaccepts null argument -> marked with@Nullable
UsernameAndPassword
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
@NullMarkedannotations to several classes and enums to improve nullness handling. - Updated
equalsmethods inDimension,Point, andRectangleclasses to accept@Nullableparameters. - Modified
getKeyFromUnicodemethod inKeysenum to return@Nullable. - These changes aim to enhance code safety by preventing 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 Possible Inconsistency |
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Performance |
Improve performance of key lookup by using a Map instead of iterating through all valuesConsider using a more efficient data structure, such as a Map, to store and lookup java/src/org/openqa/selenium/Keys.java [186-190]
Suggestion importance[1-10]: 9Why: The suggestion improves performance by replacing iteration with a Map for constant-time lookups, which is a significant enhancement for efficiency. | 9 |
| Enhancement |
Simplify the equals method using instanceof pattern matchingConsider using java/src/org/openqa/selenium/Dimension.java [44-47]
Suggestion importance[1-10]: 8Why: The suggestion correctly simplifies the | 8 |
Convert the class to a record for a more concise and immutable representationConsider using a record instead of a class for Rectangle, as it's an immutable data java/src/org/openqa/selenium/Rectangle.java [25-30]
Suggestion importance[1-10]: 8Why: Converting the class to a record provides a more concise and immutable representation, aligning with modern Java practices for data classes. | 8 | |
| Best practice |
Make fields final to ensure immutability of the classConsider making the java/src/org/openqa/selenium/Point.java [26-28]
Suggestion importance[1-10]: 7Why: Making fields final enhances the immutability of the class, which is a good practice for ensuring thread safety and reducing potential bugs. | 7 |