Skip to content

Commit

Permalink
Rename: ReflectionCache -> ConstructorInfoCache
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Jun 4, 2016
1 parent 156e466 commit c101dd7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import kotlin.reflect.declaredMemberProperties
import kotlin.reflect.jvm.javaType
import kotlin.reflect.primaryConstructor

class ReflectionCache {
private val cacheData =
mutableMapOf<KClass<*>, ConstructorInfo<*>>()
class ConstructorInfoCache {
private val cacheData = mutableMapOf<KClass<*>, ConstructorInfo<*>>()

@Suppress("UNCHECKED_CAST")
operator fun <T : Any> get(cls: KClass<T>): ConstructorInfo<T> =
Expand Down
18 changes: 9 additions & 9 deletions src/main/kotlin/deserialization/Deserializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ inline fun <reified T: Any> deserialize(json: Reader): T {
}

fun <T: Any> deserialize(json: Reader, targetClass: KClass<T>): T {
val seed = ObjectSeed(targetClass, ReflectionCache())
val seed = ObjectSeed(targetClass, ConstructorInfoCache())
var currentCompositeSeed: Seed = seed
val seedStack = Stack<Seed>()

Expand Down Expand Up @@ -68,7 +68,7 @@ class SimpleValueSeed(val value: Any?): Seed {
override fun spawn() = value
}

abstract class CompositeSeed(val reflectionCache: ReflectionCache): Seed {
abstract class CompositeSeed(val constructorInfoCache: ConstructorInfoCache): Seed {
protected fun createSeedForType(paramType: Type): Seed {
val paramClass = paramType.asJavaClass()

Expand All @@ -78,18 +78,18 @@ abstract class CompositeSeed(val reflectionCache: ReflectionCache): Seed {
"Unsupported parameter type $this")

val elementType = parameterizedType.actualTypeArguments.single()
return CollectionSeed(elementType, reflectionCache)
return CollectionSeed(elementType, constructorInfoCache)
}
return ObjectSeed(paramClass.kotlin, reflectionCache)
return ObjectSeed(paramClass.kotlin, constructorInfoCache)
}
}

class ObjectSeed<out T: Any>(
targetClass: KClass<T>,
reflectionCache: ReflectionCache
) : CompositeSeed(reflectionCache) {
constructorInfoCache: ConstructorInfoCache
) : CompositeSeed(constructorInfoCache) {

private val constructorInfo = reflectionCache[targetClass]
private val constructorInfo = constructorInfoCache[targetClass]

private val arguments = mutableMapOf<KParameter, Seed>()
private fun Seed.record(param: KParameter) = apply { arguments[param] = this }
Expand All @@ -112,8 +112,8 @@ class ObjectSeed<out T: Any>(

class CollectionSeed(
val elementType: Type,
reflectionCache: ReflectionCache
) : CompositeSeed(reflectionCache) {
constructorInfoCache: ConstructorInfoCache
) : CompositeSeed(constructorInfoCache) {

private val elements = mutableListOf<Seed>()
private fun Seed.record() = apply { elements.add(this) }
Expand Down

0 comments on commit c101dd7

Please sign in to comment.