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

Code generation on the Java VM 2019-12-05

December 05, 2019

Code generation on the Java VM 2019-12-05

December 5, 2019



December 05, 2019

More Decks by sullis

Other Decks in Programming


  1. Code generation on the Java VM Sean Sullivan 2019-12-05

  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. bytecode source code Two kinds of code generation

  14. Code generation Bytecode generation

  15. Bytecode generators produce Java class definitions

  16. ByteBuddy ObjectWeb ASM cglib Bytecode libraries Javassist Commons BCEL AspectJ

  17. Spring Framework

  18. cglib spring-core

  19. cglib spring-beans

  20. Micronaut framework

  21. “Unlike Spring which relies exclusively on runtime reflection and proxies,

    Micronaut uses compile time data to implement dependency injection” micronaut https://docs.micronaut.io/latest/guide/ioc.html
  22. “This goal is achieved through the use of Java’s annotation

    processors" micronaut https://docs.micronaut.io/latest/guide/index.html
  23. micronaut https://github.com/micronaut-projects/micronaut-core Annotation processors

  24. micronaut ObjectWeb ASM https://github.com/micronaut-projects/micronaut-core

  25. Mockito

  26. mockito 2.x ByteBuddy https://github.com/mockito/mockito

  27. “Mockito switched from CGLIB to the great library ByteBuddy to

    make mocks” https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2 mockito 2.x
  28. Datadog Java agent

  29. Datadog Java agent ByteBuddy https://github.com/DataDog/dd-trace-java

  30. How can I implement a custom Java agent?

  31. Java agent example https://github.com/sullis/java-lang-instrument-example/

  32. Java agent example “premain” method https://github.com/sullis/java-lang-instrument-example/

  33. Java agent example https://github.com/sullis/java-lang-instrument-example/ ByteBuddy AgentBuilder

  34. Code generation Source code generation

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

  36. javapoet CodeModel jdeparser2 javaparser Source generation

  37. kotlinpoet kotlinx-ast kastree Source generation

  38. scalameta treehugger.scala Source generation

  39. JavaPoet

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

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

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

  44. 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/
  45. AWS SDK for Java v2

  46. JavaPoet AWS SDK for Java v2

  47. Dagger

  48. “Dagger 2 is the first 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 configuring and injecting” https://dagger.dev/users-guide.html
  49. Dagger 2.x https://dagger.dev/users-guide.html

  50. JavaPoet Dagger 2.x

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

  52. ApiBuilder project

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

  54. https://github.com/apicollective/apibuilder-generator/ ApiBuilder

  55. ApiBuilder demo

  56. Guardrail project

  57. https://guardrail.dev/ Guardrail

  58. https://github.com/twilio/guardrail Guardrail

  59. https://github.com/twilio/guardrail Guardrail: Scala ScalaMeta

  60. https://github.com/twilio/guardrail Guardrail: Java JavaParser

  61. Guardrail example

  62. guardrail-maven-plugin <plugin> <groupId>com.twilio</groupId> <artifactId>guardrail-maven-plugin_2.12</artifactId> <version>0.54.5</version> <executions> … </executions> </plugin>

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

  64. guardrail-maven-plugin

  65. openapi-generator project

  66. openapi-generator

  67. openapi-generator

  68. openapi-generator

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

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

  71. The End