-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #755 from k163377/argument-bucket
Changes in constructor invocation and argument management
- Loading branch information
Showing
15 changed files
with
4,071 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import kotlin.reflect.KParameter | ||
|
||
internal class BucketGenerator private constructor(paramSize: Int, instanceParameter: KParameter?, instance: Any?) { | ||
private val originalParameters = arrayOfNulls<KParameter>(paramSize) | ||
private val originalArguments = arrayOfNulls<Any?>(paramSize) | ||
private val initialCount: Int | ||
|
||
init { | ||
if (instance != null) { | ||
originalParameters[0] = instanceParameter | ||
originalArguments[0] = instance | ||
initialCount = 1 | ||
} else { | ||
initialCount = 0 | ||
} | ||
} | ||
|
||
fun generate(): ArgumentBucket = ArgumentBucket( | ||
parameters = originalParameters.clone(), | ||
arguments = originalArguments.clone(), | ||
count = initialCount | ||
) | ||
|
||
companion object { | ||
fun forConstructor(paramSize: Int): BucketGenerator = BucketGenerator(paramSize, null, null) | ||
|
||
fun forMethod(paramSize: Int, instanceParameter: KParameter, instance: Any): BucketGenerator = | ||
BucketGenerator(paramSize, instanceParameter, instance) | ||
} | ||
} | ||
|
||
internal class ArgumentBucket( | ||
private val parameters: Array<KParameter?>, | ||
val arguments: Array<Any?>, | ||
private var count: Int | ||
) : Map<KParameter, Any?> { | ||
operator fun set(key: KParameter, value: Any?) { | ||
arguments[key.index] = value | ||
parameters[key.index] = key | ||
|
||
// Multiple calls are not checked because internally no calls are made more than once per argument. | ||
count++ | ||
} | ||
|
||
val isFullInitialized: Boolean get() = count == arguments.size | ||
|
||
private class Entry(override val key: KParameter, override val value: Any?) : Map.Entry<KParameter, Any?> | ||
|
||
override val entries: Set<Map.Entry<KParameter, Any?>> | ||
get() = parameters.mapNotNull { key -> key?.let { Entry(it, arguments[it.index]) } }.toSet() | ||
override val keys: Set<KParameter> | ||
get() = parameters.filterNotNull().toSet() | ||
override val size: Int | ||
get() = count | ||
override val values: Collection<Any?> | ||
get() = keys.map { arguments[it.index] } | ||
|
||
override fun isEmpty(): Boolean = this.size == 0 | ||
|
||
// Skip the check here, as it is only called after the check for containsKey. | ||
override fun get(key: KParameter): Any? = arguments[key.index] | ||
|
||
override fun containsValue(value: Any?): Boolean = keys.any { arguments[it.index] == value } | ||
|
||
override fun containsKey(key: KParameter): Boolean = parameters.any { it?.index == key.index } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucketTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import org.junit.Ignore | ||
import org.junit.experimental.runners.Enclosed | ||
import org.junit.runner.RunWith | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.functions | ||
import kotlin.reflect.full.hasAnnotation | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
|
||
@RunWith(Enclosed::class) | ||
class ArgumentBucketTest { | ||
class Normal { | ||
@Ignore | ||
data class Constructor(val foo: String, val bar: String) | ||
|
||
@Test | ||
fun constructorTest() { | ||
val function: KFunction<*> = ::Constructor | ||
val params = function.parameters | ||
val generator = BucketGenerator.forConstructor(params.size) | ||
val bucket = generator.generate() | ||
|
||
assertTrue(bucket.isEmpty()) | ||
assertEquals(0, bucket.size) | ||
assertFalse(bucket.isFullInitialized) | ||
|
||
bucket[params[0]] = "foo" | ||
|
||
assertFalse(bucket.isEmpty()) | ||
assertEquals(1, bucket.size) | ||
assertFalse(bucket.isFullInitialized) | ||
assertEquals("foo", bucket[params[0]]) | ||
|
||
bucket[params[1]] = "bar" | ||
|
||
assertFalse(bucket.isEmpty()) | ||
assertEquals(2, bucket.size) | ||
assertTrue(bucket.isFullInitialized) | ||
assertEquals("bar", bucket[params[1]]) | ||
} | ||
|
||
@Ignore | ||
data class Method(val foo: String, val bar: String) { | ||
companion object { | ||
@JvmStatic | ||
@JsonCreator | ||
fun of(foo: String, bar: String): Method = Method(foo, bar) | ||
} | ||
} | ||
|
||
@Test | ||
fun methodTest() { | ||
val function: KFunction<*> = Method.Companion::class.functions.first { it.hasAnnotation<JsonCreator>() } | ||
val params = function.parameters | ||
val generator = BucketGenerator.forMethod(params.size, params[0], Method) | ||
val bucket = generator.generate() | ||
|
||
assertFalse(bucket.isEmpty()) | ||
assertEquals(1, bucket.size) | ||
assertEquals(Method.Companion, bucket[params[0]]) | ||
assertFalse(bucket.isFullInitialized) | ||
|
||
bucket[params[1]] = "foo" | ||
|
||
assertFalse(bucket.isEmpty()) | ||
assertEquals(2, bucket.size) | ||
assertFalse(bucket.isFullInitialized) | ||
assertEquals("foo", bucket[params[1]]) | ||
|
||
bucket[params[2]] = "bar" | ||
|
||
assertFalse(bucket.isEmpty()) | ||
assertEquals(3, bucket.size) | ||
assertTrue(bucket.isFullInitialized) | ||
assertEquals("bar", bucket[params[2]]) | ||
} | ||
} | ||
|
||
// After support, add a case with a value class. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.