Skip to content
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

Merge creator cache for Constructor and Method #627

Merged
merged 2 commits into from
Feb 18, 2023
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
3 changes: 3 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Contributors:
Ilya Ryzhenkov (@orangy)
* #580: Lazy load UNIT_TYPE

WrongWrong (@k163377)
* #627: Merge creator cache for Constructor and Method

# 2.14.0

Richard Kwasnicki (Richie94@github)
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 @@ -22,6 +22,7 @@ Co-maintainers:
(fix via [jackson-dataformat-xml#547])
#580: Lazy load UNIT_TYPE
(contributed by Ilya R)
#627: Merge creator cache for Constructor and Method

2.14.2 (28-Jan-2023)
2.14.1 (21-Nov-2022)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams
import com.fasterxml.jackson.databind.util.LRUMap
import java.lang.reflect.Constructor
import java.lang.reflect.Executable
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
Expand Down Expand Up @@ -36,8 +37,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
private val javaClassToKotlin = LRUMap<Class<Any>, KClass<Any>>(reflectionCacheSize, reflectionCacheSize)
private val javaConstructorToKotlin = LRUMap<Constructor<Any>, KFunction<Any>>(reflectionCacheSize, reflectionCacheSize)
private val javaMethodToKotlin = LRUMap<Method, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaConstructorToValueCreator = LRUMap<Constructor<Any>, ConstructorValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaMethodToValueCreator = LRUMap<Method, MethodValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaExecutableToValueCreator = LRUMap<Executable, ValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaConstructorIsCreatorAnnotated = LRUMap<AnnotatedConstructor, Boolean>(reflectionCacheSize, reflectionCacheSize)
private val javaMemberIsRequired = LRUMap<AnnotatedMember, BooleanTriState?>(reflectionCacheSize, reflectionCacheSize)
private val kotlinGeneratedMethod = LRUMap<AnnotatedMethod, Boolean>(reflectionCacheSize, reflectionCacheSize)
Expand All @@ -63,22 +63,25 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
is AnnotatedConstructor -> {
val constructor = _withArgsCreator.annotated as Constructor<Any>

javaConstructorToValueCreator.get(constructor)
javaExecutableToValueCreator.get(constructor)
?: kotlinFromJava(constructor)?.let {
val value = ConstructorValueCreator(it)
javaConstructorToValueCreator.putIfAbsent(constructor, value) ?: value
javaExecutableToValueCreator.putIfAbsent(constructor, value) ?: value
}
}
is AnnotatedMethod -> {
val method = _withArgsCreator.annotated as Method
val method = _withArgsCreator.annotated

javaMethodToValueCreator.get(method)
javaExecutableToValueCreator.get(method)
?: kotlinFromJava(method)?.let {
val value = MethodValueCreator.of(it)
javaMethodToValueCreator.putIfAbsent(method, value) ?: value
javaExecutableToValueCreator.putIfAbsent(method, value) ?: value
}
}
else -> throw IllegalStateException("Expected a constructor or method to create a Kotlin object, instead found ${_withArgsCreator.annotated.javaClass.name}")
else -> throw IllegalStateException(
"Expected a constructor or method to create a Kotlin object," +
" instead found ${_withArgsCreator.annotated.javaClass.name}"
)
} // we cannot reflect this method so do the default Java-ish behavior

fun checkConstructorIsCreatorAnnotated(key: AnnotatedConstructor, calc: (AnnotatedConstructor) -> Boolean): Boolean = javaConstructorIsCreatorAnnotated.get(key)
Expand Down