Ian Stewart
04/01/2020, 11:37 PM(url: kotlin.String, cont: (result: kotlin.String) -> kotlin.Unit) -> kotlin.Unit
…so the information is there, at least internally. Is there any way of getting the ‘url’ and ‘cont’ strings out? I am able to get the types from the arguments() method.
I have read about using .reflect() to get a KFunction, but I cannot find a good example of how to do this. for myktype.classifier.reflect(), I get:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun <R> Function<TypeVariable(R)>.reflect(): KFunction<TypeVariable(R)>? defined in kotlin.reflect.jvm
marcinmoskala
04/06/2020, 3:18 PMRoundEnvironment
. Got TypeElement
but don’t have any idea how to make KClass out of that. Any ideas?jeggy
04/09/2020, 1:44 PMKType
?
I've created a sample of what I'm trying to do: https://pl.kotl.in/zBHj3IlEichristophsturm
04/30/2020, 1:26 PMnikolaymetchev
07/24/2020, 2:57 PMInner
from Outer::class
. I figured out how to do it using java reflection but I would prefer kotlin reflection.
class Outer {
object Inner : Any() {}
}
Daniele B
08/13/2020, 5:27 PMvar myVar : String
get() = functionUsingTheVariableName(::field.name)
I would like to do this, but I get:
Unsupported [References to variables aren't supported yet]
any alternative?Daniele B
08/14/2020, 1:35 AMobject LocalSettings : SettingsClass() {
var myString by StringType("mytext")
}
open class SettingsClass {
val settings = Settings()
inner class StringType(defaultValue : String) {
val default = defaultValue
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return settings.getString(property.name, default)
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
settings.putString(property.name, value)
}
}
}
can you confirmed that when the LocalSettings object is instantiated, the StringType
delegate will be called only when accessing LocalSettings.myString
?droid
08/26/2020, 4:28 PMAndroid 6
devices after enabling R8
with this error:
ReflectKotlinClass.kt line 1
kotlin.reflect.jvm.internal.impl.descriptors.runtime.components.ReflectClassStructure.loadClassAnnotations
Any ideas on this? It is crashing on Android 6 devices only at the moment.jaqxues
10/15/2020, 8:09 PMiamthevoid
11/02/2020, 6:40 AMannotations
for KClass
returns empty array?karandeep singh
11/05/2020, 5:28 PMval test by Delegate("some value")
is it possible to get the parameter some value
via reflectionDario Pellegrini
11/17/2020, 9:20 AMtateisu
12/18/2020, 2:11 AMprop.returnType.isSubtypeOf(ArrayList<String>::class.createType())
this expression raise compile error "Only classes are allowed on the left hand side of a class literal"christophsturm
12/25/2020, 12:12 PMobject S {
val string = "String"
}
fun main() {
println(measureTimeMillis {
StringProvider(S::class).getContext()
})
}
class StringProvider(val kClass: KClass<*>) {
fun getContext(): String {
return kClass.declaredMemberProperties.single { it.name == "string" }.call(kClass.objectInstance) as String
}
}
it takes 420ms on my macTimo Gruen
12/28/2020, 3:28 PMProperty
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER)
@Retention(AnnotationRetention.RUNTIME)
annotation class Property
When using the annotation on a property e.g.:
data class Example(@Property val key: String)
and introspecting the class with a AnnotationProcessor i can find three different elements which are related to that key
property.
The first one is key
, the second one is getKey
and the third (which is currently probably the reason for my confusion) is the getKey$annotations()
one, which is the only element having the annotation from java annotation processor perspective.
I would have assumed that either key
or getKey
would also be annotated on byte-code level. Any guidance on how i can access the element getKey
OR key
using the annotation processor api?
My actual goal is to retrieve the name key
as well as the type String
of the annotated elementjeggy
01/07/2021, 3:56 PMT
and I know T
is a Sealed Class
.holgerbrandl
01/30/2021, 9:06 PMopen class Person(val name: String)
class Manager(name: String, val isCEO:Boolean) : Person(name)
val declaredMembers = Manager::class.declaredMembers
val properties = declaredMembers
.filter { it.parameters.toList().size == 1 }
.filter { it is KProperty }
Usecase: https://github.com/holgerbrandl/krangl/blob/60308cabde760a16fe31782c191b4799da9ba7c9/src/main/kotlin/krangl/Builder.kt#L53 where I'd like to fix it in a way that also super-class attributes are considered.holgerbrandl
02/04/2021, 10:42 PMKFunction1.call()
is marked as red even with the latest org.jetbrains.kotlin:kotlin-reflect:1.4.30 in the classpatth. Still, the code seems to compile fine. Or is it just me? Example
import kotlin.reflect.KFunction1
class Test( val funPointer: KFunction1<String, String>) {
init{
funPointer.call("foo")
}
}
Patrick Jackson
02/11/2021, 11:35 PMnikolaymetchev
02/22/2021, 9:14 AMFatih Yalmanbas
03/26/2021, 8:54 AMchristophsturm
03/26/2021, 11:36 AMTwoClocks
04/16/2021, 6:02 PMif( someProp.returnType == Double::class )
In this case I could create an instance of a Double
and get it's type... but pretend the type is of a class that it's not easy/possible to get an instance of easily.F0X
04/23/2021, 8:52 PMKType
or check whether a value is of the type (or if necessary the classifier)?huehnerlady
05/18/2021, 10:26 AMSourabh Rawat
06/09/2021, 5:20 PMinline fun <reified T> foo(value: T?): T {
println(::value.name) // get the name
}
foo(someValue) -> should output "someValue"
foo(someValue2) -> should output "someValue2"
I tried with https://kotlinlang.org/docs/reflection.html#property-references but to no avail.
Is this even possible?jrod
07/07/2021, 2:34 AMprivate fun getClientMetadata(callback: EventListener?) = ClientMetadata(
Build.VERSION.SDK_INT,
Build.MANUFACTURER,
Build.MODEL,
Build.PRODUCT,
Build.DEVICE,
Build.HARDWARE,
callback != null && overridesMethod(callback::class, "onLogin"),
callback != null && overridesMethod(callback::class, "onSuccess"),
callback != null && overridesMethod(callback::class, "onError"),
callback != null && overridesMethod(callback::class, "onExit"),
callback != null && overridesMethod(callback::class, "onEvent"),
)
private fun getUnixTimestamp(): Long {
return System.currentTimeMillis()
}
private fun overridesMethod(cls: KClass<out EventListener>, methodName: String): Boolean {
return cls.memberFunctions.first { it.name == methodName } in cls.declaredFunctions
}
and i’m getting the following crash:jeggy
08/02/2021, 9:12 PMTwoClocks
08/23/2021, 9:08 PMfunctions
and memberFunctions
and declaredFunctions
and declaredMemberFunctions
? The docs don't point any obvious differences.henrik
10/22/2021, 9:45 AMhenrik
10/22/2021, 9:45 AMchristophsturm
10/22/2021, 9:52 AMThrowable().stackTrace[1].methodName
henrik
10/25/2021, 11:07 AMchristophsturm
10/25/2021, 11:09 AMhenrik
10/25/2021, 11:10 AM