AbstractValueAdaptingCache.get checks whether the requested type matches with the value type in the cache. This fails for Kotlin Boolean value type. The usage is as follows:
Cache spec: maximumSize=100,expireAfterWrite=5s
myCache.putIfAbsent(uuid, true) // uuid is a string
myCache.get(uuid, Boolean::class.java)
The type in AbstractValueAdaptingCache.get shows up as boolean, which seems to be the problem, but is correct according to Kotlin docs.
On the JVM, non-nullable values of this type are represented as values of the primitive type boolean.
I'm not aware of a way to use myCache.get with a nullable Boolean type that'd then be translated to Java Boolean wrapper type. Boolean?::class.java isn't valid Kotlin code.
Spring Boot 2.3.4.RELEASE, Spring 5.2.9.RELEASE, Caffeine cache 2.8.6.
Comment From: sbrannen
@sdeleuze, any thoughts on this one?
Comment From: sdeleuze
As you can see here:
- kotlin.Boolean is mapped to Java boolean primitive type
- kotlin.Boolean? is mapped to java.lang.Boolean
putIfAbsent is defined as public ValueWrapper putIfAbsent(Object key, @Nullable Object value) so when passing true the parameter in there is autoboxing and the type stored is java.lang.Boolean.
Boolean::class.java by default indeed returns the the primitive type but you can use Boolean::class.javaObjectType and your example will work as expected.
Based on that I think it works as expected and that we can close this issue. Please reopen with a more information if you think there is something we should fix.