深入浅出 Java Writer
简介 Java Writer属于JavaPoet另一个分支,也是生成Java 源文件的工具。Java Writer相比JavaPoet,更加简单,简单到整个库只有一个文件。
Java Writer官方地址:https://github.com/square/javapoet/tree/javawriter_2
JavaWriter is a utility class which aids in generating Java source files.
使用 引入 Gradle
1 compile 'com.squareup:javawriter:2.5.1'
Maven
1 2 3 4 5 <dependency> <groupId>com.squareup</groupId> <artifactId>javawriter</artifactId> <version>2.5.1</version> </dependency>
实例 我们要生成的源码文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 package com.example; public final class Person { private String firstName; private String lastName; /** * Returns the person's full name. */ public String getName() { return firstName + " " + lastName; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 File outFile;/// .java file OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile)); JavaWriter writer = new JavaWriter(writer); writer.emitPackage("com.example") .beginType("com.example.Person", "class", EnumSet.of(PUBLIC, FINAL)) .emitField("String", "firstName", EnumSet.of(PRIVATE)) .emitField("String", "lastName", EnumSet.of(PRIVATE)) .emitJavadoc("Returns the person's full name.") .beginMethod("String", "getName", EnumSet.of(PUBLIC)) .emitStatement("return firstName + \" \" + lastName") .endMethod() .endType(); .close();
参考: