diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/document/DocumentFile.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/document/DocumentFile.java index 73bdaac161..99776ede44 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/document/DocumentFile.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/document/DocumentFile.java @@ -4,15 +4,16 @@ package net.sourceforge.pmd.util.document; +import static java.util.Objects.requireNonNull; + import java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.Writer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -35,10 +36,11 @@ public class DocumentFile implements Document, Closeable { private int currentPosition = 0; private final Path temporaryPath = Files.createTempFile("pmd-", ".tmp"); - private final Writer writer = Files.newBufferedWriter(temporaryPath, StandardCharsets.UTF_8); + private final Writer writer; - public DocumentFile(final File file) throws IOException { - reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8); + public DocumentFile(final File file, final Charset charset) throws IOException { + reader = Files.newBufferedReader(requireNonNull(file).toPath(), requireNonNull(charset)); + writer = Files.newBufferedWriter(temporaryPath, charset); this.filePath = file.toPath(); mapLinesToOffsets(); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentFileTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentFileTest.java index 69ca783d6e..657639434b 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentFileTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentFileTest.java @@ -38,7 +38,7 @@ public class DocumentFileTest { public void insertAtStartOfTheFileShouldSucceed() throws IOException { writeContentToTemporaryFile("static void main(String[] args) {}"); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, 0, "public "); } @@ -85,7 +85,7 @@ public class DocumentFileTest { public void insertVariousTokensIntoTheFileShouldSucceed() throws IOException { writeContentToTemporaryFile("static void main(String[] args) {}"); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, 0, "public "); documentFile.insert(0, 17, "final "); } @@ -101,7 +101,7 @@ public class DocumentFileTest { final String code = "public static void main(String[] args)"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, code.length(), "{}"); } @@ -116,7 +116,7 @@ public class DocumentFileTest { final String code = "public static void main(final String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.delete(new RegionByLineImp(0, 0, 24, 30)); } @@ -131,7 +131,7 @@ public class DocumentFileTest { final String code = "static void main(final String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, 0, "public "); documentFile.delete(new RegionByLineImp(0, 0, 17, 23)); } @@ -147,7 +147,7 @@ public class DocumentFileTest { final String code = "void main(String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, 0, "public "); documentFile.insert(0, 0, "static "); documentFile.delete(new RegionByLineImp(0, 0, 0, 4)); @@ -166,7 +166,7 @@ public class DocumentFileTest { final String code = "int main(String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.replace(new RegionByLineImp(0, 0, 0, 3), "void"); } @@ -181,7 +181,7 @@ public class DocumentFileTest { final String code = "int main(String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.replace(new RegionByLineImp(0, 0, 0, "int".length()), "void"); documentFile.replace(new RegionByLineImp(0, 0, 4, 4 + "main".length()), "foo"); documentFile.replace(new RegionByLineImp(0, 0, 9, 9 + "String".length()), "CharSequence"); @@ -198,7 +198,7 @@ public class DocumentFileTest { final String code = "static int main(CharSequence[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { documentFile.insert(0, 0, "public"); documentFile.delete(new RegionByLineImp(0, 0, 0, 6)); documentFile.replace(new RegionByLineImp(0, 0, 7, 7 + "int".length()), "void"); @@ -224,7 +224,7 @@ public class DocumentFileTest { expectedLineToOffset.add(40); expectedLineToOffset.add(49); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { assertEquals(expectedLineToOffset, documentFile.getLineToOffset()); } } @@ -241,7 +241,7 @@ public class DocumentFileTest { expectedLineToOffset.add(41); expectedLineToOffset.add(51); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { assertEquals(expectedLineToOffset, documentFile.getLineToOffset()); } } @@ -258,7 +258,7 @@ public class DocumentFileTest { expectedLineToOffset.add(41); expectedLineToOffset.add(50); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { assertEquals(expectedLineToOffset, documentFile.getLineToOffset()); } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest.java index 8c1d91522e..b3504cecb8 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/util/document/DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest.java @@ -10,6 +10,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; @@ -39,7 +40,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe public void insertAtStartOfTheDocumentShouldSucceed() throws IOException { writeContentToTemporaryFile("static void main(String[] args) {}"); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public ")); @@ -89,7 +90,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe public void removeTokenShouldSucceed() throws IOException { writeContentToTemporaryFile("public static void main(String[] args) {}"); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); applier.addDocumentOperation(new DeleteDocumentOperation(0, 0, 7, 13)); @@ -107,7 +108,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe final String code = "static void main(final String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public ")); applier.addDocumentOperation(new DeleteDocumentOperation(0, 0, 17, 23)); @@ -126,7 +127,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe final String code = "void main(String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public ")); @@ -149,7 +150,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe final String code = "int main(String[] args) {}"; writeContentToTemporaryFile(code); - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); applier.addDocumentOperation(new ReplaceDocumentOperation(0, 0, 0, "int".length(), "void")); @@ -182,7 +183,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe } private void shuffleAndApplyOperations(List documentOperations) throws IOException { - try (DocumentFile documentFile = new DocumentFile(temporaryFile)) { + try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) { applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile); Collections.shuffle(documentOperations);