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

Code generation on the Java VM 2022-04-19

99d4c0ed3c8027ea1c37b53d6441d042?s=47 sullis
April 19, 2022

Code generation on the Java VM 2022-04-19

Code generation on the Java VM
Portland Java User Group
April 19, 2022

#java
#scala
#kotlin

99d4c0ed3c8027ea1c37b53d6441d042?s=128

sullis

April 19, 2022
Tweet

More Decks by sullis

Other Decks in Programming

Transcript

  1. Code generation on the Java VM Sean Sullivan Portland Java

    User Group 2022-04-19
  2. Code generation Java Kotlin Scala

  3. Why code generation?

  4. less boilerplate more consistency Why code generation?

  5. class names parameter names method names exception names Consistency error

    handling logging JSON serialization network I/O
  6. Code generation is not a new idea

  7. 2001: Apache Axis

  8. wsdl2java August 2001

  9. 2003: Apache XmlBeans

  10. September 2003 SchemaCodeGenerator

  11. The most common use case for source code generation…

  12. generating client libraries for REST API’s

  13. Code generation Source code generation

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

  15. javapoet javaparser jdeparser2 CodeModel Source generation

  16. kotlinpoet kotlinx-ast kastree Source generation

  17. scalameta treehugger.scala Source generation

  18. JavaPoet

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

  20. package com.example.helloworld; public final class HelloWorld { public static void

    main(String[] args) { System.out.println("Hello, JavaPoet!"); } } JavaPoet
  21. 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
  22. AWS SDK for Java v2

  23. 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/
  24. AWS SDK for Java v2

  25. JavaPoet AWS SDK for Java v2

  26. Dagger

  27. “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
  28. Dagger 2.x https://dagger.dev/users-guide.html

  29. JavaPoet Dagger 2.x

  30. REST API code generators apibuilder project guardrail project openapi-generator

  31. ApiBuilder project

  32. https://www.apibuilder.io/ ApiBuilder

  33. ApiBuilder

  34. None
  35. ApiBuilder demo

  36. Guardrail project

  37. https://guardrail.dev/ Guardrail

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

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

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

  41. Guardrail example

  42. guardrail-maven-plugin <plugin> <groupId>dev.guardrail</groupId> <artifactId>guardrail-maven-plugin</artifactId> <version>0.70.0</version> <executions> … </executions> </plugin>

  43. guardrail-maven-plugin <con fi guration> <language>java</language> <kind>client</kind> <framework>dropwizard</framework> <specPath>${project.basedir}/src/main/openapi/petstore.json</specPath> <packageName>com.example.clients.petstore</packageName> </con

    fi guration> Java client
  44. guardrail-maven-plugin

  45. openapi-generator project

  46. openapi-generator

  47. openapi-generator

  48. openapi-generator

  49. openapi-generator uses Mustache templates

  50. None
  51. Mustache

  52. Mustache template

  53. Eclipse Collections

  54. Eclipse Collections uses code generation

  55. None
  56. Why does Eclipse Collections use code generation?

  57. Primitive collections

  58. None
  59. StringTemplate library

  60. .stg fi les

  61. None
  62. None
  63. None
  64. None
  65. None
  66. None
  67. String File

  68. Bonus Round

  69. Strange Loop 2021

  70. OpenRewrite

  71. Final thoughts code generation is a powerful technique use a

    code generator to generate client libraries contribute to open source projects
  72. Code generation Questions?

  73. The End