所以我在几个 classes中有数百个字段,我想在它们上面编写一些方法,它们会自动打印每个字段及其相应的值 目前我有这个: inner class Version(val profile: Profile) { @JvmField val MINOR_VERSION = glGe
          目前我有这个:
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) 
 任何的想法?
您也可以使用Kotlin属性和Kotlin反射类,而不是使用Java字段和Java反射代码:class Reflector {
    val Foo = 1;
    fun printFields() {
        this::class.memberProperties.forEach {
            if (it.visibility == KVisibility.PUBLIC) {
                println(it.name)
                println(it.getter.call(this))
            }
        }
    }
}
        
             