kotlin - 使用 kotlin-reflect 设置可以为空的 UShort
问题描述
为什么我不能在 kotlin 中使用反射设置 UShort?我将我的问题提取到单元测试中。
我的测试如下所示:
class Junk {
var DA: UShort? = null
}
class Tests {
@Test
fun testSetShort() {
var uut = Junk()
val value = 100
val expect = 100
val properties: Collection<KProperty<*>> = Junk::class.memberProperties
val property = properties.find { property -> property.name == "DA" }
if (property is KMutableProperty<*>) {
property.setter.call(uut, value.toUShort()) /* FAILS HERE */
}
assertEquals(expect, uut.DA)
System.err.println("ok")
}
}
结果是
argument type mismatch
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at kotlin.reflect.jvm.internal.calls.CallerImpl$Method.callMethod(CallerImpl.kt:97)
at kotlin.reflect.jvm.internal.calls.CallerImpl$Method$Instance.call(CallerImpl.kt:113)
at kotlin.reflect.jvm.internal.calls.InlineClassAwareCaller.call(InlineClassAwareCaller.kt:142)
at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:108)
at Tests.testSetShort(testSetUshort.kt:24)
我尝试过的事情:
- 强制值为UShort 类型?如果可空性是问题(尽管当我尝试对可空字符串 var 做同样的事情时这不是问题)
解决方案
这是内联类的问题。如您所知,内联类仍然是实验性的,并且UShort
是一个内联类,它充当包装器Short
:
public inline class UShort @PublishedApi internal constructor(@PublishedApi internal val data: Short) : Comparable<UShort>
让我们看一下您的代码的字节码。这是您的DA
财产的汇总字节码:
private Lkotlin/UShort; DA
@Lorg/jetbrains/annotations/Nullable;() // invisible
// access flags 0x11
public final getDA-XRpZGF0()Lkotlin/UShort;
@Lorg/jetbrains/annotations/Nullable;() // invisible
...
public final setDA-ffyZV3s(Lkotlin/UShort;)V
// annotable parameter count: 1 (visible)
// annotable parameter count: 1 (invisible)
@Lorg/jetbrains/annotations/Nullable;() // invisible, parameter 0
...
如您所知,内联类应该在编译后被忽略和删除,但是因为您定义DA
为可为空,所以编译后的类型仍然UShort
是Short
.
但是,当您调用Int.toUShort
一个对象时,编译后的代码没有任何符号,UShort
而是转换为Short
(因为它是一个内联类应该如此)。这就是为什么你得到一个argument type mismatch
错误。因为 setter 需要 aUShort
但你给它 a Short
。
这就解释了为什么您的代码使用Short
而不是UShort
.
无论如何,如果你真的需要UShort
在你的代码中使用,你不应该让它可以为空,lateinit var
而是使用 a 并且它工作正常。因为如果它不可为空,则DA
属性的类型将Short
在编译后
var DA: UShort = 0u
//bytecode:
private S DA // S is JVM type for Short
// access flags 0x11
public final getDA-Mh2AYeg()S
...
// access flags 0x11
public final setDA-xj2QHRw(S)V
...
推荐阅读
- python-3.x - 使用 Python 3 配置 CDH 集群
- python-3.x - 从具有相同键的列表中创建元组字典
- python - Django - 在管理页面中更改自定义用户模型时,外键约束失败
- scala - 类型不匹配 - Array[Map[String,Array[Map[String,Long]]]]
- java - JTable 的行过滤器
- android - RxJava1 concatMap 导致 MissingBackpressureException
- c# - 为什么 UWP 连续语音识别停止
- ruby-on-rails - created_at 和 updated_at 如何在测试数据库中填充?
- php - 将数据插入表中的最用户友好的方式
- mysql - 我试图让分配了某些东西的用户能够使用 PHP 和 MYSQL 查看它