Skip to content

Commit

Permalink
replace Guava Files.{write/append} by java.nio.Files.write(..)
Browse files Browse the repository at this point in the history
The Guava methods are meanwhile deprecated and the standard Java API is almost as convenient anyway.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Oct 1, 2022
1 parent 775ef2a commit 506030b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
Expand All @@ -27,7 +28,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.io.Files;
import com.tngtech.archunit.ArchConfiguration;
import com.tngtech.archunit.base.MayResolveTypesViaReflection;
import com.tngtech.archunit.lang.ArchRule;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void save(ArchRule rule, List<String> violations) {
private void write(List<String> violations, File ruleDetails) {
String updatedViolations = Joiner.on("\n").join(escape(violations));
try {
Files.write(updatedViolations, ruleDetails, UTF_8);
Files.write(ruleDetails.toPath(), updatedViolations.getBytes(UTF_8));
} catch (IOException e) {
throw new StoreUpdateFailedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.io.File;
import java.io.IOException;

import com.google.common.io.Files;
import java.nio.file.Files;

import static com.google.common.base.Preconditions.checkState;
import static com.tngtech.archunit.testutil.TestUtils.newTemporaryFolder;
Expand All @@ -26,7 +25,7 @@ public TestClassFile create() {

checkState(sourceFile.getParentFile().exists() || sourceFile.getParentFile().mkdirs(),
"Can't create directory %s", sourceFile.getParentFile().getAbsolutePath());
Files.write(sourceCode, sourceFile, UTF_8);
Files.write(sourceFile.toPath(), sourceCode.getBytes(UTF_8));

int result = getSystemJavaCompiler().run(null, null, null, sourceFile.getAbsolutePath());
checkState(result == 0, "Compiler exit code should be 0, but it was " + result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
Expand All @@ -13,12 +14,13 @@
import org.junit.rules.ExternalResource;

import static com.google.common.base.Preconditions.checkState;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.WRITE;

public class ReplaceFileRule extends ExternalResource {
private final File tempDir = TestUtils.newTemporaryFolder();

private final List<FileAction> fileActions = new ArrayList<>();
private final List<MoveAction> moveActions = new ArrayList<>();
private final Set<File> replacedFiles = new HashSet<>();

public void replace(File target, String content, Charset charset) {
Expand Down Expand Up @@ -56,15 +58,14 @@ private void write(File target, String content, Charset charset) {

private void append(File file, String line, Charset charset) {
try {
com.google.common.io.Files.append(System.lineSeparator() + line, file, charset);
Files.write(file.toPath(), (System.lineSeparator() + line).getBytes(charset), WRITE, APPEND);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void addMoveAction(MoveAction moveAction) {
fileActions.add(moveAction);
moveActions.add(moveAction);
}

@Override
Expand Down Expand Up @@ -102,7 +103,7 @@ public void revert() {

private void move(File origin, File target) {
try {
java.nio.file.Files.move(origin.toPath(), target.toPath());
Files.move(origin.toPath(), target.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -142,7 +143,7 @@ private CreateFileAction(File target, String content, Charset charset) {
@Override
public FileAction execute() {
try {
com.google.common.io.Files.write(content, target, charset);
Files.write(target.toPath(), content.getBytes(charset));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 506030b

Please sign in to comment.