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

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

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

sullis

November 05, 2019
Tweet

More Decks by sullis

Other Decks in Programming

Transcript

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

    handling logging JSON serialization network I/O
  2. “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
  3. “This goal is achieved through the use of Java’s annotation

    processors" micronaut https://docs.micronaut.io/latest/guide/index.html
  4. “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
  5. package com.example.helloworld; public final class HelloWorld { public static void

    main(String[] args) { System.out.println("Hello, JavaPoet!"); } } JavaPoet
  6. 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
  7. 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/
  8. “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
  9. Final thoughts code generation is a powerful technique use a

    code generator to generate client libraries contribute to open source projects