Questions tagged [kotlin-reflect]
121 questions
4
votes
3 answers
Method reference to property setter
How could I get method reference to property setter without using kotlin-reflect?
Basically, if I'll write my code in java way it's super simple
fun setValue(i: Int) = Unit
val a: (Int) -> Unit = this::setValue
But for var value: Int I'm getting…

Stepango
- 4,721
- 3
- 31
- 44
4
votes
1 answer
Android build fails with kotlin-reflect and proguard
I am unable to build my release product if I include kotlin-reflect with it. I attempted adding this to proguard configuration:
-keep class kotlin.reflect.** { *; }
But it did not help at all. Here is the error result from the gradle…

David Pisoni
- 3,317
- 2
- 25
- 35
3
votes
1 answer
How to use Kotlin reflection from Java
Is it possible to use Kotlin reflection from Java?
I want to get KCallable from Kotlin function in Java and use its method callBy to call method with default arguments.
Example in Kotlin:
fun test(a: String = "default", b: String): String {
…

Lukáš Moravec
- 65
- 6
3
votes
1 answer
How to add annotation on main constructor parameters in kotlin
Having the following class:
data class TestMsg(
@Parse(";")
val someArray: Array
)
And trying to get the annotation with
TestMsg::class.primaryConstructor!!.parameters.forEach{
println(it.findAnnotation())
}
There is no…

zack evein
- 279
- 5
- 13
3
votes
1 answer
Enumerate the extension properties defined in a package
I'm writing an HTML templating language in Kotlin.
My templating engine will need to resolve property expressions, such as obj.myProperty, by looking up "myProperty" not just among the members defined in obj's class and superclasses, but also among…

Tobia
- 17,856
- 6
- 74
- 93
3
votes
1 answer
Using kotlin-reflect to find the data type of data class's properties
Given a simple data class like:
data class TestSimple(
val country: String,
var city: String? = null,
var number: Int? = null,
var code: Long? = null,
var amount: Float? = null,
var balance: Double? = null
)
Is there any way…

marcoseu
- 3,892
- 2
- 16
- 35
3
votes
3 answers
Kotlin Class should have a single no-arg constructor
I instantiate my object using:
val currency = Currency::class.createInstance()
.copy(code = "GBP")
but I'm getting an exception at that line:
java.lang.IllegalArgumentException: Class should have a single no-arg constructor: class…

ericn
- 12,476
- 16
- 84
- 127
2
votes
1 answer
Kotlin: implicit conversion from Int to nullable Double fails in reflection
I was trying to convert a Map to data class instance:
data class Person(
val name: String,
val age: Int,
val weight: Double?,
)
fun test() {
val map = mapOf(
"name" to "steven",
"age" to 30,
…

zwlxt
- 301
- 2
- 9
2
votes
1 answer
Reflection on Jetpack Compose
Currently, I’m creating a new SDK that contains sensitive fields that shouldn’t be read by the consumers (Think Credit Card Number field) and I’m using Jetpack Compose to create the forms, my question is, is it possible to do a reflection on Jetpack…

Mahmoud Elshamy
- 578
- 1
- 5
- 13
2
votes
1 answer
How to use member references to create a hierarchical path to a property in Kotlin
I want to create a String representation of the path from a root element A through it's declared member properties, until I get to a specific leaf element, using Kotlin member references. To clarify, suppose I have a class structure like this:
data…

fiftyone_51
- 105
- 7
2
votes
1 answer
Hot to get the parameterless constructor through Kotlin Reflection API?
Given a domain class with a parameterless constructor, how do we get a reference to that constructor through the Reflection API?
Consider for example a Student data class, such as:
data class Student(var nr: Int = 0, var name: String? =…

Miguel Gamboa
- 8,855
- 7
- 47
- 94
2
votes
2 answers
Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
I'm learning Kotlin and I have problem with this code:
import kotlin.reflect.*
fun main(args: Array) {
val lst = listOf(1,2,3,45,5,6,7,8)
val mapped = lst.map {Data(::isEven,it)}
for(item in mapped){
var result =…

Jan Válek
- 424
- 4
- 14
2
votes
3 answers
What's the correct way to iterate through properties of a singleton in Kotlin?
As the title suggests, I wanted to iterate through properties of a singleton object. I tried using the kotlin-reflect as there was no other way I found currently.
object Test {
const val a = "String"
const val b = "Another…

Animesh Sahu
- 7,445
- 2
- 21
- 49
2
votes
1 answer
Setting a nullable UShort using kotlin-reflect
Why can't I set a UShort using reflection in kotlin? I extracted my problem into a unit test.
My test looks like this:
class Junk {
var DA: UShort? = null
}
class Tests {
@Test
fun testSetShort() {
var uut = Junk()
val…

wz2b
- 1,017
- 7
- 24
2
votes
0 answers
The jetified-kotlin-reflect version not the same as jetified-kotlin-stdlib
When I compile my project, the error message show that the version of kotlin-reflect outOfDate, but I can't find how to update it.
w: Runtime JAR files in the classpath should have the same version. These files were found in the…

ccd
- 5,788
- 10
- 46
- 96