Questions tagged [kotlin-reflect]

121 questions
1
vote
0 answers

How to check if a Kotlin type (KClass) is basic (primitive), or not, at runtime (e.g. Reflection API)?

Kotlin specification states basic types as "Some types can have a special internal representation - for example, numbers, characters and booleans can be represented as primitive values at runtime". https://kotlinlang.org/docs/basic-types.html Yet, I…
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
1
vote
1 answer

How to find the KClass of the owner of a companion object?

I'm trying to get the class that owns a companion object so I can create loggers with inline technique inside a companion object but referencing the main class that is being logged and not the companion object. The problem is that I can't find a way…
Polyana Fontes
  • 3,156
  • 1
  • 27
  • 41
1
vote
1 answer

Kotlin - Merge two data class

Data class data class A( var data: List ) { data class Data( var key: String, var count: Long = 0, var sub: List? = null ) } A class data values expressed in json. [ { "data": [ { …
nkhcode
  • 87
  • 1
  • 8
1
vote
0 answers

How can I unit test a function using the same obfuscated files as in my release build?

In my app I’ve recently updated Kotlin from 1.5.31 to 1.6.10. Since then a specific function that uses reflection (kotlin-reflect) stopped working as expected. It works perfectly fine in 1.5.31. Now, when I run the debug build, the function works…
MatJB
  • 106
  • 2
  • 8
1
vote
1 answer

Kotlin is it possible to delegate function or function parameters?

I want to have for example: class Foo { fun doSomething(arg1: String, arg2: String, arg3: Boolean) } class FooDelegate { //different fun name fun execute by Foo::doSomething } Either with reflection or other way. What I currently have…
htafoya
  • 18,261
  • 11
  • 80
  • 104
1
vote
2 answers

Listing all the properties in a Kotlin class excluding the ones with custom getters

Is there a simple method I can use to list all of the properties in a class that don't implement a custom getter? For example: class Person(val name: String, val age: Int) { val signature: String get() = "$name - $age" val uuid =…
Andrzej Zabost
  • 1,387
  • 11
  • 26
1
vote
0 answers

Why am I getting `java.lang.AssertionError: Built-in class kotlin.Any is not found` when using `copy` in TeamCity DSL?

Background I'm trying to create some Teamcity configuration using Kotlin. I'm using a Maven in Intellij when testing the generation of the Teamcity, although I get the same result by using the command line. Problem A minimum example:…
MrSpaceman
  • 404
  • 6
  • 19
1
vote
1 answer

Deserialize map of field names and values to data class based on KClass (Reflection)

I'd like to construct an object from an instance of KClass and a Map where I map fieldnames to values. An example would be: val testVal = mapOf("title" to "Foo", "description" to "bar") data class Article(val title: String, val…
godzsa
  • 2,105
  • 4
  • 34
  • 56
1
vote
1 answer

Distinguish overloaded methods in Kotlin reflection

I have two methods: class Example { fun method(name: String): String {} fun method(name: String, length: Int): String {} } And at some point in my code I need to use the second version of method via reflection, like so: val func =…
wouldnotliketo
  • 153
  • 1
  • 10
1
vote
1 answer

Kotlin | Find param of a function which returns a lambda using the lambda

Say I have a function haveFun which takes in a Method (from java.lang.reflect package) as a param and returns a lambda as below typealias AnyFun = (o: Any?) -> Any? fun haveFun(method: Method): AnyFun { return { o -> method.invoke(o) } } data…
Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
1
vote
2 answers

Set property by string in Kotlin using reflection?

I have an object: class User { var id: String? = null var name: String? = null } and list of pairs: val fieldsToChange = listOf>(Pair("name", "foo"), Pair("id", "bar")) I would like to iterate trough list of pairs and set…
pixel
  • 24,905
  • 36
  • 149
  • 251
1
vote
1 answer

Kotlin - Performant and scalable way to get class instance property value by id or by string

I would like to make the properties of a class and it's child classes available at runtime for read and write via by integer id or by the name of the property with performance as close to that of a regular compiled read or write as feasible. There…
Dean
  • 73
  • 6
1
vote
1 answer

Compare values of common properties of objects in kotlin

I'm trying to write a method that will compare the 'common properties' (sharing the same name/type) of two objects in Kotlin. Its intended to be used in tests, for comparing 'expected' with 'actual' objects that aren't of the same type. This is a…
Morgan
  • 303
  • 2
  • 15
1
vote
2 answers

Reflecting the name of 'this' in a Kotlin extension function

Is there a way to get the name of 'this' in an extension function? fun Boolean?.persist() { if (this == null) return // Do Nothing val nameOfVariable:String = //get the name of the variable? // Persist variable …
Crocodile
  • 5,724
  • 11
  • 41
  • 67
1
vote
3 answers

Kotlin Reflection: How to know if a Kotlin class is marked with "internal" visibility modifier

I'm autogenerating code with KotlinPoet and Auto Service. I didn't find any way to know if an annotated class has "internal" modifier so I can create another class with same modifier. For example: @MyAnnotation internal class Car So I thought using…
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41