aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalder ZamarrenĖƒo <galder@zamarreno.com>2019-01-29 08:44:01 +0100
committerEgor Andreevici <egor@squareup.com>2020-01-06 14:28:54 -0500
commitdc64ed1f9bf362e68451d75f302e6e656a114066 (patch)
tree61715967b13fc5fad2006228c723c6051d70500c
parent80ddc99409399bd15b06509a6e3e75cb4117b8d2 (diff)
downloadjavapoet-dc64ed1f9bf362e68451d75f302e6e656a114066.tar.gz
Add writeTo methods returning Path/File written to #691
-rw-r--r--src/main/java/com/squareup/javapoet/JavaFile.java31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/main/java/com/squareup/javapoet/JavaFile.java b/src/main/java/com/squareup/javapoet/JavaFile.java
index a419801..d7fa6df 100644
--- a/src/main/java/com/squareup/javapoet/JavaFile.java
+++ b/src/main/java/com/squareup/javapoet/JavaFile.java
@@ -103,14 +103,30 @@ public final class JavaFile {
/** Writes this to {@code directory} as UTF-8 using the standard directory structure. */
public void writeTo(Path directory) throws IOException {
- writeTo(directory, UTF_8);
+ writeToPath(directory);
}
/**
- * Writes this to {@code directory} with the provided {@code charset}
- * using the standard directory structure.
+ * Writes this to {@code directory} with the provided {@code charset} using the standard directory
+ * structure.
*/
public void writeTo(Path directory, Charset charset) throws IOException {
+ writeToPath(directory, charset);
+ }
+
+ /**
+ * Writes this to {@code directory} as UTF-8 using the standard directory structure.
+ * Returns the {@link Path} instance to which source is actually written.
+ * */
+ public Path writeToPath(Path directory) throws IOException {
+ return writeToPath(directory, UTF_8);
+ }
+
+ /**
+ * Writes this to {@code directory} with the provided {@code charset} using the standard directory
+ * structure.
+ * Returns the {@link Path} instance to which source is actually written. */
+ public Path writeToPath(Path directory, Charset charset) throws IOException {
checkArgument(Files.notExists(directory) || Files.isDirectory(directory),
"path %s exists but is not a directory.", directory);
Path outputDirectory = directory;
@@ -125,6 +141,8 @@ public final class JavaFile {
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(outputPath), charset)) {
writeTo(writer);
}
+
+ return outputPath;
}
/** Writes this to {@code directory} as UTF-8 using the standard directory structure. */
@@ -132,6 +150,13 @@ public final class JavaFile {
writeTo(directory.toPath());
}
+ /** Writes this to {@code directory} as UTF-8 using the standard directory structure.
+ * Returns the {@link File} instance to which source is actually written. */
+ public File writeToFile(File directory) throws IOException {
+ final Path outputPath = writeToPath(directory.toPath());
+ return outputPath.toFile();
+ }
+
/** Writes this to {@code filer}. */
public void writeTo(Filer filer) throws IOException {
String fileName = packageName.isEmpty()