Questions tagged [kotlin-reflect]
121 questions
0
votes
1 answer
Get all classes from classpath which inherits a class X by super class of X
Let's say I have a sealed class A with B and C inheriting it.
sealed class A
open class B(): A()
open class C(): A()
Now, I have few classes in separate files in say classpath: com.dummy with inherits class B or C
class D : B(){}
class E :…

timekeeper
- 698
- 15
- 37
0
votes
1 answer
DelegatedProperty reflection doesn't work on release build
During runtime I need to access properties in the delegated property's delegate instance.
When I compile the following code in debug, it works just fine:
class Potato {
val somePropGoesHere: Int by PotatoDeletgate("This is the key", 0)
fun…

MichaelThePotato
- 1,523
- 2
- 10
- 19
0
votes
2 answers
How to find is a lateinit var has been initialized via Reflection?
I want to find out via reflection if lateinit property of an object has been initialized. How do I got about doing that?
Getting the property and checking non-null results in UninitializedPropertyAccessException
fun Any.isAnyEntityInitialized ()…

dawood zeba
- 3
- 1
0
votes
1 answer
Kotlin: javaSetter method is not compiling
I retrieved a property of my class using:
val prop = businessObject::class.memberProperties.first()
I can do this:
prop.javaGetter
But this method does not compile:
prop.javaSetter
Even though the method exists and is not deprecated

Mukhamedali Zhadigerov
- 811
- 1
- 15
- 32
0
votes
1 answer
How to get java class from object in kotlin?
I am trying mockito to mock a getValue function that consumes a java class as parameter.
To simplify, i did the following test:
@Test
fun test1() {
val map = HashMap()
val v:Long = 1L
map["K"]= v
…

MiguelSlv
- 14,067
- 15
- 102
- 169
0
votes
2 answers
How do I reflect a private static nested subclass?
First, I'm looking for an answer in Kotlin, but I'm interacting with a Java library.
I need to get an instance from a private static nested class, derived from an instance of the surrounding superclass.
Given you have these (simplified) nested Java…

Karlsson
- 203
- 3
- 13
0
votes
0 answers
Better way of converting JSON Array to List given a KClass via Gson
I'm building a multi-platform JSON library as a prototype to get a feel for how to structure things.
Currently i have this extension function to convert from a JSON Array to a List of Objects.
inline fun String.fromJsonList() :…

Jan Vladimir Mostert
- 12,380
- 15
- 80
- 137
0
votes
1 answer
Is it possible to find methods during runtime passing a variable or a string name in Kotlin like in groovy
For example there is the possibility in groovy to execute a method using following code:
@Test
void shouldAccessMethodDynamically() {
DynamicClass dynamic = new DynamicClass()
String name = 'dynamicMethod'
assert…

lennykey
- 1,195
- 2
- 12
- 25
0
votes
2 answers
How to set val property with Kotlin reflection?
I would like to return a copy of a class with a different value assigned to a val property.
data class Person(val name: String, val age: Int)
fun main() {
val person = Person("Morné", 25)
val property =…

Morgoth
- 4,935
- 8
- 40
- 66
0
votes
1 answer
Unable to load class 'kotlin.reflect.KDeclarationContainer'.android studio 3.1.4
i am new in kotlin and when install kotlin in android step by step
in building project i got this error
search a lot about how fix this problem but didn't work any solution like invalidate cache/reset
please help me

Sootood Ebrahimi
- 11
- 5
0
votes
0 answers
How to get Kotlin Collection<*> info with reflection?
Is there a way to know information of collection?
fun foo(clazz: Class<*>) {
// 1. How to know clazz is collection?
// 2. How to know T of Collection is String?
}
fun myFunc() {
val mySet: HashSet = HashSet()
…

Kyoung-june Yi
- 503
- 1
- 3
- 16
0
votes
1 answer
How to collect members via reflection to pass as argument later
So, I'm working on a concept that involvs writing automatically to a ByteBuffer the content of a class extending Bufferizable
abstract class Bufferizable {
abstract val fieldOrder: Array
open fun size(): Int = TODO()
var…

elect
- 6,765
- 10
- 53
- 119
0
votes
1 answer
Kotlin static constant field returning modified String value
I'm trying to reach a constant String value of the Class name. But I really don't understand why I get a modified String value.
Here is the code that I'm working on:
class TestClass {
companion object {
@JvmField
val…

Orcun Sevsay
- 1,310
- 1
- 14
- 44
0
votes
1 answer
When does Kotlin introspection become available in the lifetime of a Spring Boot Application?
I ran into a surprising bug. I am trying to make an application that would access mongodb using the repository pattern. In order to reduce code duplication I wanted to make a common base class for all repositories. The repository of each root…

Martin Drozdik
- 12,742
- 22
- 81
- 146
0
votes
0 answers
KotlinReflectionInternalError Data Class
I received the following error when attempting to use a data class as a result type in a Retrofit service.
11-15 11:35:38.345 14693-14693/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 14693
…

Bryan
- 14,756
- 10
- 70
- 125