Skip to content

Replace BooleanTriState with OptBoolean #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.19.0 (not yet released)

WrongWrong (@k163377)
* #954: Replace BooleanTriState with OptBoolean
* #944: Fixed to use common util for Member accessibility override
* #937: Added type match check to read functions

Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Co-maintainers:

2.19.0 (not yet released)

#954: Replaced `OptBoolean` of internal caching with a common implementation.
#944: Common util is now used for member accessibility overrides.
#937: For `readValue` and other shorthands for `ObjectMapper` deserialization methods,
type consistency checks have been added.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.annotation.OptBoolean
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor
import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
Expand All @@ -22,32 +23,12 @@ import kotlin.reflect.jvm.kotlinFunction
internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
companion object {
// Increment is required when properties that use LRUMap are changed.
private const val serialVersionUID = 3L
}

sealed class BooleanTriState(val value: Boolean?) {
class True : BooleanTriState(true)
class False : BooleanTriState(false)
class Empty : BooleanTriState(null)

companion object {
private val TRUE = True()
private val FALSE = False()
private val EMPTY = Empty()

fun fromBoolean(value: Boolean?): BooleanTriState {
return when (value) {
null -> EMPTY
true -> TRUE
false -> FALSE
}
}
}
private const val serialVersionUID = 4L
}

private val javaExecutableToKotlin = LRUMap<Executable, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaExecutableToValueCreator = LRUMap<Executable, ValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaMemberIsRequired = LRUMap<AnnotatedMember, BooleanTriState?>(reflectionCacheSize, reflectionCacheSize)
private val javaMemberIsRequired = LRUMap<AnnotatedMember, OptBoolean?>(reflectionCacheSize, reflectionCacheSize)

// Initial size is 0 because the value class is not always used
private val valueClassReturnTypeCache: LRUMap<AnnotatedMethod, Optional<KClass<*>>> =
Expand Down Expand Up @@ -102,8 +83,8 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
)
} // we cannot reflect this method so do the default Java-ish behavior

fun javaMemberIsRequired(key: AnnotatedMember, calc: (AnnotatedMember) -> Boolean?): Boolean? = javaMemberIsRequired.get(key)?.value
?: calc(key).let { javaMemberIsRequired.putIfAbsent(key, BooleanTriState.fromBoolean(it))?.value ?: it }
fun javaMemberIsRequired(key: AnnotatedMember, calc: (AnnotatedMember) -> Boolean?): Boolean? = javaMemberIsRequired.get(key)?.asBoolean()
?: calc(key).let { javaMemberIsRequired.putIfAbsent(key, OptBoolean.fromBoolean(it))?.asBoolean() ?: it }

private fun AnnotatedMethod.getValueClassReturnType(): KClass<*>? {
val getter = this.member.apply {
Expand Down