https://kotlinlang.org logo
Title
t

tyler

04/23/2019, 4:01 PM
Is it possible for kotlin.collections functions to return a mixture of immutable and mutable lists? I’m trying to java.collections sort a list mutated by .sortByDescending, and a very small percentage, but a substantial amount, of my users are crashing due to an UnsupportedOperationException being thrown by
AbstractList.set(int index, E element)
public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }
p

pablisco

04/23/2019, 4:10 PM
Do you have the code you are using to create the list?
And also, the code that is invoked from Java to get the list
p

pablisco

04/23/2019, 4:13 PM
Bear in mind that the Kotlin
List
returned from
CollectionsKt.listOf("a", "b")
is not going to be mutable but, if accessed from Java it will resolve it as a Java
List
which has no safety regarding mutability
t

tyler

04/23/2019, 4:13 PM
That could be resolved by returning .toMutableList()
p

pablisco

04/23/2019, 4:14 PM
Indeed
t

tyler

04/23/2019, 4:14 PM
Its interesting it doesnt happen on 8.0 and higher
p

pablisco

04/23/2019, 4:15 PM
Now, that’s interesting, yeah 🤔
t

tyler

04/23/2019, 4:16 PM
Ok, well thanks for weighing in.. I appreciate it
p

pablisco

04/23/2019, 4:17 PM
I wonder if
java.util.Arrays$ArrayList
changed it’s implementation in the JVM and/or Dalvik/ART
t

tyler

04/23/2019, 4:18 PM
The thing is, that on an emulator with 7.0 running, I’ve seen both of those list types pass through Collections.sort without throwing
so the safest bet is to use kotlin collections to sort and have java be read only.. maybe using inference to pass a consistent list type
this has been an interesting exercise in kotlin/java interop 🙂
p

pablisco

04/23/2019, 4:20 PM
Yeah, I would also try to move as much as possible to Kotlin, haha (if you can)
t

tyler

04/23/2019, 4:20 PM
I agree 100%, we’re trying to but the app is big, its a delicate balance. Its really crazy though when you start migrating a huge app to Kotlin from Java with a significant number of users, you find strange things like this happen with almost every refactor
p

pablisco

04/23/2019, 4:23 PM
Yeah, it’s tricky. I’ve experience a couple of these WTF issues myself before in a large project that we are slowly migrating
t

tyler

04/23/2019, 4:25 PM
Next I’m trying to figure out how we got here: https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/AbstractList.java#L132 From kotlins .map() and .sortByDescending()
p

pablisco

04/23/2019, 4:29 PM
Ah! it just hit me… If there is only one thing in the list there is the potential that it returns a
SingletonList
(I’m not 100% sure) which will throw this exception.
t

tyler

04/23/2019, 4:30 PM
Really? Thats great info
I dont see SingletonList in the collections.kt source
I’ll add a response on SO 😄
t

tyler

04/23/2019, 4:31 PM
Thank you 🙂
Now, any reason it doesnt happen in 8.0+ ?
p

pablisco

04/23/2019, 4:33 PM
Hmm, bad luck? 😅
or good
t

tyler

04/23/2019, 4:36 PM
I dont think so, we have enough users so that statistically it should be happening
Any way, you’ve been a huge help! I’ll mark your SO answer as accepted once it comes in
p

pablisco

04/23/2019, 4:45 PM
I’m still puzzled by why it works on 8.0 and up
t

tyler

04/23/2019, 4:47 PM
I’m going to verify that, to be 100% sure now that I have the knowledge to reproduce it
👍 1
Previously I couldnt repro, and no users on 8.0+ were crashing according to our crash reports
Yeah so I just verified, its not happening on 9.0. I stepped over Collections.sort on a SingletonList, whereas on 7.0 when attempting to step over, the exception is thrown
p

pablisco

04/23/2019, 5:24 PM
Thinking about this, it may be a later fix on the JDK as a list of one shouldn't be modified when sorting. Need to check the SC