Skip to content

Commit

Permalink
Added model classes for java.nio.file and java.nio.charset packages #429
Browse files Browse the repository at this point in the history
 (#506)

* Found Potential Fix for #429

Seems like there needs to be model classes in the file and charset packages for java.nio. These are as small as I could make them

* Create NullCharsetTest.java

Developed UnitTest to address the error from #429

* Update NullCharsetTest.java

Should be "iae" and not "e"

* Fixed Failing Tests for NullCharSet

It turns out my implementation of Charset affected the behavior of underlying Gradle JPF tests. Removed this custom Charset implementation and instead kept four files to fix the root cause:
 - FileSystem.java
 - FileSystems.java
 - Pathy.java
 - spi/FileSystemProvider.java

All other files in the initial commit to PR were redundant.
  • Loading branch information
joalen authored Dec 15, 2024
1 parent 8fe399b commit ca40e6c
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/classes/modules/java.base/java/nio/files/FileSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package java.nio.file;

import java.nio.file.spi.FileSystemProvider;

public abstract class FileSystem {
protected FileSystem() {}

public abstract Path getPath(String first, String... more);

public abstract FileSystemProvider provider();
}
186 changes: 186 additions & 0 deletions src/classes/modules/java.base/java/nio/files/FileSystems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package java.nio.file;

import java.nio.file.spi.FileSystemProvider;
import java.net.URI;
import java.util.Map;
import java.util.HashMap;

public class FileSystems {
private static volatile FileSystem defaultFileSystem;

static {
defaultFileSystem = new FileSystem() {
private final FileSystemProvider provider = new FileSystemProvider() {
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env) {
return this.getFileSystem();
}

@Override
public String getScheme() {
return "file";
}

@Override
public FileSystem getFileSystem() {
return defaultFileSystem;
}
};

@Override
public Path getPath(String first, String... more) {
return new Path() {
private final String path;
private final String[] segments;

{
StringBuilder sb = new StringBuilder(first);
if (more != null) {
for (String s : more) {
if (s != null && !s.isEmpty()) {
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') {
sb.append('/');
}
sb.append(s);
}
}
}
path = sb.toString();
segments = path.split("/");
}

@Override
public FileSystem getFileSystem() {
return defaultFileSystem;
}

@Override
public boolean isAbsolute() {
return path.startsWith("/");
}

@Override
public Path getRoot() {
return isAbsolute() ? getFileSystem().getPath("/") : null;
}

@Override
public Path getFileName() {
if (path.isEmpty()) return null;
return getFileSystem().getPath(segments[segments.length - 1]);
}

@Override
public Path getParent() {
int lastSep = path.lastIndexOf('/');
return lastSep > 0 ? getFileSystem().getPath(path.substring(0, lastSep)) : null;
}

@Override
public int getNameCount() {
if (path.isEmpty()) return 0;
if (path.equals("/")) return 0;
int count = segments.length;
if (path.startsWith("/")) count--;
if (path.endsWith("/")) count--;
return count;
}

@Override
public Path getName(int index) {
if (index < 0 || index >= getNameCount()) {
throw new IllegalArgumentException();
}
return getFileSystem().getPath(segments[path.startsWith("/") ? index + 1 : index]);
}

@Override
public Path subpath(int beginIndex, int endIndex) {
if (beginIndex < 0 || beginIndex >= getNameCount() ||
endIndex > getNameCount() || beginIndex >= endIndex) {
throw new IllegalArgumentException();
}

StringBuilder result = new StringBuilder();
int start = path.startsWith("/") ? beginIndex + 1 : beginIndex;
for (int i = start; i < start + (endIndex - beginIndex); i++) {
if (result.length() > 0) result.append('/');
result.append(segments[i]);
}
return getFileSystem().getPath(result.toString());
}

@Override
public boolean startsWith(Path other) {
return path.startsWith(other.toString());
}

@Override
public boolean endsWith(Path other) {
return path.endsWith(other.toString());
}

@Override
public Path normalize() {
return this; // Simplified implementation
}

@Override
public Path resolve(Path other) {
return getFileSystem().getPath(path + "/" + other.toString());
}

@Override
public Path relativize(Path other) {
return other; // Simplified implementation
}

@Override
public URI toUri() {
try {
return new URI("file", null, path, null);
} catch (Exception e) {
return null;
}
}

@Override
public Path toAbsolutePath() {
if (isAbsolute()) return this;
return getFileSystem().getPath("/" + path);
}

@Override
public Path toRealPath(LinkOption... options) {
return toAbsolutePath();
}

@Override
public String toString() {
return path;
}

@Override
public int compareTo(Path other) {
return path.compareTo(other.toString());
}
};
}

@Override
public FileSystemProvider provider() {
return provider;
}
};
}

public static FileSystem getDefault() {
return defaultFileSystem;
}

public static FileSystem newFileSystem(URI uri, Map<String, ?> env, ClassLoader loader) {
if (uri == null)
throw new NullPointerException();
return defaultFileSystem;
}
}
25 changes: 25 additions & 0 deletions src/classes/modules/java.base/java/nio/files/Path.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package java.nio.file;
import java.net.URI;

public interface Path extends Comparable<Path> {
FileSystem getFileSystem();
boolean isAbsolute();
Path getRoot();
Path getFileName();
Path getParent();
int getNameCount();
Path getName(int index);
Path subpath(int beginIndex, int endIndex);
boolean startsWith(Path other);
boolean endsWith(Path other);
Path normalize();
Path resolve(Path other);
Path relativize(Path other);
URI toUri();
Path toAbsolutePath();
Path toRealPath(LinkOption... options);

static Path of(String first, String... more) {
return FileSystems.getDefault().getPath(first, more);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package java.nio.file.spi;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.AccessMode;
import java.nio.file.Path;
import java.net.URI;
import java.util.Map;


public abstract class FileSystemProvider {
protected FileSystemProvider() {}

public abstract FileSystem newFileSystem(URI uri, Map<String, ?> env);

public abstract String getScheme();

public abstract FileSystem getFileSystem();
}
23 changes: 23 additions & 0 deletions src/tests/NullCharsetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import gov.nasa.jpf.util.test.TestJPF;
import org.junit.Test;
import java.nio.file.Path;

public class NullCharsetTest extends TestJPF
{
@Test
public void testDirectPathEntry()
{
if (verifyNoPropertyViolation())
{
try {
Path.of("/tmp");
} catch (IllegalArgumentException iae)
{
if ("Null charset name".equals(iae.getMessage()))
{
fail("IllegalArgumentException with 'Null charset name' encountered");
}
}
}
}
}

0 comments on commit ca40e6c

Please sign in to comment.