diff --git a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java index 6b6f01ff13..bfc25366ef 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java +++ b/javaparser-core/src/main/java/com/github/javaparser/JavaParser.java @@ -173,7 +173,7 @@ public ParseResult parse(final InputStream in) { @Deprecated public ParseResult parse(final File file, final Charset encoding) throws FileNotFoundException { ParseResult result = parse(COMPILATION_UNIT, provider(file, encoding)); - result.getResult().ifPresent(cu -> cu.setStorage(file.toPath())); + result.getResult().ifPresent(cu -> cu.setStorage(file.toPath(), encoding)); return result; } @@ -188,7 +188,7 @@ public ParseResult parse(final File file, final Charset encodin */ public ParseResult parse(final File file) throws FileNotFoundException { ParseResult result = parse(COMPILATION_UNIT, provider(file, configuration.getCharacterEncoding())); - result.getResult().ifPresent(cu -> cu.setStorage(file.toPath())); + result.getResult().ifPresent(cu -> cu.setStorage(file.toPath(), configuration.getCharacterEncoding())); return result; } @@ -206,7 +206,7 @@ public ParseResult parse(final File file) throws FileNotFoundEx @Deprecated public ParseResult parse(final Path path, final Charset encoding) throws IOException { ParseResult result = parse(COMPILATION_UNIT, provider(path, encoding)); - result.getResult().ifPresent(cu -> cu.setStorage(path)); + result.getResult().ifPresent(cu -> cu.setStorage(path, encoding)); return result; } @@ -221,7 +221,7 @@ public ParseResult parse(final Path path, final Charset encodin */ public ParseResult parse(final Path path) throws IOException { ParseResult result = parse(COMPILATION_UNIT, provider(path, configuration.getCharacterEncoding())); - result.getResult().ifPresent(cu -> cu.setStorage(path)); + result.getResult().ifPresent(cu -> cu.setStorage(path, configuration.getCharacterEncoding())); return result; } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java b/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java index 35db551c37..846ae1e07d 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java @@ -42,25 +42,25 @@ import com.github.javaparser.utils.ClassUtils; import com.github.javaparser.utils.CodeGenerationUtils; import com.github.javaparser.utils.Utils; + import java.io.IOException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Optional; import java.util.function.Function; -import static com.github.javaparser.JavaToken.Kind.*; + +import static com.github.javaparser.JavaToken.Kind.EOF; import static com.github.javaparser.Providers.UTF8; import static com.github.javaparser.Providers.provider; -import static com.github.javaparser.Range.*; +import static com.github.javaparser.Range.range; import static com.github.javaparser.StaticJavaParser.parseImport; import static com.github.javaparser.StaticJavaParser.parseName; import static com.github.javaparser.ast.Modifier.createModifierList; import static com.github.javaparser.utils.CodeGenerationUtils.subtractPaths; import static com.github.javaparser.utils.Utils.assertNotNull; -import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.Generated; -import com.github.javaparser.TokenRange; /** *

@@ -564,6 +564,11 @@ public CompilationUnit setStorage(Path path) { return this; } + public CompilationUnit setStorage(Path path, Charset charset) { + this.storage = new Storage(this, path, charset); + return this; + } + /** * Create (or overwrite) a module declaration in this compilation unit with name "name". * @@ -606,9 +611,18 @@ public static class Storage { private final Path path; + private final Charset encoding; + private Storage(CompilationUnit compilationUnit, Path path) { this.compilationUnit = compilationUnit; this.path = path.toAbsolutePath(); + this.encoding = UTF8; + } + + private Storage(CompilationUnit compilationUnit, Path path, Charset encoding) { + this.compilationUnit = compilationUnit; + this.path = path.toAbsolutePath(); + this.encoding = encoding; } /** @@ -651,16 +665,27 @@ public void save() { } /** - * Saves a compilation unit to its original location with formatting according to the function - * passed as a parameter. + * Saves a compilation unit to its original location with formatting according to the function passed as a + * parameter. * * @param makeOutput a function that formats the compilation unit */ public void save(Function makeOutput) { + save(makeOutput, encoding); + } + + /** + * Saves a compilation unit to its original location with formatting and encoding according to the function and + * encoding passed as a parameter. + * + * @param makeOutput a function that formats the compilation unit + * @param encoding the encoding to use for the saved file + */ + public void save(Function makeOutput, Charset encoding) { try { Files.createDirectories(path.getParent()); final String code = makeOutput.apply(getCompilationUnit()); - Files.write(path, code.getBytes(UTF8)); + Files.write(path, code.getBytes(encoding)); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java index 7dd6f104ba..030349f94f 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java +++ b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java @@ -8,6 +8,7 @@ import com.github.javaparser.printer.PrettyPrinter; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -102,7 +103,7 @@ public ParseResult tryToParse(String startPackage, String filen Log.trace("Parsing %s", () -> path); final ParseResult result = new JavaParser(configuration) .parse(COMPILATION_UNIT, provider(path)); - result.getResult().ifPresent(cu -> cu.setStorage(path)); + result.getResult().ifPresent(cu -> cu.setStorage(path, configuration.getCharacterEncoding())); cache.put(relativePath, result); return result; } @@ -246,7 +247,7 @@ private FileVisitResult callback(Path absolutePath, ParserConfiguration configur Path localPath = root.relativize(absolutePath); Log.trace("Parsing %s", () -> localPath); ParseResult result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(absolutePath)); - result.getResult().ifPresent(cu -> cu.setStorage(absolutePath)); + result.getResult().ifPresent(cu -> cu.setStorage(absolutePath, configuration.getCharacterEncoding())); switch (callback.process(localPath, absolutePath, result)) { case SAVE: result.getResult().ifPresent(cu -> save(cu, absolutePath)); @@ -426,31 +427,53 @@ public SourceRoot add(CompilationUnit compilationUnit) { /** * Save the given compilation unit to the given path. + * @param cu the compilation unit + * @param path the path of the java file */ private SourceRoot save(CompilationUnit cu, Path path) { + return save(cu, path, parserConfiguration.getCharacterEncoding()); + } + + /** + * Save the given compilation unit to the given path. + * @param cu the compilation unit + * @param path the path of the java file + * @param encoding the encoding to use while saving the file + */ + private SourceRoot save(CompilationUnit cu, Path path, Charset encoding) { assertNotNull(cu); assertNotNull(path); - cu.setStorage(path); + cu.setStorage(path, encoding); cu.getStorage().get().save(printer); return this; } /** * Save all previously parsed files back to a new path. + * @param root the root of the java packages + * @param encoding the encoding to use while saving the file */ - public SourceRoot saveAll(Path root) { + public SourceRoot saveAll(Path root, Charset encoding) { assertNotNull(root); Log.info("Saving all files (%s) to %s", cache::size, () -> root); for (Map.Entry> cu : cache.entrySet()) { final Path path = root.resolve(cu.getKey()); if (cu.getValue().getResult().isPresent()) { Log.trace("Saving %s", () -> path); - save(cu.getValue().getResult().get(), path); + save(cu.getValue().getResult().get(), path, encoding); } } return this; } + /** + * Save all previously parsed files back to a new path. + * @param root the root of the java packages + */ + public SourceRoot saveAll(Path root) { + return saveAll(root, parserConfiguration.getCharacterEncoding()); + } + /** * Save all previously parsed files back to where they were found. */ @@ -458,6 +481,14 @@ public SourceRoot saveAll() { return saveAll(root); } + /** + * Save all previously parsed files back to where they were found, with the given encoding. + * @param encoding the encoding to use. + */ + public SourceRoot saveAll(Charset encoding) { + return saveAll(root, encoding); + } + /** * The Java files that have been parsed by this source root object, or have been added manually. */