com.zsqw123.catreflect.impl.CatValueImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cat-reflect Show documentation
Show all versions of cat-reflect Show documentation
Greatly simplifies the complexities of Kotlin's reflection of Java.
package com.zsqw123.catreflect.impl
import com.zsqw123.catreflect.CatClass
import com.zsqw123.catreflect.CatValue
import java.lang.reflect.Field
/**
* Author zsqw123
* Create by zsqw123
* Date 2021/12/23 4:14 下午
*/
class CatValueImpl(
clazz: CatClass, valueName: String, safe: Boolean = false
) : CatValue(clazz, valueName, safe) {
private val field: Field by lazy {
val clz = clazz.getClazz()
return@lazy try {
val f = clz.getField(valueName)
f.isAccessible = true
f
} catch (_: NoSuchFieldException) {
val f = clz.getDeclaredField(valueName)
f.isAccessible = true
f
}
}
@Suppress("UNCHECKED_CAST")
override fun get(): T {
return field.get(clazz.instance()) as T
}
@Suppress("UNCHECKED_CAST")
override fun set(value: T) {
field.set(clazz.instance(), value)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy