JavaPoet

注解系列

0x00 概述

上一篇限于篇幅只介绍了APT,这篇来继续介绍javapoet,是square公司的开源库。正如其名,java诗人,通过注解来生成java源文件,通常要使用javapoet这个库与Filer配合使用。主要和注解配合用来干掉那些重复的模板代码(如butterknife
和databinding所做的事情),当然你也可以使用这个技术让你的代码更加的炫酷。

0x01 简单使用

使用之前要先引入这个库

1
compile 'com.squareup:javapoet:1.7.0'

javapoet是用来生成代码的,需要借助

常用类

使用javapoet前需要了解4个常用类

MethodSpec 代表一个构造函数或方法声明。

TypeSpec 代表一个类,接口,或者枚举声明。

FieldSpec 代表一个成员变量,一个字段声明。

JavaFile包含一个顶级类的Java文件。

国际惯例先自动生成一个helloWorld类
定义一个编译期注解

1
2
3
4
5
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface clazz_hello {
String value();
}

然后看下helloworld的注解处理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@AutoService(Processor.class)
public class HelloWorldProcess extends AbstractProcessor {

private Filer filer;

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
filer = processingEnv.getFiler();
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement element : annotations) {
if (element.getQualifiedName().toString().equals(clazz_hello.class.getCanonicalName())) {
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();

try {
JavaFile javaFile = JavaFile.builder("com.xsf", helloWorld)
.addFileComment(" This codes are generated automatically. Do not modify!")
.build();
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}

@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet<>();
annotations.add(clazz_hello.class.getCanonicalName());
return annotations;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}

这样就会在app-build-source-apt-debug-com.xsf文件夹下生成这个文件

0x02 使用进阶

方法&控制流:

添加方法

addcodeaddstatement,对与无需类引入的极简代码可以直接使用addCode

1
2
3
4
5
6
7
MethodSpec main = MethodSpec.methodBuilder("main")
.addCode(""
+ "int total = 0;\n"
+ "for (int i = 0; i < 10; i++) {\n"
+ " total += i;\n"
+ "}\n")
.build();

生成的是

1
2
3
4
5
6
void main() {
int total = 0;
for (int i = 0; i < 10; i++) {
total += i;
}
}

要是需要import的方法,如上面的.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") 就需要使用.addStatement来声明

更优雅的流控制

beginControlFlow 流开启
addStatement 处理语句
endControlFlow()流结束

如上面的用流改写就是

1
2
3
4
5
6
MethodSpec main = MethodSpec.methodBuilder("main")
.addStatement("int total = 0")
.beginControlFlow("for (int i = 0; i < 10; i++)")
.addStatement("total += i")
.endControlFlow()
.build();

占位符

javapoet里面提供了占位符来帮助我们更好地生成代码

$L 字面常量(Literals)

1
2
3
4
5
6
7
8
9
10
private MethodSpec computeRange(String name, int from, int to, String op) {
return MethodSpec.methodBuilder(name)
.returns(int.class)
.addStatement("int result = 0")
.beginControlFlow("for (int i = $L; i < $L; i++)", from, to)
.addStatement("result = result $L i", op)
.endControlFlow()
.addStatement("return result")
.build();
}

这个就是一个for循环,op负责加减乘除等符号

$S 字符串常量(String)

$T 类型(Types)

最大的特点是自动导入包,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MethodSpec today = MethodSpec.methodBuilder("today")
.returns(Date.class)
.addStatement("return new $T()", Date.class)
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(today)
.build();

JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld)
.build();

javaFile.writeTo(System.out);

生成的代码如下,而且会自动导包

1
2
3
4
5
6
7
8
9
package com.example.helloworld;

import java.util.Date;

public final class HelloWorld {
Date today() {
return new Date();
}
}

如果我们想要导入自己写的类怎么办?上面的例子是传入系统的class,这里也提供一种方式,通过ClassName.get(”类的路径”,”类名“),结合$T可以生成

1
2
3
4
5
6
7
8
9
10
11
12
13
ClassName hoverboard = ClassName.get("com.mattel", "Hoverboard");
ClassName list = ClassName.get("java.util", "List");
ClassName arrayList = ClassName.get("java.util", "ArrayList");
TypeName listOfHoverboards = ParameterizedTypeName.get(list, hoverboard);

MethodSpec beyond = MethodSpec.methodBuilder("beyond")
.returns(listOfHoverboards)
.addStatement("$T result = new $T<>()", listOfHoverboards, arrayList)
.addStatement("result.add(new $T())", hoverboard)
.addStatement("result.add(new $T())", hoverboard)
.addStatement("result.add(new $T())", hoverboard)
.addStatement("return result")
.build();

然后生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.helloworld;

import com.mattel.Hoverboard;
import java.util.ArrayList;
import java.util.List;

public final class HelloWorld {
List<Hoverboard> beyond() {
List<Hoverboard> result = new ArrayList<>();
result.add(new Hoverboard());
result.add(new Hoverboard());
result.add(new Hoverboard());
return result;
}
}

