Kotlin, how to retrieve the field value by reflective

So I have hundreds of fields in several classes, I want to write some methods on them, they will automatically print each field and its corresponding value

Currently I have this:

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)) }
}
}

But this is What I get:

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(UnsafeFieldAccessorI.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAc cessorImpl.java:58)
at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.getInt(UnsafeQualifiedIntegerFieldAccessorImpl.java:58)

Any ideas?

You can also use Kotlin attributes and Kotlin reflection classes instead of Java fields and Java reflection codes :

class Reflector {
val Foo = 1;

fun printFields() {
this:: class.memberProperties.forEach {
if (it.visibility == KVisibility.PUBLIC) {
println(it.name)
println(it.getter.call(this))
}
}
}
}

So I have hundreds of fields in several classes, and I want to top them Write some methods, they will automatically print each field and its corresponding value

Currently I have this:

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)) }
}
}

But this is what I got:

false
Exce ption 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)

Any ideas?

You can also use Kotlin attributes and Kotlin reflection classes instead of Java fields and Java reflection codes:

< /p>

class Reflector {
val Foo = 1;

fun printFields() {
this::class.memberProperties.forEach {
if ( it.visibility == KVisibility.PUBLIC) {
println(it.name)
println(it.getter.call(this))
}
}
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2697 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.