diff options
author | Egor Andreevici <egor@squareup.com> | 2020-01-06 14:29:28 -0500 |
---|---|---|
committer | Egor Andreevici <egor@squareup.com> | 2020-01-06 14:29:28 -0500 |
commit | 74a0f27a71a5627a81799010d761fb4f5b08e1f6 (patch) | |
tree | 61715967b13fc5fad2006228c723c6051d70500c | |
parent | 80ddc99409399bd15b06509a6e3e75cb4117b8d2 (diff) | |
parent | dc64ed1f9bf362e68451d75f302e6e656a114066 (diff) | |
download | javapoet-74a0f27a71a5627a81799010d761fb4f5b08e1f6.tar.gz |
Merge branch 'galderz-t_javafile_writeto_691'
-rw-r--r-- | src/main/java/com/squareup/javapoet/JavaFile.java | 31 |
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() |