$30 off During Our Annual Pro Sale. View Details »

Code generation on the Java VM 2022-04-19

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

sullis

April 19, 2022
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. package com.example.helloworld; public final class HelloWorld { public static void

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

    code generator to generate client libraries contribute to open source projects