Slide 1

Slide 1 text

Code generation on the Java VM Sean Sullivan Portland Java User Group 2022-04-19

Slide 2

Slide 2 text

Code generation Java Kotlin Scala

Slide 3

Slide 3 text

Why code generation?

Slide 4

Slide 4 text

less boilerplate more consistency Why code generation?

Slide 5

Slide 5 text

class names parameter names method names exception names Consistency error handling logging JSON serialization network I/O

Slide 6

Slide 6 text

Code generation is not a new idea

Slide 7

Slide 7 text

2001: Apache Axis

Slide 8

Slide 8 text

wsdl2java August 2001

Slide 9

Slide 9 text

2003: Apache XmlBeans

Slide 10

Slide 10 text

September 2003 SchemaCodeGenerator

Slide 11

Slide 11 text

The most common use case for source code generation…

Slide 12

Slide 12 text

generating client libraries for REST API’s

Slide 13

Slide 13 text

Code generation Source code generation

Slide 14

Slide 14 text

java.lang.StringBuilder java.io.StringWriter Source generation

Slide 15

Slide 15 text

javapoet javaparser jdeparser2 CodeModel Source generation

Slide 16

Slide 16 text

kotlinpoet kotlinx-ast kastree Source generation

Slide 17

Slide 17 text

scalameta treehugger.scala Source generation

Slide 18

Slide 18 text

JavaPoet

Slide 19

Slide 19 text

JavaPoet https://github.com/square/javapoet

Slide 20

Slide 20 text

package com.example.helloworld; public final class HelloWorld { public static void main(String[] args) { System.out.println("Hello, JavaPoet!"); } } JavaPoet

Slide 21

Slide 21 text

import com.squareup.javapoet.*; MethodSpec main = MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(void.class) .addParameter(String[].class, "args") .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") .build(); TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(main) .build(); JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld) .build(); javaFile.writeTo(System.out); JavaPoet method builder class builder fi le builder

Slide 22

Slide 22 text

AWS SDK for Java v2

Slide 23

Slide 23 text

AWS SDK for Java v2 “To provide SDK support for the many services that AWS owns, the AWS SDKs make extensive use of code generation” https://aws.amazon.com/blogs/developer/aws-sdk-for-java-2-x-released/

Slide 24

Slide 24 text

AWS SDK for Java v2

Slide 25

Slide 25 text

JavaPoet AWS SDK for Java v2

Slide 26

Slide 26 text

Dagger

Slide 27

Slide 27 text

“Dagger 2 is the fi rst to implement the full stack with generated code” Dagger 2.x “Dependency injection frameworks have existed for years with a whole variety of APIs for con fi guring and injecting” https://dagger.dev/users-guide.html

Slide 28

Slide 28 text

Dagger 2.x https://dagger.dev/users-guide.html

Slide 29

Slide 29 text

JavaPoet Dagger 2.x

Slide 30

Slide 30 text

REST API code generators apibuilder project guardrail project openapi-generator

Slide 31

Slide 31 text

ApiBuilder project

Slide 32

Slide 32 text

https://www.apibuilder.io/ ApiBuilder

Slide 33

Slide 33 text

ApiBuilder

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

ApiBuilder demo

Slide 36

Slide 36 text

Guardrail project

Slide 37

Slide 37 text

https://guardrail.dev/ Guardrail

Slide 38

Slide 38 text

https://github.com/guardrail-dev/guardrail Guardrail

Slide 39

Slide 39 text

https://github.com/guardrail-dev/guardrail Guardrail: Scala ScalaMeta

Slide 40

Slide 40 text

https://github.com/guardrail-dev/guardrail Guardrail: Java JavaParser

Slide 41

Slide 41 text

Guardrail example

Slide 42

Slide 42 text

guardrail-maven-plugin dev.guardrail guardrail-maven-plugin 0.70.0 …

Slide 43

Slide 43 text

guardrail-maven-plugin java client dropwizard ${project.basedir}/src/main/openapi/petstore.json com.example.clients.petstore Java client

Slide 44

Slide 44 text

guardrail-maven-plugin

Slide 45

Slide 45 text

openapi-generator project

Slide 46

Slide 46 text

openapi-generator

Slide 47

Slide 47 text

openapi-generator

Slide 48

Slide 48 text

openapi-generator

Slide 49

Slide 49 text

openapi-generator uses Mustache templates

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Mustache

Slide 52

Slide 52 text

Mustache template

Slide 53

Slide 53 text

Eclipse Collections

Slide 54

Slide 54 text

Eclipse Collections uses code generation

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Why does Eclipse Collections use code generation?

Slide 57

Slide 57 text

Primitive collections

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

StringTemplate library

Slide 60

Slide 60 text

.stg fi les

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

String File

Slide 68

Slide 68 text

Bonus Round

Slide 69

Slide 69 text

Strange Loop 2021

Slide 70

Slide 70 text

OpenRewrite

Slide 71

Slide 71 text

Final thoughts code generation is a powerful technique use a code generator to generate client libraries contribute to open source projects

Slide 72

Slide 72 text

Code generation Questions?

Slide 73

Slide 73 text

The End