JavaPoet 是一個生成.Java源文件的Java API。源文件生成在annotation processing
或者和一些元數據,例如文件,數據庫設計,協議格式等,進行交互時會頗有用。經過代碼生成,你可以減小冗餘的代碼模板,同時保證惟一正確的源文件爲元數據。java
原文 JavaPoet
is a Java API for generating .java
source files.android
Source file generation can be useful when doing things such as annotation processing or interacting with metadata files (e.g., database schemas, protocol formats). By generating code, you eliminate the need to write boilerplate while also keeping a single source of truth for the metadata.git
基本使用邏輯,按照正常的新建.java,按照邏輯編寫對應的代碼,編寫的代碼替換爲Javapoet的語法就能夠了,Javapoet 的語法過一遍不用記憶,甚至看一下最小案例,直接開車都木有問題,使用的時候不記得訪問GitHub主頁對應搜索github
引用配置數據庫
compile 'com.squareup:javapoet:1.11.1'
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.11.1</version>
</dependency>
複製代碼
HelloWorld.javabash
package com.example.helloworld;
public final class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, JavaPoet!");
}
}
複製代碼
JavaPoet.java數據庫設計
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);
複製代碼
會自動
import java.util.Date;`包名
ClassName hoverboard = ClassName.get("com.mattel", "Hoverboard");
CodeBlock.builder().add("I ate $2L $1L", "tacos", 3)
Map<String, Object> map = new LinkedHashMap<>();
map.put("food", "tacos");
map.put("count", 3);
CodeBlock.builder().addNamed("I ate $count:L $food:L", map)
複製代碼
JavaFile.builder("com.example.helloworld", hello)
.addStaticImport(hoverboard, "createNimbus")
.build();
複製代碼
增長代碼塊ide
MethodSpec main = MethodSpec.methodBuilder("main")
.addCode(""
+ "int total = 0;\n"
+ "for (int i = 0; i < 10; i++) {\n"
+ " total += i;\n"
+ "}\n")
.build();
void main() {
int total = 0;
for (int i = 0; i < 10; i++) {
total += i;
}
}
複製代碼
ddStatement
endControlFlow 不用寫{\n;}
等MethodSpec main = MethodSpec.methodBuilder("main")
.addStatement("int total = 0")
.beginControlFlow("for (int i = 0; i < 10; i++)")
.addStatement("total += i")
.endControlFlow()
.build();
複製代碼
函數內部調用函數
public String byteToHex(int b) {
char[] result = new char[2];
result[1] = hexDigit(b & 0xf);
return new String(result);
}
public char hexDigit(int i) {
return (char) (i < 10 ? i + '0' : i - 10 + 'a');
}
//-------------------------------------
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[1] = $N(b & 0xf)", hexDigit)
.addStatement("return new String(result)")
.build();
複製代碼
public abstract class HelloWorld {
protected abstract void flux();
}
//-------------------------
MethodSpec flux = MethodSpec.methodBuilder("flux")
.addModifiers(Modifier.ABSTRACT, Modifier.PROTECTED)
.build();
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addMethod(flux)
.build();
複製代碼
public HelloWorld(String greeting) {
this.greeting = greeting;
}
//------------------------------
MethodSpec flux = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(String.class, "greeting")
.addStatement("this.$N = $N", "greeting", "greeting")
.build();
複製代碼
Parameters
void welcomeOverlords(final String android, final String robot) {
}
//-------------------------------
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();
複製代碼
FieldSpec android = FieldSpec.builder(String.class, "android")
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.initializer("$S + $L", "Lollipop v.", 5.0d)
.build();
複製代碼