mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce FileRules Refaster rule collection (#767)
This commit is contained in:
committed by
GitHub
parent
43fcbb770d
commit
507a19d10e
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ final class RefasterRulesTest {
|
||||
ComparatorRules.class,
|
||||
DoubleStreamRules.class,
|
||||
EqualityRules.class,
|
||||
FileRules.class,
|
||||
ImmutableListRules.class,
|
||||
ImmutableListMultimapRules.class,
|
||||
ImmutableMapRules.class,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user