-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
FileRules
Refaster rule collection
- Loading branch information
1 parent
48e2f57
commit 33920a3
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...e-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |