Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Path.toFile() #1158

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace Path.toFile()
In some use cases it's preferable to have for example the nanorc configs inside a jar. Although a lot of the code base use `java.nio.file.Path`, a couple of places still call `Path.toFile()`, which isn't supported for NIO's ZIP file system, which can be created for a `jar:` URL, so that the contents of the jar can be used as a (read only) file system.

This PR replaces occurences of `Path.toFile()` and replaces the `File` operations with NIO's `Files`.
snazy committed Jan 20, 2025
commit 1c6ef59948a34bbf359ed57a2844c0d943fbe5a0
11 changes: 6 additions & 5 deletions builtins/src/main/java/org/jline/builtins/ConfigurationPath.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
package org.jline.builtins;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigurationPath {
@@ -33,9 +34,9 @@ public ConfigurationPath(Path appConfig, Path userConfig) {
*/
public Path getConfig(String name) {
Path out = null;
if (userConfig != null && userConfig.resolve(name).toFile().exists()) {
if (userConfig != null && Files.exists(userConfig.resolve(name))) {
out = userConfig.resolve(name);
} else if (appConfig != null && appConfig.resolve(name).toFile().exists()) {
} else if (appConfig != null && Files.exists(appConfig.resolve(name))) {
out = appConfig.resolve(name);
}
return out;
@@ -62,10 +63,10 @@ public Path getUserConfig(String name) throws IOException {
public Path getUserConfig(String name, boolean create) throws IOException {
Path out = null;
if (userConfig != null) {
if (!userConfig.resolve(name).toFile().exists() && create) {
userConfig.resolve(name).toFile().createNewFile();
if (!Files.exists(userConfig.resolve(name)) && create) {
Files.createFile(userConfig.resolve(name));
}
if (userConfig.resolve(name).toFile().exists()) {
if (Files.exists(userConfig.resolve(name))) {
out = userConfig.resolve(name);
}
}
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
@@ -2105,7 +2105,7 @@ private boolean save(String name) throws IOException {
return false;
}
} else if (!Files.exists(newPath)) {
newPath.toFile().createNewFile();
Files.createFile(newPath);
}
Path t = Files.createTempFile("jline-", ".temp");
try (OutputStream os = Files.newOutputStream(
Original file line number Diff line number Diff line change
@@ -245,7 +245,7 @@ public Map<String, Boolean> scripts() {
}
}
for (Path p : scripts) {
String name = p.toFile().getName();
String name = p.getFileName().toString();
int idx = name.lastIndexOf(".");
out.put(name.substring(0, idx), name.substring(idx + 1).equals(scriptExtension));
}
@@ -395,7 +395,7 @@ public ScriptFile(String command, String cmdLine, String[] args) {
for (String e : scriptExtensions()) {
String file = command + "." + e;
Path path = Paths.get(p, file);
if (path.toFile().exists()) {
if (Files.exists(path)) {
script = path;
scriptExtension(command);
found = true;
@@ -959,7 +959,7 @@ private Object slurpcmd(CommandInput input) {
: engine.getSerializationFormats().get(0);
try {
Path path = Paths.get(arg);
if (path.toFile().exists()) {
if (Files.exists(path)) {
if (!format.equals(SLURP_FORMAT_TEXT)) {
out = slurp(path, encoding, format);
} else {
34 changes: 13 additions & 21 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
@@ -1363,7 +1363,7 @@ protected Map<String, Object> readAttributes(Path path) {
}
}
attrs.computeIfAbsent("isExecutable", s -> Files.isExecutable(path));
attrs.computeIfAbsent("permissions", s -> getPermissionsFromFile(path.toFile()));
attrs.computeIfAbsent("permissions", s -> getPermissionsFromFile(path));
return attrs;
}
}
@@ -2138,27 +2138,19 @@ private static boolean isWindowsExecutable(String fileName) {
* the file is readable/writable/executable. If so, then <U>all</U> the
* relevant permissions are set (i.e., owner, group and others)
*/
private static Set<PosixFilePermission> getPermissionsFromFile(File f) {
Set<PosixFilePermission> perms = EnumSet.noneOf(PosixFilePermission.class);
if (f.canRead()) {
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.OTHERS_READ);
}

if (f.canWrite()) {
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.OTHERS_WRITE);
}

if (f.canExecute() || (OSUtils.IS_WINDOWS && isWindowsExecutable(f.getName()))) {
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
private static Set<PosixFilePermission> getPermissionsFromFile(Path f) {
try {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(f);
if (OSUtils.IS_WINDOWS && isWindowsExecutable(f.getFileName().toString())) {
perms = new HashSet<>(perms);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
}
return perms;
} catch (IOException e) {
throw new RuntimeException(e);
}

return perms;
}

public static Map<String, String> getLsColorMap(CommandSession session) {
5 changes: 3 additions & 2 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ package org.jline.groovy
import org.codehaus.groovy.runtime.HandleMetaClass
import org.codehaus.groovy.runtime.typehandling.GroovyCastException

import java.nio.file.Files
import java.nio.file.Path
import org.jline.script.GroovyEngine.Format
import groovy.json.JsonOutput
@@ -62,9 +63,9 @@ class Utils {

static void persist(Path file, Object object, Format format) {
if (format == Format.JSON) {
file.toFile().write(JsonOutput.toJson(object))
Files.writeString(file, JsonOutput.toJson(object))
} else if (format == Format.NONE) {
file.toFile().write(toString(object))
Files.writeString(file, toString(object))
} else {
throw new IllegalArgumentException()
}
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ public void purge() throws IOException {
public void write(Path file, boolean incremental) throws IOException {
Path path = file != null ? file : getPath();
if (path != null && Files.exists(path)) {
path.toFile().delete();
Files.deleteIfExists(path);
}
internalWrite(path, incremental ? getLastLoaded(path) : 0);
}