From 0dd6ca541630d58236b4e9aaa9696e24f3d3999a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 26 Aug 2023 15:54:40 +0300 Subject: [PATCH] Introduce `FileRules` Refaster rule collection --- .../errorprone/refasterrules/FileRules.java | 43 +++++++++++++++++++ .../refasterrules/RefasterRulesTest.java | 1 + .../refasterrules/FileRulesTestInput.java | 17 ++++++++ .../refasterrules/FileRulesTestOutput.java | 17 ++++++++ 4 files changed, 78 insertions(+) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java new file mode 100644 index 0000000000..bc230e0ae5 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java @@ -0,0 +1,43 @@ +package tech.picnic.errorprone.refasterrules; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to expressions dealing with files. */ +@OnlineDocumentation +final class FileRules { + private FileRules() {} + + /** Prefer {@link Files#readString(Path, Charset)} over more contrived alternatives. */ + static final class FilesReadStringWithCharset { + @BeforeTemplate + String before(Path path, Charset charset) throws IOException { + return new String(Files.readAllBytes(path), charset); + } + + @AfterTemplate + String after(Path path, Charset charset) throws IOException { + return Files.readString(path, charset); + } + } + + /** Prefer {@link Files#readString(Path)} over more verbose alternatives. */ + static final class FilesReadString { + @BeforeTemplate + String before(Path path) throws IOException { + return Files.readString(path, UTF_8); + } + + @AfterTemplate + String after(Path path) throws IOException { + return Files.readString(path); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index 0dc5d22049..6a67c37b7f 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -40,6 +40,7 @@ final class RefasterRulesTest { ComparatorRules.class, DoubleStreamRules.class, EqualityRules.class, + FileRules.class, ImmutableListRules.class, ImmutableListMultimapRules.class, ImmutableMapRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java new file mode 100644 index 0000000000..c33f3e679f --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refasterrules; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class FileRulesTest implements RefasterRuleCollectionTestCase { + String testFilesReadStringWithCharset() throws IOException { + return new String(Files.readAllBytes(Paths.get("foo")), StandardCharsets.ISO_8859_1); + } + + String testFilesReadString() throws IOException { + return Files.readString(Paths.get("foo"), StandardCharsets.UTF_8); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java new file mode 100644 index 0000000000..5b516e31f9 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refasterrules; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class FileRulesTest implements RefasterRuleCollectionTestCase { + String testFilesReadStringWithCharset() throws IOException { + return Files.readString(Paths.get("foo"), StandardCharsets.ISO_8859_1); + } + + String testFilesReadString() throws IOException { + return Files.readString(Paths.get("foo")); + } +}