Questions tagged [kotlin-reflect]
121 questions
0
votes
1 answer
How to get the class through reflection and then pass in the generic?
There is such a function
public inline fun PeriodicWorkRequestBuilder(
repeatInterval: Duration
): PeriodicWorkRequest.Builder {
return PeriodicWorkRequest.Builder(W::class.java, repeatInterval)
}
requires…

SageJustus
- 631
- 3
- 9
0
votes
1 answer
How to get KClass> type from List in kotlin
I'm having problem with Kotlin poet PropertySpec object. Because i can't provide List to the PropertySpec builder.
Help please.
PropertySpec.builder("services", List::class.asTypeName()).build()
0
votes
3 answers
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 exception
Exception in thread "main"…

user1064504
- 573
- 6
- 16
0
votes
1 answer
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:
@Target(AnnotationTarget.FIELD,…

Marconymous
- 11
- 1
- 1
0
votes
0 answers
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't know what to do anymore.
Lazy and ::class.java…

Antonio Roldan
- 33
- 7
0
votes
1 answer
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.memberProperties such that only str is included.
In other…

zaitsman
- 8,984
- 6
- 47
- 79
0
votes
1 answer
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
}
fun getComp() {
print(this)
…

Will Chen
- 482
- 4
- 12
0
votes
1 answer
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 using reflection?
The init block does not appear…

David Rawson
- 20,912
- 7
- 88
- 124
0
votes
0 answers
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<*> {
val props = schema.memberProperties.map {
…

godzsa
- 2,105
- 4
- 34
- 56
0
votes
2 answers
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 interface. The delegate only exists in the class (not…

Kolja
- 1,197
- 9
- 17
0
votes
1 answer
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 that gets the private method, probably what I…

Alejandro Casanova
- 3,633
- 4
- 31
- 46
0
votes
1 answer
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 it is:
Imagine this data class:
data class…

julioribeiro
- 1,565
- 2
- 14
- 22
0
votes
1 answer
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(
val name: String,
…

matrixguy
- 286
- 1
- 6
- 30
0
votes
2 answers
How can I use getInstance in Kotlin?
I'm unable to use createInstance(). For example, Foo::class.createInstance()
I have a gradle project using Kotlin 1.3.70 kotlin("jvm") version "1.3.70" plugin. and have implementation(kotlin("stdlib-jdk8")) and implementation(kotlin("reflect")) in…
user2211907
0
votes
1 answer
kotlinFunction returns null if method is defined on an interface with a type parameter
A quick demo of a problem:
import kotlin.reflect.jvm.kotlinFunction
interface A {
fun aaa(t: T): String {
return ""
}
}
class B : A
fun main() {
println(B::class.java.methods[0].kotlinFunction) // returns…

dimafeng
- 365
- 4
- 15