V
Size: a a a
V
ИЗ
data class User(And sure enough the code above prints:
val email: String,
val firstName: String = "<EMPTY>")
fun main(args: Array<String>) {
val json = "{}"
val gson = Gson()
println(gson.fromJson(json, User::class.java))
}
User(email=null, firstName=null)
ИЗ
V
V
V
V
ИЗ
V
V
ИЗ
V
ИЗ
V
ИЗ
The tricky part comes when trying to understand how can Gson create instances of classes that have no no-args constructor and completely ignore the initialization process. The fact is Gson uses Unsafe.
Believe it or not Unsafe is unsafe. It enables bypassing the object initialization effectively avoiding all the null checks that are generated in the constructor.
Since Kotlin does the null checks in the initialization process allocateInstance will effectively be bypassing them.
V
V
V
V
V