Janar
09/10/2018, 11:17 AMimport java.security.MessageDigest
fun foo(rp: String) {
return bar(rp)
}
private fun bar(rp: String): ByteArray {
return MessageDigest.getInstance("SHA-256").digest(rp.toByteArray())
}
The generarted code for kotlin uses: Intrinsics.checkParameterIsNotNull
for foo
and generates if (rpId ==null)
in bar
. In the later case it throws a TypeCastException
in case of null and in former an IllegalArgumentException
.
So why does generated code need to handle null checks differently??ribesg
09/10/2018, 11:22 AMJanar
09/10/2018, 11:25 AMimport java.security.MessageDigest
fun foo(rp: String) {
return bar(rp)
}
private fun bar(rp: String): ByteArray {
return MessageDigest.getInstance("SHA-256").digest(rp.toByteArray())
}
String.toByteArray()
? Even then we could have still used the same null check in Intrinsics.checkParameterIsNotNull(rp, "rp")
??diesieben07
09/10/2018, 5:02 PMfoo
is public, and therefor can be called from untrusted code (including Java), who might be passing null. In those cases we want IllegalArgumentException
. However bar
is private, and null
should never end up there. If it for some reason does, you have done an unchecked cast somewhere in your code.Janar
09/10/2018, 5:24 PMfoo
and bar
the null check in bar disappears!!? a primitive byte[] can be null too!
import java.security.MessageDigest
fun foo(rp: ByteArray):ByteArray {
return bar(rp)
}
private fun bar(rp: ByteArray): ByteArray {
return MessageDigest.getInstance("SHA-256").digest(rp)
}
private static final byte[] bar(byte[] rp) {
byte[] var10000 = MessageDigest.getInstance("SHA-256").digest(rp);
Intrinsics.checkExpressionValueIsNotNull(var10000, "MessageDigest.getInstance(\"SHA-256\").digest(rp)");
return var10000;
}
diesieben07
09/10/2018, 5:26 PMdigest
is a Java method, so it's argument is a platform type (ByteArray!
in this case). This type is allowed to accept nulls, so the compiler does not emit the check.TypeCastException
is remnants of the this as java.lang.String
in String.toByteArray
.Janar
09/10/2018, 5:29 PMdmitry.petrov
09/10/2018, 8:30 PMJanar
09/11/2018, 6:53 PM