Disable not null checks in Kotlin

谁说胖子不能爱 提交于 2019-12-11 02:33:32

问题


class User(val name: String)

I know that in constructor will be added this check

Intrinsics.checkParameterIsNotNull(name)

To make sure that name do not store null.

Is there a way to instrument to not generate such checks? Maybe an annotation?

We need this in Spring, when map json string to a class. The validation is done by @NotNull and @Validfrom javax

class User(@field:NotNull @field:Email val name: String)

so, checks are performed immediately after object creation, but since exceptions are thrown from constructor this is not possible.


回答1:


You can see how to do it in this post, using either a compiler flag:

-Xno-param-assertions

Or Proguard:

-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
 static void checkParameterIsNotNull(java.lang.Object, java.lang.String); 
}



回答2:


With Proguard, you can add this rule to get ride of all null checks

-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
    static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}


来源:https://stackoverflow.com/questions/49360740/disable-not-null-checks-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!