Describe 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
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)
Bug is still present in Kotlin 1.3.50-eap-54
Probably related to #69
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).
e: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:167)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:188)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:167)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:188)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.analyzePropertiesSerializers(SerializationPluginDeclarationChecker.kt:155)
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.check(SerializationPluginDeclarationChecker.kt:44)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.runDeclarationCheckers(ModifiersChecker.java:272)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifierListCommon(ModifiersChecker.java:213)
at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifiersForDeclaration(ModifiersChecker.java:172)
at org.jetbrains.kotlin.resolve.DeclarationsChecker.process(DeclarationsChecker.kt:91)
at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:245)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:225)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:60)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:111)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:81)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:555)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:82)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:546)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:349)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:105)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:237)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:37)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:79)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:91)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:606)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:99)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1645)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Wow, I have the same issue. Any workaround?
1.3.50
lib version - 0.12.0
Not the same. Stacktrace is the same. But problem here if u use typeaslias in class which marked serializable..
I am getting the same issue with 1.3.50. I probably need to stick to 1.3.41 for now.
For me, I had a property which had a type which was a type-alias in another module (which was a Map
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.
Can someone try Kotlin 1.3.60? It seems that problem there is now a runtime error instead of compile-time
@sandwwraith It works for me ... no runtime error. See #563
I'm talking about #609 which is a purely cross-module issue. Seems that original bug does not happen anymore on 1.3.60; if you encounter it again, please re-open with a reproducer
Most helpful comment
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).