所以我在几个类中有数百个字段,我想在它们上编写一些方法,它们会自动
println
每个字段及其相应的值
目前我有这个:
inner class Version(val profile: Profile) {
@JvmField val MINOR_VERSION = glGetInteger(GL_MINOR_VERSION)
fun write(file: File? = null) {
//file.printWriter().use { out -> out.pri }
this::class.java.fields.forEach {
println(it.isAccessible)
println(it.getInt(it)) }
}
}
但这就是我得到的:
false
Exception in thread "main" java.lang.IllegalArgumentException: Can not set final int field uno.caps.Caps$Version.MINOR_VERSION to java.lang.reflect.Field
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.getInt(UnsafeQualifiedIntegerFieldAccessorImpl.java:58)
有什么想法吗?
除了使用 Java 字段和 Java 反射代码,您还可以使用 Kotlin 属性和 Kotlin 反射类:
class Reflector {
val Foo = 1;
fun printFields() {
this::class.memberProperties.forEach {
if (it.visibility == KVisibility.PUBLIC) {
println(it.name)
println(it.getter.call(this))
}
}
}
}
看来您正在将
Field
变量 it
作为参数传递 getInt
而参数应该是字段所属的对象 this
:
来自
Field.getInt(Object obj)
的 Javadoc:
也许这就是您想要做的:
class Reflector {
@JvmField val Foo = 1;
fun printFields() {
this.javaClass.fields.forEach {
println(it.isAccessible)
println(it.getInt(this))
}
}
}
fun main(args : Array<String>) {
Reflector().printFields()
}
请注意,您应该首先添加以下依赖项:
dependencies {
implementation(kotlin("reflect"))
// ...
}
此外,要获取静态 Java 类的字段或静态字段,请使用
staticProperties
代替memeberProperties
:
// R.raw is a static class in Android and its fields are also static
for (property in R.raw::class.staticProperties) {
println(property.name)
println(property.get())
}