在导入包这里,javapoet 同样支持import static,通过addStaticImport

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ClassName hoverboard = ClassName.get("com.mattel", "Hoverboard");

ClassName namedBoards = ClassName.get("com.mattel", "Hoverboard", "Boards");

MethodSpec beyond = MethodSpec.methodBuilder("beyond")
.returns(listOfHoverboards)
.addStatement("$T result = new $T<>()", listOfHoverboards, arrayList)
.addStatement("result.add($T.createNimbus(2000))", hoverboard)
.addStatement("result.add($T.createNimbus(\"2001\"))", hoverboard)
.addStatement("result.add($T.createNimbus($T.THUNDERBOLT))", hoverboard, namedBoards)
.addStatement("$T.sort(result)", Collections.class)
.addStatement("return result.isEmpty() $T.emptyList() : result", Collections.class)
.build();

TypeSpec hello = TypeSpec.classBuilder("HelloWorld")
.addMethod(beyond)
.build();

JavaFile.builder("com.example.helloworld", hello)
.addStaticImport(hoverboard, "createNimbus")
.addStaticImport(namedBoards, "*")
.addStaticImport(Collections.class, "*")
.build();

$N 命名(Names)

通常指我们自己生成的方法名或者变量名等等比如这样的代码块

1
2
3
4
5
6
7
8
9
10
public String byteToHex(int b) {
char[] result = new char[2];
result[0] = hexDigit((b >>> 4) & 0xf);
result[1] = hexDigit(b & 0xf);
return new String(result);
}

public char hexDigit(int i) {
return (char) (i < 10 ? i + '0' : i - 10 + 'a');
}

我们可以传递hexDigit()来代替。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MethodSpec hexDigit = MethodSpec.methodBuilder("hexDigit")
.addParameter(int.class, "i")
.returns(char.class)
.addStatement("return (char) (i < 10 ? i + '0' : i - 10 + 'a')")
.build();

MethodSpec byteToHex = MethodSpec.methodBuilder("byteToHex")
.addParameter(int.class, "b")
.returns(String.class)
.addStatement("char[] result = new char[2]")
.addStatement("result[0] = $N((b >>> 4) & 0xf)", hexDigit)
.addStatement("result[1] = $N(b & 0xf)", hexDigit)
.addStatement("return new String(result)")
.build();

构建类的元素

Methods

方法的修饰,如Modifiers.ABSTRACT

1
2
3
4
5
6
7
8
MethodSpec flux = MethodSpec.methodBuilder("flux")
.addModifiers(Modifier.ABSTRACT, Modifier.PROTECTED)
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addMethod(flux)
.build();

这将会生成如下代码

1
2
3
public abstract class HelloWorld {
protected abstract void flux();
}

当然Methods需要和MethodSpec.Builder配置来增加方法参数、异常、javadoc、注解等。

构造器

这个其实也是个函数方法而已,因此可以使用MethodSpec来生成构造器方法。比如:

1
2
3
4
5
6
7
8
9
10
11
MethodSpec flux = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(String.class, "greeting")
.addStatement("this.$N = $N", "greeting", "greeting")
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC)
.addField(String.class, "greeting", Modifier.PRIVATE, Modifier.FINAL)
.addMethod(flux)
.build();

将会生成

1
2
3
4
5
6
7
public class HelloWorld {
private final String greeting;

public HelloWorld(String greeting) {
this.greeting = greeting;
}
}

参数

之前我们是通过addstatement直接设置参数,其实参数也有自己的一个专用类ParameterSpec,我们可以使用ParameterSpec.builder()来生成参数,然后MethodSpec的addParameter去使用,这样更加优雅。

1
2
3
4
5
6
7
8
ParameterSpec android = ParameterSpec.builder(String.class, "android")
.addModifiers(Modifier.FINAL)
.build();

MethodSpec welcomeOverlords = MethodSpec.methodBuilder("welcomeOverlords")
.addParameter(android)
.addParameter(String.class, "robot", Modifier.FINAL)
.build();

生成的代码

1
2
void welcomeOverlords(final String android, final String robot) {
}

字段

可以使用FieldSpec去声明字段,然后加到Method中处理

1
2
3
4
5
6
7
8
9
FieldSpec android = FieldSpec.builder(String.class, "android")
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC)
.addField(android)
.addField(String.class, "robot", Modifier.PRIVATE, Modifier.FINAL)
.build();

然后生成代码

1
2
3
4
5
public class HelloWorld {
private final String android;

private final String robot;
}

通常Builder可以更加详细的创建字段的内容,比如javadoc、annotations或者初始化字段参数等,如:

1
2
3
4
FieldSpec android = FieldSpec.builder(String.class, "android")
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.initializer("$S + $L", "Lollipop v.", 5.0d)
.build();

对应生成的代码

1
private final String android = "Lollipop v." + 5.0;

接口

接口方法必须是PUBLIC ABSTRACT并且接口字段必须是PUBLIC STATIC FINAL ,使用TypeSpec.interfaceBuilder

如下

