2

Introducing JavaPoet

 2 years ago
source link: https://developer.squareup.com/blog/introducing-javapoet/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
January 29th, 2015 | 2 minute read

Introducing JavaPoet

Square has a new library for generating Java code.

Twitter
Facebook
Reddit
LinkedIn

Written by Jesse Wilson.

I used to hate generated code. Early implementations of JSPs, EJBs, and protocol buffers spewed out huge, misformatted files littered with warnings and fully-qualified type names:

*/***
*     * <code>optional string error_text = 2;</code>*
*     */*
    **public** Builder **setErrorText(**
        java**.**lang**.**String value**)** **{**
      **if** **(**value **==** **null)** **{**
  **throw** **new** **NullPointerException();**
**}**
bitField0_ **|=** 0x00000002**;**
      errorText_ **=** value**;**
      onChanged**();**
      **return** **this;**
    **}**

But things get better. Integrating generated code into a project is easier thanks to Java’s powerful annotation processing facilities. Testing code generators has been tamed by Google’s compile-testing library. Today Square moves faster thanks to the generated code in AutoValue, Dagger, Butter Knife, and Wire.

Unfortunately the code that generates code in each of these libraries is still awkward. For example, Dagger writes each generated file top to bottom which means it needs to prepare imports as a clumsy separate step. It models the .java file as a string and doesn’t understand its structure.

Today I’m eager to announce JavaPoet, a successor to JavaWriter. JavaPoet models a .java file with types, methods, and fields and does imports automatically. Generate “Hello World” like this:

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.emit(System.out);

Get JavaPoet on GitHub. Jesse Wilson Android and jokes.medium.com

Picture of Square Engineering

By Square Engineering
@SquareEngMedium


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK