135 questions
0
votes
0
answers
39
views
In a KMP project, cannot import kotlin.reflect.full
gradle
-> libs.version.toml
[versions]
...
kotlin = "2.1.0"
[libraries]
...
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }...
3
votes
0
answers
89
views
Dynamically added annotation is not visible to Kotlin reflection
I am working with a Java agent that is adding an annotation dynamically to a class file. When this class file is a Kotlin class, and if it is investigated by Kotlin reflection, the annotation does not ...
0
votes
0
answers
54
views
How do I call a KCallable in Kotlin Android passing the receiver and arguments?
I have tried the answer in this post but I still get the error in my Kotlin Android project.
Here is how my code looks like:
interface BaseServiceScope {
suspend fun resolveState2(intent: Intent?)...
2
votes
1
answer
171
views
How to set value of kotlin object-class property using kotlin.reflect API?
How can I set property value for classes with object keyword using kotlin.reflect API? I was hoping that this is analogous to setting values in regular classes, however, this is not the case. When ...
0
votes
0
answers
32
views
how do I get annotation instances that are declared by AnnotationTarget.EXPRESSION?
I am trying to read the annotations of a value that I pass to a function. I have an annotation defined as:
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class ...
0
votes
1
answer
94
views
Kotlin reflection - retrieve property if it exists
I'd like to retrieve a property if the referenced object has one. (I have a more complex use case, but this demonstrates the problem nicely). It appears, the only way to do this is by breaking ...
0
votes
1
answer
119
views
How to access a Kotlin private top-level property in the unnamed (main) file?
How to use Java or Kotlin reflection to access the value of a private variable in the Main.kt file?
package com.example
private val myName = "abc"
fun main() {
println(myName)
}
0
votes
1
answer
53
views
using kotlin.reflect on the elements of a array not working
I am trying to use kotlin.reflect to get the names of variables in this array all. Then iterate over the array and get the name of each variable a, b, c. But well its not working and im not sure how ...
2
votes
1
answer
325
views
Convert `Type` to a `KType`
Is there a way to get a Kotlin KType from a java.lang.reflect.Type?
Background: I'm working on some code that gets properties/fields from a class and passes them to methods (think JUnit 4 Theories for ...
1
vote
1
answer
271
views
Kotlin - obtaining an instance of both KProperty1 and CallableReference through reflection
I'm working with a third-party library that has a function which expects an argument of type KProperty1<T, V>, and casts it to an instance of CallableReference internally. I need to acquire a ...
0
votes
1
answer
111
views
How to implement a Kotlin Comparator with Reflect and safe cast?
I want to implement a function that compares two objects of the same type based on their Comparable properties, such as:
fun <T:Any> reflectCompare(a: T, b: T) : Int {
a::class
....
1
vote
1
answer
301
views
How to access the receiver of a method reference?
I have a method reference (represented by the kotlin.reflect.KFunction0 interface). What I would like to do is to get the receiver object of the method reference.
For example:
data class MyClass(val ...
0
votes
1
answer
124
views
Is there a way to check if kotlin class property was defined as nullable at runtime?
I have the following simple class structure:
package qwerty
import kotlin.reflect.full.declaredMemberProperties
class UserSet(
var user1: User,
var user2: User? = null,
)
class User(
...
1
vote
0
answers
94
views
How can I access KProperty1<T,V> from java class?
I am trying to call a function in Kotlin file from a java code but unable to do so, I can easily call the function from kotlin using A::a where A is the data class and 'a' is its property
I have a ...
2
votes
1
answer
297
views
Kotlin: implicit conversion from Int to nullable Double fails in reflection
I was trying to convert a Map<String, Any> to data class instance:
data class Person(
val name: String,
val age: Int,
val weight: Double?,
)
fun test() {
val map = mapOf(
...
0
votes
1
answer
343
views
How to get annotations of a variable in external method using Kotlin?
I'm starting with Kotlin reflection. I need to write a method in Kotlin that would take a variable (in this case lets say of type String) and return all annotations (if any) that are applied to that ...
0
votes
1
answer
70
views
Kotlin - return all properties that implement an interface and are annotated
I have a simple kotlin 1.7.10 program - I have an interface called Rule and property annotation called NextRule + 2 implementations like
import kotlin.reflect.KClass
interface Rule {
fun process(...
0
votes
1
answer
349
views
How to get the class through reflection and then pass in the generic?
There is such a function
public inline fun <reified W : ListenableWorker> PeriodicWorkRequestBuilder(
repeatInterval: Duration
): PeriodicWorkRequest.Builder {
return PeriodicWorkRequest....
2
votes
1
answer
429
views
KotlinReflectionNotSupportedError in Compose Preview
I have defined the Kotlin reflection dependency in my Gradle file as follows:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
Within the App, reflection works just fine. ...
0
votes
1
answer
911
views
How to get KClass<List<String>> type from List<String> in kotlin
I'm having problem with Kotlin poet PropertySpec object. Because i can't provide List<String> to the PropertySpec builder.
Help please.
PropertySpec.builder("services", List<String&...
1
vote
1
answer
497
views
Get the annotation variable and its type of a data class
I have a data class in kotlin , which is also a Room entity.
Eg :
data class Pack(
@ColumnInfo(name = "name")
@SerializedName("name")
var name:String,
@...
6
votes
2
answers
496
views
method toString throws KotlinReflectionInternalError
Why calling method toString() on function reference results in an error KotlinReflectionInternalError
fun main() {
fun foo(){}
::foo.toString()
}
output:
Exception in thread "main" ...
2
votes
1
answer
920
views
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 ...
0
votes
3
answers
850
views
Simple reflection in Kotlin doesn't work in existing Java project
I have simple Kotlin code in an existing Java project
class A(val p: Int)
fun main() {
println("Hello World")
println(A::javaClass)
println(A::p)
}
However, this throws an ...
1
vote
1
answer
334
views
Getting a Method from a method reference
I have a method inside a class:
class Foo {
fun bar()
}
And I want to pass the method bar to a third-party library method that takes a java.lang.reflect.Method argument:
fun baz(Method method)
...
3
votes
1
answer
255
views
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, ...
2
votes
1
answer
203
views
Does Dagger support multibinding with KClass<*> type?
I have a KeyMap like this:
@Target(AnnotationTarget.FUNCTION)
@MapKey
annotation class JsonSerializerKey(val value: KClass<*>)
and want to provide them as:
// #1 doesn't work
Map<KClass<*&...
0
votes
1
answer
933
views
Kotlin Annotation not present at Runtime
I'm trying to use Kotlin Reflection to generate JFrames from Classes with their members.
I have created a couple Annotations, some of them are present at runtime and some are not.
Component Annotation:...
0
votes
0
answers
989
views
Unresolved reference: java, unresolved reference lazy, unresolved reference with
Last time I did this the project ran just fine but I am getting these errors now, I tried installing kotlin-reflect but still it didn't work.
It doesn't recognise lazy and with either and I simply don'...
0
votes
2
answers
2k
views
Get Kotlin class from string a call a method in it
I have 2 simple classes in kotlin
package com.sample.repo
class SampleClassA() {
fun test(): String {
return "Do things A way"
}
}
package com.sample.repo
class SampleClassB(...
1
vote
1
answer
975
views
How to get only declared members (not inherited) with Kotlin Reflection?
Is there any way to get only the declared members of a class (not inherited) with Kotlin Reflection?
Something equivalent to getDeclaredMethods(), or ...Fields(), in Java, but for members and JVM ...
0
votes
1
answer
860
views
Find all primitive memberProperties Kotlin Android
I want to find memberProperties that are not other classes I declared.
For example
data class X(val other: String)
data class Y(val str: String, val y: X?)
I want to filter() my Y::class....
2
votes
1
answer
1k
views
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(...
4
votes
0
answers
908
views
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 ...
3
votes
0
answers
1k
views
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&...
1
vote
1
answer
775
views
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 ...
1
vote
1
answer
2k
views
Kotlin - Merge two data class
Data class
data class A(
var data: List<Data>
) {
data class Data(
var key: String,
var count: Long = 0,
var sub: List<Data>? = null
)
}
A class data ...
3
votes
0
answers
248
views
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, ...
5
votes
1
answer
5k
views
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, ...
1
vote
1
answer
1k
views
Why can I not access `::class.companionObject`?
I am trying to access the companion object of an unknown class with a known interface, given an instance of the class.
Code below:
class AccessTest() {
companion object {
val prop = 5
}...
1
vote
1
answer
1k
views
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 ...
0
votes
1
answer
204
views
How do I see if a class has an init block using reflection?
If I have a Kotlin class like the following:
data class Foo(
val count: AtomicInteger
) {
init {
count.incrementAndGet()
}
}
How do I determine that this class has an init block ...
0
votes
0
answers
165
views
Is it possible to copy a KClass and change it's properties?
What I'd like to do is use Kotlin reflection to make a copy of a class and set all of it's properties optional/nullable.
E.g.
fun looseClass(schema: KClass<*>): KClass<*> {
...
0
votes
2
answers
831
views
Access Implementation's property on variable of type Interface
I'm trying to access the delegate of the property (id) of a class (FooImpl). The problem is, this class implements an interface (Foo), and the property in question overrides a property of this ...
1
vote
2
answers
954
views
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: ...
0
votes
1
answer
1k
views
Get private method with reflection in order to pass it to a higher order function in Kotlin
I'm having a hard time trying to get a private method in Kotlin using reflection in order to pass it as a parameter to a higher order function, here is what I got and what I need to do:
The function ...
1
vote
0
answers
2k
views
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 ...
2
votes
2
answers
3k
views
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<String>) {
val lst = listOf(1,2,3,45,5,6,7,8)
val mapped = lst.map {Data(::isEven,it)}...
0
votes
1
answer
423
views
Kotlin Reflection - Returns an instance with set properties values based on it's property name
I'm trying to create a function that returns any data class object setting it's property values with its property names (if all strings) without changing it's default values
I have an example on how ...
0
votes
1
answer
2k
views
Check annotation on a variable in kotlin data class
I have to check if a particular variable inside a kotlin data class has an annotation present or not.
Annotation class
annotation class Test(
val identifier: String
)
Data class
data class Player(...