Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java谷園から逃げ出した話

Panini
September 25, 2020

 Java谷園から逃げ出した話

Panini

September 25, 2020
Tweet

More Decks by Panini

Other Decks in Programming

Transcript

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

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

    Most protobuf implementations only generate Java • NullPointerException
  3. 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
  4. 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
  5. 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)
  6. Protok • protoc plugin to generate Kotlin • Null safety

    • Message as data class • https://github.com/panpanini/protok
  7. Summary • Mercari US has used Protok in production for

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

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

    application should not try to catch." • Exception: "indicates conditions that an application should catch."
  10. 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
  11. 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.
  12. 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
  13. 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
  14. Summary • Protok provides null safety for proto3 messages •

    JVM only supports 255 method parameters • Error and Exception are different in Java