kotlin-reflect

Method reference to property setter

余生长醉 提交于 2021-02-04 18:59:26
问题 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 var value = 1 val a: KMutableProperty0<Int> = this::value 回答1: this::value is a property reference. It returns a KMutableProperty . To get the setter you will need the setter field of KMutableProperty . So you will need this: class C { var field: Int

Two Additional types in default constructor in Kotlin?

五迷三道 提交于 2020-04-13 18:00:20
问题 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 class Model( @SerializedName("xyz") val entity: String?, @SerializedName("abc") val id: Long? = null ) val constructors = clazz.declaredConstructors // how I call the constructors My actual question is why I have these 2 fields and what is the logic

Combining/merging data classes in Kotlin

二次信任 提交于 2020-04-11 04:19:09
问题 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: MyDataClass): MyDataClass where this function checks each property on both classes and where they are different uses the left parameter to create a new MyDataClass. Is this possible possible using kotlin-reflect, or some other means? EDIT: more clarity Here

Error when use callBy on a function with default parameters in Kotlin

我与影子孤独终老i 提交于 2020-01-02 08:54:38
问题 I try to call a function with default parameters values without put parameters in Kotlin. For example: class Test { fun callMeWithoutParams(value : Double = 0.5) = value * 0.5 fun callIt(name: String) = this.javaClass.kotlin .members.first { it.name == name } .callBy(emptyMap()) } fun main(args: Array<String>) { println(Test().callIt("callMeWithoutParams")) } I have the exception: Exception in thread "main" java.lang.IllegalArgumentException: No argument provided for a required parameter:

Kotlin's reflection : Unknown type parameter

南笙酒味 提交于 2019-12-24 05:56:47
问题 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 convoluted: create an anonymous subclass of a generic class, then get its super-type first parameter. Here's an example: @Suppress("unused") @PublishedApi internal abstract class TypeReference<T> {} inline fun <reified T> jGeneric() = ((object :

How to obtain a KType in Kotlin?

北慕城南 提交于 2019-12-23 19:06:18
问题 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 narrows the search to only factories that return that type of object (or some sub-type). fun mapToFactory(phrase: Phrase, type: KType = Any::class): Any {...} type needs to accept just about anything, including Int , which from my experience seems

Generic extensions of KProperty1 in Kotlin

青春壹個敷衍的年華 提交于 2019-12-12 11:23:51
问题 I have the following code: import kotlin.reflect.KProperty1 infix fun <T, R> KProperty1<T, R>.eq(value: R) { println(this.name + " = $value") } infix fun <T, R> KProperty1<T, R>.eq(value: KProperty1<T, R>) { println(this.name + " = " + value.name) } data class Person(val age: Int, val name: String, val surname: String?) fun main() { Person::age eq 1 // Valid. First function Person::name eq "Johan" // Valid. First function Person::name eq Person::name // Valid. Second function Person::age eq 1

Error when use callBy on a function with default parameters in Kotlin

牧云@^-^@ 提交于 2019-12-06 06:00:23
I try to call a function with default parameters values without put parameters in Kotlin. For example: class Test { fun callMeWithoutParams(value : Double = 0.5) = value * 0.5 fun callIt(name: String) = this.javaClass.kotlin .members.first { it.name == name } .callBy(emptyMap()) } fun main(args: Array<String>) { println(Test().callIt("callMeWithoutParams")) } I have the exception: Exception in thread "main" java.lang.IllegalArgumentException: No argument provided for a required parameter: instance of fun Test.callMeWithoutParams(kotlin.Double): kotlin.Double at kotlin.reflect.jvm.internal

Reflectively calling function and using default parameters

 ̄綄美尐妖づ 提交于 2019-12-01 18:09:01
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)? You can use the callBy , which respects the default values: ::function.callBy(emptyMap()) // is just function() Things will be messy if you have many parameters without default values: fun foo(a: Int, b: String = "") {} val ref = ::foo val params = ref.parameters ref.callBy(mapOf(params[0] to 1)) // is just foo(1) It will be even more boring if your function is a member function of a non-object type, or it's

Reflectively calling function and using default parameters

萝らか妹 提交于 2019-12-01 17:51:29
问题 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)? 回答1: You can use the callBy, which respects the default values: ::function.callBy(emptyMap()) // is just function() Things will be messy if you have many parameters without default values: fun foo(a: Int, b: String = "") {} val ref = ::foo val params = ref.parameters ref.callBy(mapOf(params[0] to 1)) // is