From 879ba3a5648a7f723207edfea1020a581b9392a4 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 30 Apr 2024 06:19:22 +0200 Subject: [PATCH] Introduce temporary file creation Refaster rules --- .../errorprone/refasterrules/FileRules.java | 32 +++++++++++++++++++ .../refasterrules/FileRulesTestInput.java | 9 ++++++ .../refasterrules/FileRulesTestOutput.java | 9 ++++++ 3 files changed, 50 insertions(+) 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 index bc230e0ae59..647d4015a2e 100644 --- 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 @@ -4,10 +4,12 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.FileAttribute; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** Refaster rules related to expressions dealing with files. */ @@ -40,4 +42,34 @@ String after(Path path) throws IOException { return Files.readString(path); } } + + /** Prefer {@link File#createTempFile(String, String)} over more verbose alternatives. */ + static final class FileCreateTempFile { + @BeforeTemplate + File before(String prefix, String suffix) throws IOException { + return File.createTempFile(prefix, suffix, null); + } + + @AfterTemplate + File after(String prefix, String suffix) throws IOException { + return File.createTempFile(prefix, suffix); + } + } + + /** + * Prefer {@link Files#createTempFile(String, String, FileAttribute[])} over {@link + * File#createTempFile(String, String)}, as the former creates files with more restrictive + * permissions. + */ + static final class FilesCreateTempFileToFile { + @BeforeTemplate + File before(String prefix, String suffix) throws IOException { + return File.createTempFile(prefix, suffix); + } + + @AfterTemplate + File after(String prefix, String suffix) throws IOException { + return Files.createTempFile(prefix, suffix).toFile(); + } + } } 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 index c33f3e679fc..110a73b6d40 100644 --- 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 @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refasterrules; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -14,4 +15,12 @@ String testFilesReadStringWithCharset() throws IOException { String testFilesReadString() throws IOException { return Files.readString(Paths.get("foo"), StandardCharsets.UTF_8); } + + File testFileCreateTempFile() throws IOException { + return File.createTempFile("foo", "bar", null); + } + + File testFilesCreateTempFileToFile() throws IOException { + return File.createTempFile("foo", "bar"); + } } 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 index 5b516e31f9e..65237e881c3 100644 --- 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 @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refasterrules; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -14,4 +15,12 @@ String testFilesReadStringWithCharset() throws IOException { String testFilesReadString() throws IOException { return Files.readString(Paths.get("foo")); } + + File testFileCreateTempFile() throws IOException { + return File.createTempFile("foo", "bar"); + } + + File testFilesCreateTempFileToFile() throws IOException { + return Files.createTempFile("foo", "bar").toFile(); + } }