Questions tagged [kotlin-reflect]
121 questions
25
votes
4 answers
Java/Android/Kotlin: Reflection on private Field and call public methods on it
Is it possiable to use reflection to access a object's private field and call a public methods on this field?
i.e
class Hello {
private World word
}
class World {
public BlaBlaBla foo()
}
Hello h = new Hello()
World world = reflect on the…

TeeTracker
- 7,064
- 8
- 40
- 46
17
votes
2 answers
Proguard and Kotlin-Reflect/Kotlin Annotations
Looking for some help from someone who puts the pro in proguard.
Annotations used by kotlin-reflect (required dependency for jackson-module-kotlin v v2.8.8) are getting stripped out after upgrading to kotlin 1.1.2-3. The error from proguard is:
…

Bryan
- 367
- 3
- 11
15
votes
2 answers
How to obtain a KType in Kotlin?
I'm experimenting with the reflection functionality in Kotlin, but I can't seem to understand how to obtain a KType value.
Suppose I have a class that maps phrases to object factories. In case of ambiguity, the user can supply a type parameter that…

user1582024
- 715
- 1
- 6
- 15
14
votes
3 answers
Reference to Kotlin class property setter as function
In the example below, t::x returns a reference to a property getter. How do I obtain the same for a setter?
class Test(var x: String) {}
fun main(args: Array) {
val t = Test("A")
val getter: () -> String = t::x
…

David Soroko
- 8,521
- 2
- 39
- 51
13
votes
1 answer
Kotlin reflect proguard SmallSortedMap
Warning: kotlin.reflect.jvm.internal.KClassImpl: can't find referenced class kotlin.reflect.jvm.internal.KClassImpl$kotlin.reflect.jvm.internal.KClassImpl$Data
Warning: kotlin.reflect.jvm.internal.KClassImpl: can't find referenced class…

gaara87
- 2,087
- 3
- 21
- 43
12
votes
2 answers
How to change a kotlin private val using reflection?
I can access a private val value using reflection as below
fun main() {
val mainClass = MainClass()
val f = MainClass::class.memberProperties.find { it.name == "info" }
f?.let {
it.isAccessible = true
val w =…

Elye
- 53,639
- 54
- 212
- 474
11
votes
2 answers
Kotlin Reflection with Proguard fails
Let's say I have this class
data class Person(val name: String?)
When I proguard and run the app I am getting the following exception
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: No accessors or field is found for property val…

ImMathan
- 3,911
- 4
- 29
- 45
9
votes
0 answers
kotlin-reflect with proguard causes reflection errors
I'm using ProGuard for my spring boot application code. After I upgraded to Spring Boot 2, I cannot start my application anymore.
Spring Boot 2 uses kotlin-reflect to create beans, which uses kotlin.Metadata annotation during reflection. This…

rosencreuz
- 1,276
- 10
- 21
8
votes
2 answers
Kotlin runtime error kotlin.Any is not found
I have a multi-module project. With the same build, the apk's size may differ by 300KB (from 17 to 17.3MB). In the build with 17MB in runtime I get an error:
java.lang.AssertionError: Built-in class kotlin.Any is not found.
Also in the project,…

Mikhail
- 800
- 8
- 21
8
votes
1 answer
Reflectively calling function and using default parameters
Given the following function
fun function(x: Int = 12) {
println("x = $x")
}
How can I using reflection invoke it without specifying x (or somehow using default value, not hard coding it)?

Mibac
- 8,990
- 5
- 33
- 57
7
votes
1 answer
(Reflection) Implicit and explicit lambda declaration
I am trying to understand the reflection. I have the following code:
fun main(args: Array) {
println(lengthL1())
println(lengthL2(s))
println(lengthL1.get()) // Error
println(lengthL2.get(s)) //…

LiTTle
- 1,811
- 1
- 20
- 37
6
votes
1 answer
Two Additional types in default constructor in Kotlin?
Since I've been using kotlin-reflect to invoke my default and declared one, I see second different constructor.
I have realized that two different fields int arg3 and kotlin.jvm.internal.DefaultConstructorMarker arg4 added to my constructor.
data…

hcknl
- 1,219
- 10
- 15
6
votes
1 answer
Get the class of nullable type
I am trying to match the type of the nullable String? in a Kotlin reflection exercise:
data class Test(a: String, b: String?)
val test = Test("1", "2")
val properties = test::class.declaredMemberProperties
val propertyNames =…

Morgoth
- 4,935
- 8
- 40
- 66
6
votes
1 answer
Kotlin-reflect and android gradle plugin 3.1.0
I'm using org.jetbrains.kotlin:kotlin-reflect library in my project (included in gradle dependencies).
The app has been working without any problems however after upgrading android gradle plugin to newest version 3.1.0 the app started to crash on…

Marcin Bak
- 1,410
- 14
- 25
6
votes
2 answers
How can I instantiate an object using default constructor parameter values in Kotlin?
I have data class with default values.
data class Project(
val code: String,
val name: String,
val categories: List = emptyList())
Java reflection fails to instantiate the class when some values are null. I'm getting exception…

rosencreuz
- 1,276
- 10
- 21