From 2fbc25ba5ce85c2df692928a5bd2f8e5b5ca0bd8 Mon Sep 17 00:00:00 2001 From: Ashley <73482956+ascopes@users.noreply.github.com> Date: Sat, 29 Oct 2022 10:43:11 +0100 Subject: [PATCH] Update relative path semantics in PathFileObject --- .../ascopes/jct/compilers/PathFileObject.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/PathFileObject.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/PathFileObject.java index 2404df7a8..38982ecc0 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/PathFileObject.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/PathFileObject.java @@ -66,7 +66,7 @@ public class PathFileObject implements JavaFileObject { private static final long NOT_MODIFIED = 0L; private final Location location; - private final Path root; + private final Path rootPath; private final Path relativePath; private final Path fullPath; private final String name; @@ -76,14 +76,28 @@ public class PathFileObject implements JavaFileObject { /** * Initialize this file object. * - * @param root the root directory that the path is a package within. - * @param relativePath the path to point to. + * @param rootPath the root directory that the path is a package within. + * @param relativePath the path to point to, relative to the root. */ - public PathFileObject(Location location, Path root, Path relativePath) { - this.location = requireNonNull(location, "location"); - this.root = requireNonNull(root, "root"); - this.relativePath = root.relativize(requireNonNull(relativePath, "relativePath")); - fullPath = root.resolve(relativePath); + public PathFileObject(Location location, Path rootPath, Path relativePath) { + requireNonNull(location, "location"); + requireNonNull(rootPath, "rootPath"); + requireNonNull(relativePath, "relativePath"); + + if (!rootPath.isAbsolute()) { + throw new IllegalArgumentException("rootPath must be absolute"); + } + + this.location = location; + this.rootPath = rootPath; + + if (relativePath.isAbsolute()) { + this.relativePath = rootPath.relativize(relativePath); + } else { + this.relativePath = relativePath; + } + + fullPath = rootPath.resolve(relativePath); name = relativePath.toString(); uri = fullPath.toUri(); kind = FileUtils.pathToKind(relativePath); @@ -150,7 +164,7 @@ public Location getLocation() { * @return the root path. */ public Path getRoot() { - return root; + return rootPath; } @Override