Skip to content

Commit 234d824

Browse files
authored
Merge pull request #627 from k163377/reduce-creator-cache
Merge creator cache for Constructor and Method
2 parents ce4f7a5 + a215244 commit 234d824

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

release-notes/CREDITS-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Contributors:
2020
Ilya Ryzhenkov (@orangy)
2121
* #580: Lazy load UNIT_TYPE
2222

23+
WrongWrong (@k163377)
24+
* #627: Merge creator cache for Constructor and Method
25+
2326
# 2.14.0
2427

2528
Richard Kwasnicki (Richie94@github)

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Co-maintainers:
2222
(fix via [jackson-dataformat-xml#547])
2323
#580: Lazy load UNIT_TYPE
2424
(contributed by Ilya R)
25+
#627: Merge creator cache for Constructor and Method
2526

2627
2.14.2 (28-Jan-2023)
2728
2.14.1 (21-Nov-2022)

src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
66
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams
77
import com.fasterxml.jackson.databind.util.LRUMap
88
import java.lang.reflect.Constructor
9+
import java.lang.reflect.Executable
910
import java.lang.reflect.Method
1011
import kotlin.reflect.KClass
1112
import kotlin.reflect.KFunction
@@ -36,8 +37,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
3637
private val javaClassToKotlin = LRUMap<Class<Any>, KClass<Any>>(reflectionCacheSize, reflectionCacheSize)
3738
private val javaConstructorToKotlin = LRUMap<Constructor<Any>, KFunction<Any>>(reflectionCacheSize, reflectionCacheSize)
3839
private val javaMethodToKotlin = LRUMap<Method, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
39-
private val javaConstructorToValueCreator = LRUMap<Constructor<Any>, ConstructorValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
40-
private val javaMethodToValueCreator = LRUMap<Method, MethodValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
40+
private val javaExecutableToValueCreator = LRUMap<Executable, ValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
4141
private val javaConstructorIsCreatorAnnotated = LRUMap<AnnotatedConstructor, Boolean>(reflectionCacheSize, reflectionCacheSize)
4242
private val javaMemberIsRequired = LRUMap<AnnotatedMember, BooleanTriState?>(reflectionCacheSize, reflectionCacheSize)
4343
private val kotlinGeneratedMethod = LRUMap<AnnotatedMethod, Boolean>(reflectionCacheSize, reflectionCacheSize)
@@ -63,22 +63,25 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
6363
is AnnotatedConstructor -> {
6464
val constructor = _withArgsCreator.annotated as Constructor<Any>
6565

66-
javaConstructorToValueCreator.get(constructor)
66+
javaExecutableToValueCreator.get(constructor)
6767
?: kotlinFromJava(constructor)?.let {
6868
val value = ConstructorValueCreator(it)
69-
javaConstructorToValueCreator.putIfAbsent(constructor, value) ?: value
69+
javaExecutableToValueCreator.putIfAbsent(constructor, value) ?: value
7070
}
7171
}
7272
is AnnotatedMethod -> {
73-
val method = _withArgsCreator.annotated as Method
73+
val method = _withArgsCreator.annotated
7474

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

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

0 commit comments

Comments
 (0)