Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

About Me ● Matthew Vern / Panini ● Twitter: @panini_ja Github: panpanini ● Mercari, Inc ● Android Engineer (US) ● Kotlin可愛い

Slide 3

Slide 3 text

Summary ● Protobuf is a type-safe alternative to json ● Most protobuf implementations only generate Java ● NullPointerException

Slide 4

Slide 4 text

Protocol Buffers

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Protocol Buffers - messages ● Message is a definition of an object you want to share

Slide 7

Slide 7 text

Protocol Buffers - messages ● Proto3 syntax: ○ https://developers.google.com/protocol-buffers/docs/proto3 ○ When no value is set, return a Default Value

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Protogen Java - message

Slide 10

Slide 10 text

Protogen Java - message

Slide 11

Slide 11 text

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)

Slide 12

Slide 12 text

Protok

Slide 13

Slide 13 text

Protok ● protoc plugin to generate Kotlin ● Null safety ● Message as data class ● https://github.com/panpanini/protok

Slide 14

Slide 14 text

Protok - message

Slide 15

Slide 15 text

Summary ● With Kotlin, we could avoid NPE Java谷園 ● No more crashes with Protok!

Slide 16

Slide 16 text

Protokがクラッシュした話 全部Java(JVM)が悪い(???)

Slide 17

Slide 17 text

Summary ● Mercari US has used Protok in production for 1.5 years with no problems ● Suddenly, there were problems

Slide 18

Slide 18 text

The problem ● After updating our proto definitions, the app would suddenly crash ● java.lang.VerifyError: Verifier rejected class

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Error vs Exception ● Error: "indicates serious problems that an application should not try to catch." ● Exception: "indicates conditions that an application should catch."

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

JVM limitations ● https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.11

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Solution

Slide 28

Slide 28 text

Summary ● Protok provides null safety for proto3 messages ● JVM only supports 255 method parameters ● Error and Exception are different in Java

Slide 29

Slide 29 text

Have a nice Kotlin!