diff options
-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() |