Why is it possible to use an ImmutableList with parcel.readList
?
void someParcelFunction(Parcel parcel) {
ImmutableList<String> myList = ImmutableList.of();
parcel.readList(myList, ImmutableList.class.getClassLoader(), String.class);
}
When I look at the source code for parcel.readList
I can see that the add
method is called on the outVal
(first argument) of the method:
private <T> void readListInternal(@NonNull List<? super T> outVal, int n,
@Nullable ClassLoader loader, @Nullable Class<T> clazz) {
while (n > 0) {
T value = readValue(loader, clazz);
//Log.d(TAG, "Unmarshalling value=" + value);
outVal.add(value);
n--;
}
}
But in Guava's documentation it looks like this method is deprecated and should throw an UnsupportedOperationException
: https://guava.dev/releases/20.0/api/docs/com/google/common/collect/ImmutableList.html#add-int-E-. And this would make sense because not only should an Immutable list be immutable, but I am also initialising this list to empty and then it seems like it's actually being modified.
However, the code above runs and no exceptions are thrown. Why is that?
n
is more than 0. Other than that, this looks fundamentally destined to fail, so you need to provide a more complete example, like creation of the Parcel. Could it also be that the code is throwing, but you're catching and swallowing the exception somewhere else?