Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 3eeaea7

Browse files
committed
アクセス制限の有るクラスではpublicなプロパティでもゲッターにアクセスできない場合が有るため、アクセス可能にして使うよう修正
1 parent 27bfe3b commit 3eeaea7

File tree

1 file changed

+8
-2
lines changed
  • src/main/kotlin/com/wrongwrong/mapk/core

1 file changed

+8
-2
lines changed

src/main/kotlin/com/wrongwrong/mapk/core/KMapper.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class KMapper<T: Any>(private val function: KFunction<T>, propertyNameConverter:
4545
fun map(src: Any): T {
4646
val srcMap: Map<String, KProperty1.Getter<*, *>> =
4747
src::class.memberProperties.filterTargets().associate { property ->
48-
val getter = property.getter
48+
val getter = property.getAccessibleGetter()
4949

5050
val key = getter.annotations
5151
.find { it is KPropertyAlias }
@@ -73,7 +73,7 @@ class KMapper<T: Any>(private val function: KFunction<T>, propertyNameConverter:
7373
is Pair<*, *> -> mapOf(arg.first as String to { arg.second })
7474
else -> {
7575
arg::class.memberProperties.filterTargets().associate { property ->
76-
val getter = property.getter
76+
val getter = property.getAccessibleGetter()
7777

7878
val key = getter.annotations
7979
.find { it is KPropertyAlias }
@@ -103,6 +103,12 @@ private fun Collection<KProperty1<*, *>>.filterTargets(): Collection<KProperty1<
103103
}
104104
}
105105

106+
private fun KProperty1<*, *>.getAccessibleGetter(): KProperty1.Getter<*, *> {
107+
// アクセス制限の有るクラスではpublicなプロパティでもゲッターにアクセスできない場合が有るため、アクセス可能にして使う
108+
getter.isAccessible = true
109+
return getter
110+
}
111+
106112
private fun <T : Any> getTarget(clazz: KClass<T>): KFunction<T> {
107113
val constructors: List<KFunction<T>> = clazz.constructors
108114
.filter { it.annotations.any { annotation -> annotation is KConstructor } }

0 commit comments

Comments
 (0)