Add check for Optional used as a field type #490
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.
This adds a check to generate a warning when
Optional
is used as a field type.Optional
is primarily meant as a return type, not a field type. See for example Brian Goetz, Java’s language architect, shedding light on Oracle’s intention with theOptional
type.Example code which did not generate any warnings, but hides a potential
NullPointerException
in a concurrency scenario:Whereas this will generate a warning:
and this will not:
This is a follow-up to the suggestion made here: openhab/openhab-addons#19061 (comment)
A few @openhab/add-ons-maintainers already favors the idea, but it would be good to hear from @openhab/core-maintainers as well in case anyone thinks this approach is too drastic or are directly against it.
It should be mentioned that some add-ons already use
Optional
as a field type and thus will start having these warnings. I intend to create at least a few pull requests to address some of those.Personal test notes:
Then to use the check when building an add-on: