Questions tagged [kotlin-reflect]
121 questions
6
votes
5 answers
Combining/merging data classes in Kotlin
Is there a way to merge kotlin data classes without specifying all the properties?
data class MyDataClass(val prop1: String, val prop2: Int, ...//many props)
with a function with the following signature:
fun merge(left: MyDataClass, right:…

Ben Flowers
- 1,434
- 7
- 21
- 49
5
votes
1 answer
Data class metadata is removed with proguard / R8 for Kotlin 1.6.0
I have a package with some data classes and I'm trying to access the constructor at runtime using Kotlin reflection clazz.primaryConstructor,
Everything is working as expected but when I enable R8, data classes metadata are removed so for example…

Ahmad Mahmoud Saleh
- 169
- 1
- 4
- 15
5
votes
1 answer
Why are Kotlin interfaces "not open"?
In Kotlin 1.4.30, when I type
open interface I
the Kotlin compiler warns me that modifier 'open' is redundant for 'interface'. This makes complete sense: Of course interfaces are open, otherwise they would be useless.
However, the reflection…

blubb
- 9,510
- 3
- 40
- 82
5
votes
1 answer
I can't see private members in a Kotlin KClass
I'm working on a library that uses reflection, and I would like to manipulate all the properties / function of a given KClass.
Using the KClass::members property, I can manipulate all the accessible members (as per the documentation), ie: the…

XGouchet
- 10,002
- 10
- 48
- 83
5
votes
0 answers
IllegalStateException: failed to analyze: java.lang.AssertionError: annotation tree hasn't been attributed yet: @kotlin.Metadata
We get the following build error when updating from Kotlin 1.3.21 to 1.3.30+ in Android Studio 3.4.1:
e: java.lang.IllegalStateException: failed to analyze: java.lang.AssertionError: annotation tree hasn't been attributed yet: @kotlin.Metadata(mv =…

Philipp Fahlteich
- 361
- 3
- 15
5
votes
1 answer
Generic extensions of KProperty1 in Kotlin
I have the following code:
import kotlin.reflect.KProperty1
infix fun KProperty1.eq(value: R) {
println(this.name + " = $value")
}
infix fun KProperty1.eq(value: KProperty1) {
println(this.name + " = " +…

Morgoth
- 4,935
- 8
- 40
- 66
5
votes
2 answers
Declare a function generic type parameter in a way that allows both for nullable types and using that type parameter to declare `KClass`
KClass is defined as public interface KClass : KDeclarationContainer, KAnnotatedElement, KClassifier
This is tricky, because the class of a String? should be KClass, but is impossible to obtain.
Given the following 3 examples below…

Burg
- 1,988
- 1
- 18
- 22
5
votes
1 answer
Read Kotlin function annotation value using reflection?
Given an interface method like this (Android Retrofit), how do I read the URL path specified in the annotation argument from Kotlin code at runtime?
ApiDefinition interface:
@GET("/api/somepath/objects/")
fun getObjects(...)
Read the annotation…

Ollie C
- 28,313
- 34
- 134
- 217
5
votes
1 answer
What is the purpose of having bound class reference return a covariant type?
I'm playing with reflection and I came out with this problem. When using bound class reference via the ::class syntax, I get a covariant KClass type:
fun foo(entry: T) {
with(entry::class) {
this // is instance of KClass
…

devrocca
- 2,497
- 2
- 19
- 27
4
votes
0 answers
Moshi: When is Reflection needed?
Currently using Moshi with Retrofit but need to look back as I noticed I am not implementing it right and confused when to add kotlin-reflect in the dependencies.
According to README
The reflection adapter requires the following additional…

Bitwise DEVS
- 2,858
- 4
- 24
- 67
4
votes
1 answer
Kotlin Reflection: primaryConstructor not found with R8 obfuscation
In my current Kotlin Android project I just had to face the following problem:
For this class hierarchy
abstract class MyClass {
...
}
class MySubClassA: MyClass() {
...
}
class MySubClassB: MyClass() {
...
}
class MySubClassC: MyClass()…

Nantoka
- 4,174
- 1
- 33
- 36
4
votes
0 answers
How to set proguard for Kotlin-reflect?
I'm currently making a module using Kotlin-reflect. When I try to build a binary as a release mode, there is no proguard error, but module is not working.
instance::class.memberProperties.forEach {
val name = it.annotations.find { annotation ->…

Kyoung-june Yi
- 503
- 1
- 3
- 16
4
votes
2 answers
How can I turn a KFunction without instance param to a KFunction with it?
class X {
fun someFunc(x: Int, y: String, z: Double) {
println("x = [$x], y = [$y], z = [$z]")
}
}
fun main(args: Array) {
val func = X::someFunc
val instance = X()
func.call(instance, 1, "Hi", 123.45)
}
Given…

Mibac
- 8,990
- 5
- 33
- 57
4
votes
1 answer
How to get a kotlin package by reflection
Kotlin reflection library defines KDeclarationContainer, which Represents an entity which may contain declarations of any other entities, such as a class or a package.
this::class returns KClass, which extends KDeclarationContainer, but how do I get…

tango24
- 464
- 2
- 11
4
votes
1 answer
Kotlin's reflection : Unknown type parameter
I am running some experiments on Kotlin's reflection.
I am trying to get a reflection object of a generic class with its argument.
In Java, that would be a ParameterizedType.
The way to get such a thing using Java's reflection API is a bit…

Salomon BRYS
- 9,247
- 5
- 29
- 44