1
2
3
4
5
6
7
8
9
10
TypeSpec helloWorld = TypeSpec.interfaceBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC)
.addField(FieldSpec.builder(String.class, "ONLY_THING_THAT_IS_CONSTANT")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", "change")
.build())
.addMethod(MethodSpec.methodBuilder("beep")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.build())
.build();

生成的代码如下

1
2
3
4
5
public interface HelloWorld {
String ONLY_THING_THAT_IS_CONSTANT = "change";

void beep();
}
  • 枚举类型

使用TypeSpec.enumBuilder来创建,使用addEnumConstant来添加

1
2
3
4
5
6
TypeSpec helloWorld = TypeSpec.enumBuilder("Roshambo")
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("ROCK")
.addEnumConstant("SCISSORS")
.addEnumConstant("PAPER")
.build();

生成的代码

1
2
3
4
5
6
7
public enum Roshambo {
ROCK,

SCISSORS,

PAPER
}

更复杂的类型也可以支持,如重写、注解等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
TypeSpec helloWorld = TypeSpec.enumBuilder("Roshambo")
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("ROCK", TypeSpec.anonymousClassBuilder("$S", "fist")
.addMethod(MethodSpec.methodBuilder("toString")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addStatement("return $S", "avalanche!")
.build())
.build())
.addEnumConstant("SCISSORS", TypeSpec.anonymousClassBuilder("$S", "peace")
.build())
.addEnumConstant("PAPER", TypeSpec.anonymousClassBuilder("$S", "flat")
.build())
.addField(String.class, "handsign", Modifier.PRIVATE, Modifier.FINAL)
.addMethod(MethodSpec.constructorBuilder()
.addParameter(String.class, "handsign")
.addStatement("this.$N = $N", "handsign", "handsign")
.build())
.build();

生成代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public enum Roshambo {
ROCK("fist") {
@Override
public void toString() {
return "avalanche!";
}
},

SCISSORS("peace"),

PAPER("flat");

private final String handsign;

Roshambo(String handsign) {
this.handsign = handsign;
}
}

匿名内部类

需要使用Type.anonymousInnerClass(""),通常可以使用$L占位符来指代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TypeSpec comparator = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(ParameterizedTypeName.get(Comparator.class, String.class))
.addMethod(MethodSpec.methodBuilder("compare")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(String.class, "a")
.addParameter(String.class, "b")
.returns(int.class)
.addStatement("return $N.length() - $N.length()", "a", "b")
.build())
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addMethod(MethodSpec.methodBuilder("sortByLength")
.addParameter(ParameterizedTypeName.get(List.class, String.class), "strings")
.addStatement("$T.sort($N, $L)", Collections.class, "strings", comparator)
.build())
.build();

生成代码

1
2
3
4
5
6
7
8
void sortByLength(List<String> strings) {
Collections.sort(strings, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.length() - b.length();
}
});
}

定义匿名内部类的一个特别棘手的问题是参数的构造。在上面的代码中我们传递了不带参数的空字符串。TypeSpec.anonymousClassBuilder(“”)。

注解

注解使用起来比较简单

1
2
3
4
5
6
MethodSpec toString = MethodSpec.methodBuilder("toString")
.addAnnotation(Override.class)
.returns(String.class)
.addModifiers(Modifier.PUBLIC)
.addStatement("return $S", "Hoverboard")
.build();

生成代码

1
2
3
4
@Override
public String toString() {
return "Hoverboard";
}

通过AnnotationSpec.builder() 可以对注解设置属性:

1
2
3
4
5
6
7
8
9
MethodSpec logRecord = MethodSpec.methodBuilder("recordEvent")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addAnnotation(AnnotationSpec.builder(Headers.class)
.addMember("accept", "$S", "application/json; charset=utf-8")
.addMember("userAgent", "$S", "Square Cash")
.build())
.addParameter(LogRecord.class, "logRecord")
.returns(LogReceipt.class)
.build();

代码生成如下

1
2
3
4
5
@Headers(
accept = "application/json; charset=utf-8",
userAgent = "Square Cash"
)
LogReceipt recordEvent(LogRecord logRecord);

注解同样可以注解其他注解,通过$L引用如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MethodSpec logRecord = MethodSpec.methodBuilder("recordEvent")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addAnnotation(AnnotationSpec.builder(HeaderList.class)
.addMember("value", "$L", AnnotationSpec.builder(Header.class)
.addMember("name", "$S", "Accept")
.addMember("value", "$S", "application/json; charset=utf-8")
.build())
.addMember("value", "$L", AnnotationSpec.builder(Header.class)
.addMember("name", "$S", "User-Agent")
.addMember("value", "$S", "Square Cash")
.build())
.build())
.addParameter(LogRecord.class, "logRecord")
.returns(LogReceipt.class)
.build();

生成代码

1
2
3
4
5
@HeaderList({
@Header(name = "Accept", value = "application/json; charset=utf-8"),
@Header(name = "User-Agent", value = "Square Cash")
})
LogReceipt recordEvent(LogRecord logRecord);

0x03 后续

在javapoet之前有javawriter,但javapoet有着更强大的代码模型,并且对类的理解更加到位,因此推荐使用javapoet

0%