Java谷園から逃げ出した話

3295d80faee461db1093982361256fe4?s=47 Panini
September 25, 2020

 Java谷園から逃げ出した話

3295d80faee461db1093982361256fe4?s=128

Panini

September 25, 2020
Tweet

Transcript

  1. Java谷園から逃げ出した 話 Kotlin愛好会 vol.24

  2. About Me • Matthew Vern / Panini • Twitter: @panini_ja

    Github: panpanini • Mercari, Inc • Android Engineer (US) • Kotlin可愛い
  3. Summary • Protobuf is a type-safe alternative to json •

    Most protobuf implementations only generate Java • NullPointerException
  4. Protocol Buffers

  5. Protocol Buffers • https://developers.google.com/protocol-buffers • Google's language-neutral, platform-neutral, extensible mechanism

    for serializing structured data • Defines API contract between Server and Client • Proto models are generated per platform needed
  6. Protocol Buffers - messages • Message is a definition of

    an object you want to share
  7. Protocol Buffers - messages • Proto3 syntax: ◦ https://developers.google.com/protocol-buffers/docs/proto3 ◦

    When no value is set, return a Default Value
  8. Protogen Java • (in Jan 2019) Only 2 ways to

    generate proto for Android ◦ https://github.com/square/wire ◦ https://developers.google.com/protocol-buffers/docs/reference/java-generated • Neither use Nullability annotations ⇒ NullPointerException
  9. Protogen Java - message

  10. Protogen Java - message

  11. Problems • Generated code doesn’t provide Nullability annotations ◦ Because

    no annotations, Kotlin will use platform types (eg: String!) • Both Wire & Google protogen-java return null by default, and require callers to check to replace Default Value ◦ Eg: Wire.get(person.name, Person.DEFAULT_NAME)
  12. Protok

  13. Protok • protoc plugin to generate Kotlin • Null safety

    • Message as data class • https://github.com/panpanini/protok
  14. Protok - message

  15. Summary • With Kotlin, we could avoid NPE Java谷園 •

    No more crashes with Protok!
  16. Protokがクラッシュした話 全部Java(JVM)が悪い(???)

  17. Summary • Mercari US has used Protok in production for

    1.5 years with no problems • Suddenly, there were problems
  18. The problem • After updating our proto definitions, the app

    would suddenly crash • java.lang.VerifyError: Verifier rejected class
  19. None
  20. Error vs Exception • Error: "indicates serious problems that an

    application should not try to catch." • Exception: "indicates conditions that an application should catch."
  21. None
  22. VerifyError • https://docs.oracle.com/javase/7/docs/api/java/lang/VerifyError.html • Thrown when the "verifier" detects that

    a class file, though well formed, contains some sort of internal inconsistency or security problem. • Not a compile error, so probably we found a limitation of the JVM
  23. JVM limitations • https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.11

  24. JVM limitations • https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.11 • The number of method parameters

    is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.
  25. Problems • Protok uses the constructor to pass fields to

    the message instance • Constructor is a function • A function can only have 255* parameters ⇒ A protok message can only have 255 fields
  26. Solution • Protok no longer uses data class • https://github.com/panpanini/protok/pull/64

    • Use Builder pattern & function with lambda to make a better Kotlin API
  27. Solution

  28. Summary • Protok provides null safety for proto3 messages •

    JVM only supports 255 method parameters • Error and Exception are different in Java
  29. Have a nice Kotlin!