kotlin反射访问java字段访问器(getter和setter)

9rbhqvlz  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(282)

我有以下java类 _17_A.java ```
package _z_additional;

public class _17_A {
private int x = 0;

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

}

我试图获取类中java字段的访问器的示例 `_17_A` 使用以下kotlin代码(和kotlin反射) `_17_ObtainingJavaAccessors_Test.kt` ```
package _z_additional

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaMethod

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
object _17_ObtainingJavaAccessors_Test {

    @Test
    fun demonstrate() {
        val cls: KClass<*> = _17_A::class
        cls.declaredMemberProperties.forEach {
            val getter1: Method? = it.javaGetter
            val getter2: Method? = it.getter.javaMethod
            println("getter1 = ${getter1}")
            println("getter2 = ${getter2}")
        }
    }

}

输出

getter1 = null
getter2 = null

问题是我越来越 null s

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题