summaryrefslogtreecommitdiff
path: root/annotations/generate_annotated_java_files.py
diff options
context:
space:
mode:
authorPete Gillin <peteg@google.com>2018-11-09 13:41:37 +0000
committerPete Gillin <peteg@google.com>2018-11-19 16:20:11 +0000
commit8309e1489e841204836ee3d7d3eb0580e1ac9613 (patch)
treea73a67aef401a68282d24983b2874efb16e08cdb /annotations/generate_annotated_java_files.py
parent6064789ff97baa79c8325873df7c92182f0c8e00 (diff)
Add nullability annotations for ConcurrentHashMap.
This is an unusual case. Normally, even if a Map does not accept null keys, calling containsKey(null) is legitimate, and returns false, so Map.containsKey() takes @Nullable Object. On a CHM, calling containsKey(null) throws NPE. (This behaviour is allowed by the contract of Map, which says "Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter.") This change therefore declares the argument to CHM.containsKey as @NonNull Object. This relationship breaks the normal 'rules' that an implementation can't narrow the type of an argument to an interface method it's implementing, but it is the best representation of reality. The practical consequence is that a warning or error will be generated on chm.containsKey(null) if chm is statically typed as CHM, but not if it is a CHM instance that is statically types as Map. This gap in null-safety checking is an inevitable consequence of the optionality in the contract of Map. There are some rather non-obvious nullability contracts on the functional programming methods on CHM. For example, consider the forEach overload which takes a BiFunction<? super K, ? super V, ? extends U> and a Consumer<? super U>: the BiFunction is allowed to return null, so its third parameter is @Nullable, but the Consumer is not applied in that case, so its parameter is @NonNull. See the class-level and method-level javadoc. Test: make core-current-stubs-nullability-validation-check-nullability-warnings Bug: 64930165 Change-Id: Ia36ef9f841df1dae4ce06b24acd34290d25e415b
Diffstat (limited to 'annotations/generate_annotated_java_files.py')
0 files changed, 0 insertions, 0 deletions