Ayfri
12/07/2021, 9:22 AMKMutableProperty1<*, *>
for the members of my class ?
I only found memberProperties
which returns a list of KProperty<*, *>
and not castable so what should I use ?v79
12/15/2021, 9:23 PMApple
extends class Fruit
, how can I create a map like mapOf<String,KClass<Fruit>>("apple" to Apple)
?Ivan Đorđević
01/26/2022, 2:30 PMKClass
to KType
on Kotlin/Native? I saw that for JVM there's createType
To add to my question, is there a way to obtain a KType
from ObjCClass
directly instead?aidanvii
02/04/2022, 4:27 PMjames
02/08/2022, 1:52 PMDariusz Kuc
02/16/2022, 4:27 AMclass Foo(val bar: () -> Bar)
// or
class Foo(val bar: suspend () -> Bar)
how to check whether bar
is actually a lambda.
I was hoping that simple KProperty.returnType is Function<*>
would do the trick, sadly that returns false...rakeeb
02/22/2022, 8:04 PMIncomplete hierarchy for class SubSealedClass, unresolved classes [<package-to>.ParentSealedClass]
while deserializing the sealed class. am i missing proguard rules for kotlin reflection?jean
03/03/2022, 1:30 PMfun test(json: JsonObject) {
val type = String::class
json.decode<type>() //error : Unresolved reference: type
}
private inline fun <reified T> JsonObject.decode(): T = Json.decodeFromJsonElement(this)
Joe
03/09/2022, 4:26 PMClass
instance in kotlin that is equal to void.class
in java? Unit::class.java
doesn't do it:
>>> System.out::class.java.methods.first {it.name == "println"}.returnType == Unit::class.java
res1: kotlin.Boolean = false
(I'm using bytebuddy to dynamically generate a method and need it to be declared void
to be used properly by some jersey code)jean
03/10/2022, 1:39 PMenum class DeviceAttributeType {
...
@SerializedName("waterOverflowIndication")
WATER_OVERFLOW_INDICATION,
@SerializedName("windowCoverLiftPercentage")
WINDOW_COVER_LIFT_PERCENTAGE;
...
override fun toString() = DeviceAttributeType::class.java
.getField(name)
.getAnnotation(SerializedName::class.java)
?.value ?: super.toString()
}
lazt omen
05/19/2022, 1:22 AMdata class Business(
val id: Int,
val name: String
)
val columns = listOf("id", "name")
val values = listOf(1, "Hello")
Is it possible to instantiate this class mapping dynamically my columns to contructor parameters?maxmello
08/10/2022, 1:38 PMpublic inline constructor(size: Int, init: (Int) -> T)
via reflection, but I don’t know how that second parameter is supposed to work.jw
09/07/2022, 5:27 PMMR3Y
10/11/2022, 9:38 PMKClass
of lambda and for a KClass
of anonymous class? In the following example, I can't get an instance through createInstance()
on 2 or 3:
fun interface A<Input, Output> {
fun action(value: Input): Output
}
// 1
class B : A<Float, Int> {
override fun action(value: Float): Int {
return value.toInt()
}
}
println(B::class.createInstance()) // ok, no problems here
// 2
class BAnonymous = object : A<Float, Int> {
override fun action(value: Float): Int {
return value.toInt()
}
}
println(BAnonymous::class.createInstance()) // java.lang.IllegalArgumentException: Class should have a single no-arg constructor
// 3
val BLambda = A<Float, Int> { it.toInt() }
println(BLambda::class.createInstance()) // kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class com.example.MainKt$$Lambda$15/0x0000000800ba5440
I need to call action()
on each one of those, and either using
kclass.declaredFunctions.single().call(instance, /*Float number, e.g 2.3f*/)
requires an instance for the first parameter of call
or even creating an instance with createInstance()
and calling action
on it directly crashes with the aforementioned errorsAdam Firen
11/04/2022, 5:19 PMvalue classes
as their parameters.
Example:
value class Foo(t: String)
class Bar(foo: Foo)
I have looked into the issue a little bit and it looks like the root cause is that in java reflection api Bar
class constructor is seen as
constructor(foo: String)
Hence my question: is there any way to figure out in java reflection api whether a parameter is of an value class
?
If that's not possible then the only way I can see in jackson to implement the handling is to use kotlin reflection api on each class (it returns the correct value class
type) and then do deserialization based on that. I think it might make a big hit on performance though.Alan B
12/08/2022, 9:31 PM/*
I'm working on a function to copy values from one instance to another.
At the moment I don't care about nullability, or type mismatches for field values.
Two data classes. Copy Public's matching properties from Private into Public.
*/
data class Private(val a: String, val b: String)
data class Public(val a: String)
// This next function works "fine" but it's obviously typed to the receiver and has zero reuse.
// Note: I have no idea what or why this receiver is what it is with the this@toPublic
fun Private.toPublic(): Public {
val constructor = Public::class.primaryConstructor ?: error("No primary constructor found")
val propertiesByName = Private::class.memberProperties.associateBy { it.name }
return constructor.callBy(constructor.parameters.associateWith { parameter ->
when (parameter.name) {
// Eventually plan on adding a map to the function
// so properties can be mapped to unmatched names, or coerced to other types
// Note: I have no idea what or why this receiver is what it is with the this@toPublic
else -> propertiesByName[parameter.name]?.get(this@toPublic) // <-- so confused with the receiver
}
})
}
val privateOne = Private(a = "A", b = "B")
val publicOne = privateOne.toPublic()
// publicOne now has its 'a' value copied from privateOne
/*
Now imagine that function above but make it a reusable. This is now more pseudocode as it will not compile,
and it's just wrong because I'm dumb in this area. What is the receiver to be in such a case on that else line?
*/
inline fun <reified R> toPublic(input: Any, output: R & Any): R {
val constructor = output::class.primaryConstructor ?: error("No primary constructor found?")
val propertiesByName = input::class.memberProperties.associateBy { it.name }
val result: R = constructor.callBy(constructor.parameters.associateWith { parameter ->
when (parameter.name) {
// I have absolutely no clue as to what the receiver is here
else -> propertiesByName[parameter.name]?.get(input) // <-- so confused with the receiver
}
})
return result
}
// what I would like to do
fun Private.toPublic2() = toPublic(this, Public::class)
/*
Again, my confusion is on the receivers.
Thanks for your time.
*/
Ilya Nothen
12/13/2022, 6:11 PMv79
01/18/2023, 7:55 PMreflect()
on a lambda specified using the ::methodName
syntax, and that it will return null? I'm experimenting with a library (https://github.com/moia-oss/lambda-kotlin-request-router) and this code passes:
POST("/something") { req: Request<Unit> -> ResponseEntity.ok(Unit) }
But this code fails, and indeed the library seems to suggest that it will always fail because of a limitation in kotlin reflection on lambdas:
val dummy = object {
fun handler(r: Request<Unit>) = ResponseEntity.ok(Unit)
}
GET("/some", dummy::handler)
The library uses kotlin reflection while processing the GET() function:
al requestType = handler.reflect()?.parameters?.first()?.type?.arguments?.first()?.type
?: throw IllegalArgumentException("reflection failed, try using a real lambda instead of function references (Kotlin 1.6 bug?)")
Clearly the library has chosen not to support the ::method syntax, despite using it in its documentation and samples. Any suggestions? Using Kotlin 1.7.22enighma
04/13/2023, 6:50 PMKProperty1
is an enum?
I thought something like this out to work:
prop.returnType::class.isSubclassOf(Enum::class)
enighma
04/18/2023, 5:10 PM@JvmField
annotation, so maybe that's what makes the above not work?Emil Kantis
04/27/2023, 10:21 PMKCallable
with default parameters, without explicitly supplying those parameters?dewildte
08/25/2023, 8:02 PM::functionReference.name
?
More details in the 🧵Settingdust
09/10/2023, 3:14 AMkotlin1910.kotlin1910_builtins
.
Then, I added custom transformers for avoiding shadow
relocate the .kotlin_builtins
constant in kotlin reflect.
And relocate the kotlin/kotlin.kotlin_builtins
to kotlin1910/kotlin1910.kotlin_builtins
But the reflect can't find the builtin class AssertionError: Built-in class kotlin1910.Any is not found
Zoltan Demant
10/10/2023, 6:09 AMinterface Dispatcher<T : Request<R>, R> {
val type: KClass<T>
suspend fun dispatch(
request: T,
): R
}
nikolaymetchev
11/23/2023, 4:29 PMv79
11/23/2023, 8:43 PM(kType.classifier as KClass<*>).constructors
and that requires kotlin-reflect. Any alternatives?martmists
12/24/2023, 4:13 PMSean Proctor
02/26/2024, 11:49 PMColton Idle
03/09/2024, 7:01 AMval field = CryptoFormat::class.java.getDeclaredField("NON_RAW_PREFIX_SIZE")
field.isAccessible = true
val modifers = field.javaClass.getDeclaredField("modifiers")
modifers.isAccessible = true
modifers.setInt(field, field.modifiers and java.lang.reflect.Modifier.FINAL.inv())
field.setInt(null, 2000)
val newValue = CryptoFormat.NON_RAW_PREFIX_SIZE
println("NON_RAW_PREFIX_SIZE is now: $newValue")
Error: java.lang.NoSuchFieldException: modifiers
Emil Kantis
03/15/2024, 9:19 AMString
? e.g.
fun add(a: Int, b: Int) {
println("${a::name} is $a") // a is 5
}