Add nullability annotations to JSONObject.

Since names cannot be null, get(null) is guaranteed to throw, so the
parameter to get() is declared @NonNull. Since opt(null) is guaranteed
to return null, the parameter to opt() is declared @Nullable. The
type-specific get* and opt* methods follow that pattern.

The overload of optString which takes a fallback actually does accept
a null fallback, and may return null if the fallback is null but not
otherwise. The most helpful thing seems to be to declare the fallback
parameter and the return type as both @NonNull since that is
presumably the common usage.

The toString() implementation does return null if a JSONException was
thrown during the processing. It seems like this shouldn't be possible
for a validly constituted instance, and forcing users to do null
checks on the returned String seems like it would not be helpful, so
the return value is annotated as @NonNull. (JSONStringer.toString also
returns null if its out field is zero length, but the preceding call
to writeTo(JSONStringer) will ensure that at least the { and } will
have been appended, so that can't happen.)

Test: make core-libart
Bug: 64930165
Change-Id: Ie5a8c6c212b86035a1daeff14b784a5ca9e0f626
1 file changed