java.lang.IndexOutOfBoundsException depending on the location of base class
See original GitHub issueDescribe the bug
So this is a weird one.
I just got a java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
compile error when trying to serialize a subclass of an abstract class in a UnitTest.
(See the stack trace below)
The abstract base class looks like this:
@Serializable
abstract class ContentObject(val uid: String = UUID.randomUUID().toString())
The UnitTest is in another file and looks like this:
@Test
fun `Child class of ContentObject has uid`(){
@Serializable
class TestClass : ContentObject()
val map = Mapper.map(TestClass.serializer(),TestClass())
assertEquals(false,map.isEmpty())
}
Trying to find out what the problem is, I copied the abstract base class into my UnitTest class. Having the base class inside of the UnitTest class fixes the issue, I am able to compile the UnitTest successfully. When removing the abstract base class from the UnitTest class again, the compilation fails and the same compile error occures.
This also happens when compiling for the android device directly (not using UnitTests).
To Reproduce Open Android Studio, create a new project. Put abstract base class (with property as shown above) in it’s own file in the “main” app scope. Put UnitTest in the “test” app scope. Compile. (Alternatively, look at this sample project: KotlinCompileError-IndexOutOfBoundsException.zip)
Expected behavior The IndexOutOfBoundsException compile exception shouldn’t occur for a class that when moved to another place compiles successfully. I guess there seems to be something weird going on with the code generation?
Environment
- Kotlin version: 1.3.41 / 1.3.50-eap-54
- Library version: 0.11.1
- Kotlin platforms: JVM (Android)
- Gradle version: 5.1.1
- IDE version: Android Studio 3.4.2
Stack Trace
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at kotlinx.serialization.internal.SerialClassDescImpl.getElementName(SerialClassDescImpl.kt:80)
at kotlinx.serialization.NamedValueEncoder.elementName(Tagged.kt:159)
at kotlinx.serialization.NamedValueEncoder.getTag(Tagged.kt:156)
at kotlinx.serialization.NamedValueEncoder.getTag(Tagged.kt:155)
at kotlinx.serialization.TaggedEncoder.encodeStringElement(Tagged.kt:122)
at com.dev.haasedev.wooner.contentObjects.ContentObject.write$Self(ContentObject.kt:18)
at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass.write$Self(DatabaseSerializerTest.kt)
at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass$$serializer.serialize(DatabaseSerializerTest.kt)
at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass$$serializer.serialize(DatabaseSerializerTest.kt:141)
at kotlinx.serialization.Encoder$DefaultImpls.encodeSerializableValue(Coders.kt:42)
at kotlinx.serialization.TaggedEncoder.encodeSerializableValue(Tagged.kt:21)
at kotlinx.serialization.CoreKt.encode(Core.kt:73)
at kotlinx.serialization.Mapper.map(Mapper.kt:101)
at kotlinx.serialization.Mapper$Companion.map(Mapper.kt:138)
at com.dev.haasedev.wooner.DatabaseSerializerTest.Child class of ContentObject has uid(DatabaseSerializerTest.kt:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top GitHub Comments
I have just ran into this issue (or something closely related) in Kotlin 1.3.50, however did not have any issue with 1.3.41. I’ve put the stack trace for my case below.
I came across this thread before coming up with a minimal failing example, however I do also have an abstract class, and a serializable derived class. In my case both these classes are in the same file, but are used from elsewhere (including tests).
I can confirm what @Brainfree said is what is happening to me to. When I remove the typealias I don’t get the exception during the build anymore.