Skip to content

Commit

Permalink
Merge pull request #43093 from Shadow-Devil/replace-paths-get-with-pa…
Browse files Browse the repository at this point in the history
…th-of

[Refactoring] Replace `Paths.get` with `Path.of`
  • Loading branch information
gimantha authored Oct 4, 2024
2 parents 50fe1c5 + c61fc38 commit ca94ad5
Show file tree
Hide file tree
Showing 361 changed files with 1,120 additions and 1,433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -54,7 +54,7 @@ public String getDescription(String topic) throws HelpProviderException {
private static String readFileAsString(String file) throws HelpProviderException {
String content;
try {
content = Files.readString(Paths.get(file));
content = Files.readString(Path.of(file));
} catch (IOException e) {
throw new HelpProviderException("Error occurred while executing the command");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -88,7 +88,7 @@ public List<String> getTopicList() {
private String readFileAsString(String file) {
String content = null;
try {
content = Files.readString(Paths.get(file));
content = Files.readString(Path.of(file));
} catch (IOException e) {
addDebugDiagnostic("Error loading the file : " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
Expand Down Expand Up @@ -166,7 +166,7 @@ public String getBufferFileUri() throws IOException {
@Override
public void evaluateDeclarationFile(String filePath) throws BallerinaShellException {
try {
String statements = Files.readString(Paths.get(filePath), Charset.defaultCharset());
String statements = Files.readString(Path.of(filePath), Charset.defaultCharset());
Collection<Node> nodes = treeParser.parseDeclarations(statements);
Collection<Snippet> snippets = snippetFactory.createSnippets(nodes);
getValue(Optional.ofNullable(invoker.getCompilation(snippets)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -81,11 +80,11 @@ private void addShutdownHookAndCleanup() {
try {
long profilerTotalTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS) -
profilerStartTime;
Files.deleteIfExists(Paths.get(Constants.TEMP_JAR_FILE_NAME));
Files.deleteIfExists(Path.of(Constants.TEMP_JAR_FILE_NAME));
OUT_STREAM.printf("%s[6/6] Generating output...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
JsonParser jsonParser = new JsonParser();
HttpServer httpServer = new HttpServer();
String cpuFilePath = Paths.get(currentDir, CPU_PRE_JSON).toString();
String cpuFilePath = Path.of(currentDir, CPU_PRE_JSON).toString();
jsonParser.initializeCPUParser(cpuFilePath);
deleteFileIfExists(cpuFilePath);
OUT_STREAM.printf(" Execution time: %d seconds %n", profilerTotalTime / 1000);
Expand All @@ -104,7 +103,7 @@ private void deleteFileIfExists(String filePath) {
return;
}
try {
Files.delete(Paths.get(filePath));
Files.delete(Path.of(filePath));
} catch (IOException e) {
throw new ProfilerException("Failed to delete file: " + filePath + "%n", e);
}
Expand Down Expand Up @@ -173,7 +172,7 @@ private void addToUsedArgs(String[] args, List<String> usedArgs, int i) {
private void extractProfiler() throws ProfilerException {
OUT_STREAM.printf("%s[1/6] Initializing...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
try {
Path profilerRuntimePath = Paths.get("io/ballerina/runtime/profiler/runtime");
Path profilerRuntimePath = Path.of("io/ballerina/runtime/profiler/runtime");
new ProcessBuilder("jar", "xvf", "Profiler.jar", profilerRuntimePath.toString()).start().waitFor();
} catch (IOException | InterruptedException exception) {
throw new ProfilerException(exception);
Expand All @@ -183,8 +182,8 @@ private void extractProfiler() throws ProfilerException {
private void createTempJar() {
try {
OUT_STREAM.printf("%s[2/6] Copying executable...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
Path sourcePath = Paths.get(balJarName);
Path destinationPath = Paths.get(Constants.TEMP_JAR_FILE_NAME);
Path sourcePath = Path.of(balJarName);
Path destinationPath = Path.of(Constants.TEMP_JAR_FILE_NAME);
Files.copy(sourcePath, destinationPath);
} catch (IOException e) {
throw new ProfilerException("Error occurred while copying the file: " + balJarName, e);
Expand Down Expand Up @@ -241,7 +240,7 @@ private void modifyJar() throws InterruptedException, IOException {
for (String instrumentedFilePath : instrumentedPaths) {
FileUtils.deleteDirectory(new File(instrumentedFilePath));
}
Path filePath = Paths.get("io/ballerina/runtime/profiler/runtime");
Path filePath = Path.of("io/ballerina/runtime/profiler/runtime");
FileUtils.deleteDirectory(new File(filePath.toString()));
profilerMethodWrapper.invokeMethods(profilerDebugArg);
}
Expand All @@ -258,7 +257,7 @@ private void loadDirectories(List<String> changedDirs) {
}

private void listAllFiles(final File userDirectory) {
String absolutePath = Paths.get(Constants.TEMP_JAR_FILE_NAME).toFile().getAbsolutePath();
String absolutePath = Path.of(Constants.TEMP_JAR_FILE_NAME).toFile().getAbsolutePath();
analyseInstrumentedDirectories(userDirectory, absolutePath.replaceAll(Constants.TEMP_JAR_FILE_NAME, ""));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void invokeMethods(String debugArg) throws IOException, InterruptedExcept
if (debugArg != null) {
commands.add(debugArg);
}
commands.add(Paths.get(System.getProperty(USER_DIR), Constants.TEMP_JAR_FILE_NAME).toString());
commands.add(Path.of(System.getProperty(USER_DIR), Constants.TEMP_JAR_FILE_NAME).toString());
if (balJarArgs != null) {
commands.add(balJarArgs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public final class FileUtils {

Expand All @@ -34,7 +33,7 @@ private FileUtils() {
}

static String readFileAsString(String file) throws IOException {
Path path = Paths.get(file);
Path path = Path.of(file);
int count = 0;
while (!Files.exists(path)) {
if (count++ > MAX_WAIT_TIME_FOR_FILE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.ballerina.runtime.profiler.util.Constants.BALLERINA_HOME;
import static io.ballerina.runtime.profiler.util.Constants.HTML_TEMPLATE_FILE;
Expand All @@ -52,7 +51,7 @@ String getSiteData(String contents) {
}

public String readFileAsString() throws IOException {
Path resourceFilePath = Paths.get(System.getenv(BALLERINA_HOME)).resolve("resources")
Path resourceFilePath = Path.of(System.getenv(BALLERINA_HOME)).resolve("resources")
.resolve("profiler").resolve(HTML_TEMPLATE_FILE);
if (!Files.exists(resourceFilePath)) {
throw new ProfilerRuntimeException("resource file not found: " + HTML_TEMPLATE_FILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
Expand All @@ -52,7 +51,7 @@ public void initializeHTMLExport() throws IOException {
String profilerOutputDir = System.getProperty(WORKING_DIRECTORY);
OUT_STREAM.printf(" Output: " + Constants.ANSI_YELLOW + "%s" + File.separator + HTML_PROFILER_REPORT +
Constants.ANSI_RESET + "%n", profilerOutputDir);
Path resourcePath = Paths.get(System.getenv(BALLERINA_HOME)).resolve("resources")
Path resourcePath = Path.of(System.getenv(BALLERINA_HOME)).resolve("resources")
.resolve("profiler");

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.ballerina.runtime.internal.util.RuntimeUtils;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Constants used by toml parser.
Expand All @@ -36,7 +35,7 @@ public final class TomlConstants {
public static final String CONFIG_DATA_ENV_VARIABLE = "BAL_CONFIG_DATA";
public static final String MODULES_ROOT = "modules";
public static final String TEST_DIR_NAME = "tests";
public static final Path DEFAULT_CONFIG_PATH = Paths.get(RuntimeUtils.USER_DIR, CONFIG_FILE_NAME);
public static final Path DEFAULT_CONFIG_PATH = Path.of(RuntimeUtils.USER_DIR, CONFIG_FILE_NAME);

private TomlConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -147,7 +146,7 @@ private static String populateConfigDetails(List<Path> paths, Map<String, String
if (envVars.containsKey(CONFIG_FILES_ENV_VARIABLE)) {
String[] configPathList = envVars.get(CONFIG_FILES_ENV_VARIABLE).split(File.pathSeparator);
for (String pathString : configPathList) {
paths.add(Paths.get(pathString));
paths.add(Path.of(pathString));
}
} else if (envVars.containsKey(CONFIG_DATA_ENV_VARIABLE)) {
return envVars.get(CONFIG_DATA_ENV_VARIABLE);
Expand All @@ -161,7 +160,7 @@ private static String populateConfigDetails(List<Path> paths, Map<String, String

public static ConfigDetails getTestConfigPaths(Module module, String pkgName, String sourceRoot) {
String moduleName = module.getName();
Path testConfigPath = Paths.get(sourceRoot);
Path testConfigPath = Path.of(sourceRoot);
if (!Files.exists(testConfigPath)) {
testConfigPath = getSourceRootInContainer();
}
Expand All @@ -179,6 +178,6 @@ public static ConfigDetails getTestConfigPaths(Module module, String pkgName, St

private static Path getSourceRootInContainer() {
// Since we are inside a docker container, it's current working directory is the source root.
return Paths.get(RuntimeUtils.USER_DIR);
return Path.of(RuntimeUtils.USER_DIR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -136,14 +135,14 @@ public static TransactionResourceManager getInstance() {
*
*/
private void setLogProperties() {
final Path projectRoot = Paths.get(RuntimeUtils.USER_DIR);
final Path projectRoot = Path.of(RuntimeUtils.USER_DIR);
if (projectRoot != null) {
String logDir = getTransactionLogDirectory();
Path logDirPath = Paths.get(logDir);
Path logDirPath = Path.of(logDir);
Path transactionLogDirectory;
if (!logDirPath.isAbsolute()) {
logDir = projectRoot.toAbsolutePath().toString() + File.separatorChar + logDir;
transactionLogDirectory = Paths.get(logDir);
transactionLogDirectory = Path.of(logDir);
} else {
transactionLogDirectory = logDirPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.ballerina.runtime.internal.util.RuntimeUtils;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Utils class for runtime unit tests.
Expand All @@ -37,11 +36,11 @@ private TestUtils() {
}

public static Path getConfigPath(String configFileName) {
return Paths.get(RuntimeUtils.USER_DIR, "src", "test", "resources", "config_files", configFileName);
return Path.of(RuntimeUtils.USER_DIR, "src", "test", "resources", "config_files", configFileName);
}

public static Path getConfigPathForNegativeCases(String configFileName) {
return Paths.get(RuntimeUtils.USER_DIR, "src", "test", "resources", "config_files", "negative", configFileName);
return Path.of(RuntimeUtils.USER_DIR, "src", "test", "resources", "config_files", "negative", configFileName);
}

public static VariableKey[] getSimpleVariableKeys(Module module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -59,7 +58,7 @@ public class AddCommand implements BLauncherCmd {
private String template = "lib";

public AddCommand() {
this.userDir = Paths.get(System.getProperty("user.dir"));
this.userDir = Path.of(System.getProperty("user.dir"));
this.errStream = System.err;
this.exitWhenFinish = true;
CommandUtil.initJarFs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.ballerina.cli.cmd.Constants.BUILD_COMMAND;
import static io.ballerina.projects.util.ProjectUtils.isProjectUpdated;
Expand All @@ -57,7 +56,7 @@ public class BuildCommand implements BLauncherCmd {
private final boolean exitWhenFinish;

public BuildCommand() {
this.projectPath = Paths.get(System.getProperty(ProjectConstants.USER_DIR));
this.projectPath = Path.of(System.getProperty(ProjectConstants.USER_DIR));
this.outStream = System.out;
this.errStream = System.err;
this.exitWhenFinish = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.ballerina.cli.cmd.Constants.CLEAN_COMMAND;

Expand Down Expand Up @@ -57,7 +56,7 @@ public CleanCommand(Path projectPath, boolean exitWhenFinish) {
}

public CleanCommand() {
this.projectPath = Paths.get(System.getProperty(ProjectConstants.USER_DIR));
this.projectPath = Path.of(System.getProperty(ProjectConstants.USER_DIR));
this.outStream = System.out;
this.exitWhenFinish = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
Expand Down Expand Up @@ -384,7 +383,7 @@ private static Path updateModuleDirectoryNaming(Path includePath, Path balaPath,
String destinationDirName = moduleDirName.split(templatePkgName + ProjectConstants.DOT, 2)[1];
Path includePathRelativeToModuleRoot = modulesDirPath.resolve(moduleRootPath)
.relativize(absoluteIncludePath);
return Paths.get(ProjectConstants.MODULES_ROOT).resolve(destinationDirName)
return Path.of(ProjectConstants.MODULES_ROOT).resolve(destinationDirName)
.resolve(includePathRelativeToModuleRoot);
}
return includePath;
Expand Down Expand Up @@ -516,8 +515,8 @@ public static void writeBallerinaToml(Path balTomlPath, PackageJson packageJson,
JsonObject dependenciesObj = (JsonObject) dependencies;
if (null == dependenciesObj.get("scope")) {
String libPath = dependenciesObj.get("path").getAsString();
Path libName = Optional.of(Paths.get(libPath).getFileName()).get();
Path libRelPath = Paths.get("libs", libName.toString());
Path libName = Optional.of(Path.of(libPath).getFileName()).get();
Path libRelPath = Path.of("libs", libName.toString());
Files.writeString(balTomlPath, "\npath = \"" + libRelPath + "\"", StandardOpenOption.APPEND);
}

Expand Down Expand Up @@ -912,7 +911,7 @@ private static Path getTemplatePath() throws URISyntaxException {
final String[] array = uri.toString().split("!");
return jarFs.getPath(array[1]);
} else {
return Paths.get(uri);
return Path.of(uri);
}
}

Expand Down
Loading

0 comments on commit ca94ad5

Please sign in to comment.