Questions tagged [kotlin-reflect]

1

votes
0

answer
423

Views

Getting unresolved reference error in Kotlin Android Library Project

I am getting unresolved reference error in Kotlin Android Library project. 1.) standard_kotlin_lib.jar --> contains: Kotlin classes, META-INF/standard_kotlin_lib.kotlin_module 2.) A.aar --> compiles only standard_kotlin_lib.jar file. After compilation, inside generated A.aar file’s classes.jar: ME...
user3066038
1

votes
0

answer
248

Views

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 -> annotation is Property} ?.let { property -> (property a...
Kyoung-june Yi
1

votes
0

answer
66

Views

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 annotation has unobfuscated values and therefore kotlin-r...
rosencreuz
1

votes
1

answer
144

Views

Kotlin check if function requires instance parameter

If a KCallable is created using Any()::toString then when it's later referenced you don't need to pass that Any instance (when using call). Though if it's created using Any::toString (not an instance of Any) it's required to pass an Any instance. So my question is how can I easily find out whether...
Mibac
1

votes
1

answer
173

Views

Proguard generating notes for all INSTANCE fields with kotlin-reflect

I've added the kotlin-reflect library to my Android project, and now Proguard is generating hundreds of lines of notes, listing all classes that have a field named INSTANCE, which of course includes every single Kotlin object. Here's just a short part of the Proguard output: Note: kotlin.reflect.jvm...
zsmb13
1

votes
1

answer
193

Views

Check if class is a valid value for KParameter

Given the following example code fun function(text: CharSequence) { println(text) } val textParam = ::function.parameters[0] val stringClass = String::class How can I check if textParam accepts stringClass as a parameter?
Mibac
2

votes
2

answer
43

Views

KClass::memberExtensionFunctions always be empty

Code import kotlin.reflect.full.* class FooBar(val bar: String) fun FooBar.baz(): Unit {println(this.bar)} fun main(args: Array) { FooBar::class.declaredMemberExtensionFunctions.forEach { println(it) } FooBar::class.memberExtensionFunctions.forEach { println(it) } } Output is empty
jilen
5

votes
1

answer
255

Views

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 } } As I could learn from the docs, this will return the exact type of the obj...
devrocca
5

votes
1

answer
562

Views

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 one device (Nexus 5, Android 4.4.3). The logs are as...
Marcin Bak
13

votes
1

answer
584

Views

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 kotlin.reflect.jvm.internal.KClassImpl$kotlin.reflect.jvm.inter...
gaara87
4

votes
2

answer
477

Views

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 java.lang.IllegalArgumentException: Parameter specified as non-null is...
rosencreuz
3

votes
1

answer
37

Views

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 + ' = ' + value.name) } data class Person(val age: Int, val name: String, val surname: String?) fun main...
Morgoth
1

votes
1

answer
74

Views

obtain class from nested type parameters in kotlin

I have a val built like this val qs = hashMapOf() How can I obtain the class of ProfileModel.PersonalInfo from this variable? In other words what expression(involving qs of course) should replace Any so that this test passes. @Test fun obtaionResultTypeFromQuestionList(){ val resultType = Any() asse...
saiedmomen
6

votes
1

answer
77

Views

(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)) // Error println(lengthNL1.get()) println(lengthNL2.get(s)) println(lengthNL1()) println(lengthNL2(s)) } val...
LiTTle
14

votes
2

answer
2.7k

Views

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: Warning:kotlin.reflect.jvm.internal.impl.descriptors.C...
Bryan
1

votes
1

answer
159

Views

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 = properties.joinToString(',') { when (it.returnType) { String?::class.crea...
Morgoth
1

votes
1

answer
39

Views

Generic function works, but generic class doesn't?

I would like a class that is a generic for KProperty1, I can do this for a function, but not a class: import kotlin.reflect.KProperty1 data class Dog(val name: String, val age: Int) fun reflectionHelper(input: P) = input.name fun getReflectionHelper(clazz: P) = ReflectionHelper() class ReflectionH...
Morgoth
2

votes
1

answer
602

Views

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 the code above how can I convert it to a function with instance built-in so when calling I...
Mibac
4

votes
2

answer
406

Views

Declare a function generic type parameter in a way that allows both for nullable types and using that type parameter to declare `KClass<T>`

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 (that should all do essentially the same work), 1 of them doesn't co...
Burg
2

votes
0

answer
199

Views

Kotlin Delegate with MultiDex on Android below 21

I have the following Delegate... fun integerPref(initialValue: Int) = object : ObservableProperty(initialValue) { override fun afterChange(property: KProperty, oldValue: Int, newValue: Int) { getSharedPreference(INTEGER_PREF, EasyInjection.mode) .edit() .putInt(property.toString(), newValue) .apply(...
Jongz Puangput
1

votes
2

answer
431

Views

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 var value = 1 val a: KMutableProperty0 = this::value
Stepango
1

votes
1

answer
897

Views

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 convoluted: create an anonymous subclass of a generic class,...
Salomon BRYS
1

votes
1

answer
1.6k

Views

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 narrows the search to only factories that return th...
user1582024
6

votes
1

answer
449

Views

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
2

votes
1

answer
801

Views

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 value: val method = ApiDefinition::getObjects.javaMetho...
Ollie C
4

votes
0

answer
474

Views

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 build: Optimizing... Unexpected error while performing partial eva...
David Pisoni
2

votes
1

answer
355

Views

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 the parent KDeclarationContainer (a KPackage?)
tango24
2

votes
1

answer
259

Views

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

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: Ar...
OlivierWah