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())
}Janar
09/10/2018, 11:29 AMString.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)
}Janar
09/10/2018, 5:25 PMprivate 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.diesieben07
09/10/2018, 5:27 PMdiesieben07
09/10/2018, 5:28 PMTypeCastException is remnants of the this as java.lang.String in String.toByteArray.Janar
09/10/2018, 5:29 PMJanar
09/10/2018, 5:58 PMdmitry.petrov
09/10/2018, 8:30 PMJanar
09/11/2018, 6:53 PMJanar
09/11/2018, 6:54 PM