Kotlin data types and Java libs

Hello,

I’m hoping to find out if there are known issues/work-arounds for Java < - > Kotlin data types:

  • JCache / ehcache

  • MyBatis

  • Orika

    Invalid key type, expected : int but was : java.lang.Integer
    java.lang.ClassCastException: Invalid key type, expected : int but was : java.lang.Integer

I’m curious how others are working with Kotlin and using other libraries. Things that use Reflection or whatnot must cause some grief?

Where does this exception come from? Any code samples where this is occurring? Kotlin/library versions?

I was able to get this working using Any as the type, but I assume this means I’m adding some unnecessary type casting to the mix.

The spots where it says Cache<Any, Any> i had as Cache<Int, Int> and was getting the error back from ehcache.

I tried specifying java.lang.Integer and that didnt seem to work. I tried doing java.lang.Integer.valueOf(kotlinInt) and that didn’t seem to work.

Something I am trying to figure out is what is the actual interop of Kotlin and other libraries. My team is feeling a little pain in that when we use mybatis, orika, library X, that there are things that are expecting java.lang.String or java.lang.Integer and we would end up having to write custom mappers or marshallers to deal with Kotlin types for every library we may want to use.

Hopefully I’m just missing something obvious :stuck_out_tongue:

The mapping of Kotlin’s types to Java is documented here. There are no wrappers involved; a kotlin.Int is either int or java.lang.Integer depending on the context, and a kotlin.String is a java.lang.String.

Hello,

I’m not totally sure I am following, but I think you are saying it should just work?

Is something getting confused when this gets compiled? Based on the error, it almost looks like perhaps my code is being turned into a java int primitive, while the ehcache definition is java.lang.Integer? Totally guessing here…

Is there a way to “force” Kotlin to use Integer vs int for a kotlin.Int?

One last - on that page, I dont explicitly see java.lang.Integer called out. Is it mapped (assuming yes)?

I’m not familiar with ehcache so I don’t know why you’re getting the error. If you use Int as a type argument of a generic class, it is always compiled to java.lang.Integer (you can’t use a primitive type as a type argument on the JVM). An Int? is also always compiled to java.lang.Integer.