Code generation on the Java VM - Montreal - 2019-11-05

99d4c0ed3c8027ea1c37b53d6441d042?s=47 sullis
November 05, 2019

Code generation on the Java VM - Montreal - 2019-11-05

Montreal Java User Group
November 5, 2019

#Java
#Scala
#Kotlin
#JavaPoet

99d4c0ed3c8027ea1c37b53d6441d042?s=128

sullis

November 05, 2019
Tweet

Transcript

  1. Génération de code sur la Java VM Sean Sullivan Montreal

    Java User Group 5 Novembre 2019
  2. About me

  3. None
  4. Voorhees Computing Center

  5. None
  6. Java, 23 years Scala, 8 years Kotlin, 1 year

  7. génération de code sur la JVM Java Kotlin Scala

  8. Why code generation?

  9. less boilerplate more consistency Why code generation?

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

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

  12. 2001: Apache Axis

  13. wsdl2java August 2001

  14. 2003: Apache XmlBeans

  15. September 2003 SchemaCodeGenerator

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

  17. generating client libraries for REST API’s

  18. bytecode source code Two kinds of code generation

  19. génération de code sur la JVM Bytecode generation

  20. Bytecode generators produce Java class definitions

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

  22. Spring Framework

  23. cglib spring-core

  24. cglib spring-beans

  25. Micronaut framework

  26. “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
  27. “This goal is achieved through the use of Java’s annotation

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

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

  30. Mockito

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

  32. “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
  33. Datadog Java agent

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

  35. How can I implement a custom Java agent?

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

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

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

  39. génération de code sur la JVM Source code generation

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

  41. javapoet CodeModel jdeparser2 javaparser Source generation

  42. kotlinpoet kastree Source generation

  43. scalameta treehugger.scala Source generation

  44. JavaPoet

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

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

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

  49. 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/
  50. AWS SDK for Java v2

  51. JavaPoet AWS SDK for Java v2

  52. Dagger

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

  55. JavaPoet Dagger 2.x

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

  57. ApiBuilder project

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

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

  60. ApiBuilder demo

  61. Guardrail project

  62. https://guardrail.dev/ Guardrail

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

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

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

  66. Guardrail demo

  67. openapi-generator project

  68. openapi-generator demo

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

    code generator to generate client libraries contribute to open source projects
  70. génération de code sur la JVM Questions?

  71. The End