-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
122 additions
and
51 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
38 changes: 38 additions & 0 deletions
38
...-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/type/KotlinTypeCache.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,38 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.kotlin.type | ||
|
||
import com.navercorp.fixturemonkey.api.container.ConcurrentLruCache | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.memberFunctions | ||
|
||
private val CONSTRUCTORS = ConcurrentLruCache<KClass<*>, Collection<KFunction<*>>>(2048) | ||
private val KOTLIN_TYPES = ConcurrentLruCache<Class<*>, KClass<*>>(2048) | ||
private val MEMBER_FUNCTIONS = ConcurrentLruCache<KClass<*>, Collection<KFunction<*>>>(2048) | ||
fun Class<*>.declaredKotlinConstructors(): Collection<KFunction<*>> = | ||
CONSTRUCTORS.computeIfAbsent(this.cachedKotlin()) { it.constructors } | ||
|
||
fun Class<*>.cachedKotlin(): KClass<*> = KOTLIN_TYPES.computeIfAbsent(this) { it.kotlin } | ||
|
||
fun Class<*>.kotlinMemberFunctions(): Collection<KFunction<*>> = | ||
MEMBER_FUNCTIONS.computeIfAbsent(this.cachedKotlin()) { cachedKotlin().memberFunctions } | ||
|
||
fun KClass<*>.cachedMemberFunctions(): Collection<KFunction<*>> = | ||
MEMBER_FUNCTIONS.computeIfAbsent(this) { this.memberFunctions } |
69 changes: 69 additions & 0 deletions
69
...ey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/type/KotlinTypeExtensions.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,69 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.kotlin.type | ||
|
||
import com.navercorp.fixturemonkey.api.type.TypeReference | ||
import com.navercorp.fixturemonkey.api.type.Types | ||
import java.lang.reflect.AnnotatedType | ||
import java.lang.reflect.Type | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.jvm.javaType | ||
|
||
/** | ||
* Provides a collection of extension functions for Type, | ||
* facilitating the conversion and handling of types in Kotlin reflection. | ||
* | ||
* These utilities enhance the interoperability between Kotlin's type system and Java's | ||
* type system, particularly focusing on [TypeReference]. | ||
*/ | ||
fun Type.actualType(): Class<*> = Types.getActualType(this) | ||
|
||
fun <T> Class<T>.toAnnotatedType(): AnnotatedType = Types.generateAnnotatedTypeWithoutAnnotation(this) | ||
|
||
fun Type.toAnnotatedType(): AnnotatedType = Types.generateAnnotatedTypeWithoutAnnotation(this) | ||
|
||
fun <T> Class<T>.toTypeReference(): TypeReference<T> = object : TypeReference<T>() { | ||
override fun getType(): Type { | ||
return this@toTypeReference | ||
} | ||
|
||
override fun getAnnotatedType(): AnnotatedType { | ||
return Types.generateAnnotatedTypeWithoutAnnotation(this@toTypeReference) | ||
} | ||
} | ||
|
||
fun Type.toTypeReference(): TypeReference<*> = object : TypeReference<Any?>() { | ||
override fun getType(): Type { | ||
return this@toTypeReference | ||
} | ||
|
||
override fun getAnnotatedType(): AnnotatedType { | ||
return Types.generateAnnotatedTypeWithoutAnnotation(this@toTypeReference) | ||
} | ||
} | ||
|
||
fun KType.toTypeReference(): TypeReference<*> = object : TypeReference<Any?>() { | ||
override fun getType(): Type { | ||
return this@toTypeReference.javaType | ||
} | ||
|
||
override fun getAnnotatedType(): AnnotatedType { | ||
return this@toTypeReference.javaType.toAnnotatedType() | ||
} | ||
} |
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