Skip to content

Commit

Permalink
Add explicit @nullable annotations to all type arguments inside org.j…
Browse files Browse the repository at this point in the history
…son package, in order for J2KT to correctly infer nullability in types on the boundary between non-null-marked and null-marked scope.

PiperOrigin-RevId: 541739440
  • Loading branch information
micapolos-google authored and copybara-github committed Jun 20, 2023
1 parent 2e327ea commit 21378cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
public class JSONArray {

@UnsupportedAppUsage
private final List<Object> values;
private final List<@Nullable Object> values;

/**
* Creates a {@code JSONArray} with no values.
*/
public JSONArray() {
values = new ArrayList<Object>();
values = new ArrayList<@Nullable Object>();
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ public JSONArray(Object array) throws JSONException {
throw new JSONException("Not a primitive array: " + array.getClass());
}
final int length = Array.getLength(array);
values = new ArrayList<Object>(length);
values = new ArrayList<@Nullable Object>(length);
for (int i = 0; i < length; ++i) {
put(JSONObject.wrap(Array.get(array, i)));
}
Expand Down Expand Up @@ -292,7 +292,7 @@ public Object get(int index) throws JSONException {
}
return value;
} catch (IndexOutOfBoundsException e) {
throw new JSONException("Index " + index + " out of range [0.." + values.size() + ")", e);
throw new JSONException("Index " + index + " out of range [0.." + values.size() + ")", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ public class JSONObject {
};

@UnsupportedAppUsage
private final LinkedHashMap<@NonNull String, Object> nameValuePairs;
private final LinkedHashMap<@NonNull String, @Nullable Object> nameValuePairs;

/**
* Creates a {@code JSONObject} with no name/value mappings.
*/
public JSONObject() {
nameValuePairs = new LinkedHashMap<String, Object>();
nameValuePairs = new LinkedHashMap<@NonNull String, @Nullable Object>();
}

/**
Expand Down Expand Up @@ -689,7 +689,7 @@ public long optLong(@Nullable String name, long fallback) {
@Nullable public JSONArray names() {
return nameValuePairs.isEmpty()
? null
: new JSONArray(new ArrayList<String>(nameValuePairs.keySet()));
: new JSONArray(new ArrayList<@Nullable String>(nameValuePairs.keySet()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ enum Scope {
* levels of nesting.
*/
@UnsupportedAppUsage
private final List<Scope> stack = new ArrayList<Scope>();
private final List<@Nullable Scope> stack =
new ArrayList<@Nullable Scope>();

/**
* A string containing a full set of spaces for a single level of
Expand Down

0 comments on commit 21378cc

Please sign in to comment.