Skip to content

Commit e7a6603

Browse files
committed
Add PathFileObject#getAbsolutePath
Create PathFileObject#getAbsolutePath and make it return the same thing as PathFileObject#getFullPath. I have then deprecated PathFileObject#getFullPath as the naming is not completely clear as to what a 'full path' implies. This will be removed in 1.0.0.
1 parent 1c38930 commit e7a6603

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/PathFileObjectAssert.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,29 @@ public AbstractPathAssert<?> relativePath() {
5555
}
5656

5757
/**
58-
* Perform an assertion on the file object's full path.
58+
* Perform an assertion on the file object's absolute path.
5959
*
6060
* @return the assertions for the path.
6161
* @throws AssertionError if the file object is null.
62+
* @deprecated use {@link #absolutePath()} instead.
6263
*/
64+
@Deprecated(forRemoval = true, since = "0.7.3")
65+
@SuppressWarnings("removal")
6366
public AbstractPathAssert<?> fullPath() {
6467
isNotNull();
6568

6669
return assertThat(actual.getFullPath());
6770
}
71+
72+
/**
73+
* Perform an assertion on the file object's absolute path.
74+
*
75+
* @return the assertions for the path.
76+
* @throws AssertionError if the file object is null.
77+
*/
78+
public AbstractPathAssert<?> absolutePath() {
79+
isNotNull();
80+
81+
return assertThat(actual.getAbsolutePath());
82+
}
6883
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/JarContainerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void close() throws IOException {
9090

9191
@Override
9292
public boolean contains(PathFileObject fileObject) {
93-
var path = fileObject.getFullPath();
93+
var path = fileObject.getAbsolutePath();
9494
var root = holder.access().getPathRoot().getPath();
9595
return path.startsWith(root) && Files.isRegularFile(path);
9696
}
@@ -183,7 +183,7 @@ public String inferBinaryName(PathFileObject javaFileObject) {
183183
// we cannot then parse the URI back to a path without removing the `file://` bit first. Since
184184
// we assume we always have instances of PathJavaFileObject here, let's just cast to that and
185185
// get the correct path immediately.
186-
var fullPath = javaFileObject.getFullPath();
186+
var fullPath = javaFileObject.getAbsolutePath();
187187

188188
if (fullPath.startsWith( holder.access().getPathRoot().getPath())) {
189189
return FileUtils.pathToBinaryName(javaFileObject.getRelativePath());

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/PathWrappingContainerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void close() throws IOException {
7474

7575
@Override
7676
public boolean contains(PathFileObject fileObject) {
77-
var path = fileObject.getFullPath();
77+
var path = fileObject.getAbsolutePath();
7878
return path.startsWith(root.getPath()) && Files.isRegularFile(path);
7979
}
8080

@@ -143,7 +143,7 @@ public PathRoot getPathRoot() {
143143

144144
@Override
145145
public String inferBinaryName(PathFileObject javaFileObject) {
146-
return javaFileObject.getFullPath().startsWith(root.getPath())
146+
return javaFileObject.getAbsolutePath().startsWith(root.getPath())
147147
? FileUtils.pathToBinaryName(javaFileObject.getRelativePath())
148148
: null;
149149
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/PathFileObject.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class PathFileObject implements JavaFileObject {
6969
private final Location location;
7070
private final Path rootPath;
7171
private final Path relativePath;
72-
private final Path fullPath;
72+
private final Path absolutePath;
7373
private final String name;
7474
private final URI uri;
7575
private final Kind kind;
@@ -93,15 +93,15 @@ public PathFileObject(Location location, Path rootPath, Path relativePath) {
9393
this.location = location;
9494
this.rootPath = rootPath;
9595

96-
// TODO(ascopes): should we allow absolute paths here? Not sure that it makes a lot of sense
97-
// here.
96+
// TODO(ascopes): should we allow absolute paths in the input here? Not sure that it makes a
97+
// lot of sense.
9898
this.relativePath = relativePath.isAbsolute()
9999
? rootPath.relativize(relativePath)
100100
: relativePath;
101101

102-
fullPath = rootPath.resolve(relativePath);
102+
absolutePath = rootPath.resolve(relativePath);
103103
name = this.relativePath.toString();
104-
uri = fullPath.toUri();
104+
uri = absolutePath.toUri();
105105
kind = FileUtils.pathToKind(relativePath);
106106
}
107107

@@ -113,7 +113,7 @@ public PathFileObject(Location location, Path rootPath, Path relativePath) {
113113
@Override
114114
public boolean delete() {
115115
try {
116-
return Files.deleteIfExists(fullPath);
116+
return Files.deleteIfExists(absolutePath);
117117
} catch (IOException ex) {
118118
LOGGER.debug("Ignoring error deleting {}", uri, ex);
119119
return false;
@@ -133,6 +133,15 @@ public boolean equals(@Nullable Object other) {
133133
return other instanceof FileObject && uri.equals(((FileObject) other).toUri());
134134
}
135135

136+
/**
137+
* Get the absolute path of this file object.
138+
*
139+
* @return the full path.
140+
*/
141+
public Path getAbsolutePath() {
142+
return absolutePath;
143+
}
144+
136145
/**
137146
* Get the class access level, where appropriate.
138147
*
@@ -182,9 +191,12 @@ public String getCharContent(boolean ignoreEncodingErrors) throws IOException {
182191
* Get the full path of this file object.
183192
*
184193
* @return the full path.
194+
* @deprecated use {@link #getAbsolutePath()} instead.
185195
*/
196+
@Deprecated(since = "0.7.3", forRemoval = true)
197+
@SuppressWarnings("DeprecatedIsStillUsed")
186198
public Path getFullPath() {
187-
return fullPath;
199+
return absolutePath;
188200
}
189201

190202
/**
@@ -206,7 +218,7 @@ public Kind getKind() {
206218
@Override
207219
public long getLastModified() {
208220
try {
209-
return Files.getLastModifiedTime(fullPath).toMillis();
221+
return Files.getLastModifiedTime(absolutePath).toMillis();
210222
} catch (IOException ex) {
211223
LOGGER.debug("Ignoring error reading last modified time for {}", uri, ex);
212224
return NOT_MODIFIED;
@@ -402,13 +414,13 @@ public String toString() {
402414
}
403415

404416
private InputStream openUnbufferedInputStream() throws IOException {
405-
return Files.newInputStream(fullPath);
417+
return Files.newInputStream(absolutePath);
406418
}
407419

408420
private OutputStream openUnbufferedOutputStream() throws IOException {
409421
// Ensure parent directories exist first.
410-
Files.createDirectories(fullPath.getParent());
411-
return Files.newOutputStream(fullPath);
422+
Files.createDirectories(absolutePath.getParent());
423+
return Files.newOutputStream(absolutePath);
412424
}
413425

414426
private CharsetDecoder decoder(boolean ignoreEncodingErrors) {

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/filemanagers/PathFileObjectTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void equalsReturnsTrueIfTheFileObjectHasTheSameUri() {
202202
}
203203

204204
@DisplayName(".equals(PathFileObject) returns true if the file object is the same instance")
205+
@SuppressWarnings("EqualsWithItself")
205206
@Test
206207
void equalsReturnsTrueIfTheFileObjectIsTheSameInstance() {
207208
// Given
@@ -212,7 +213,20 @@ void equalsReturnsTrueIfTheFileObjectIsTheSameInstance() {
212213
var fileObject = new PathFileObject(location, rootPath, relativePath);
213214

214215
// Then
215-
assertThat(fileObject).isEqualTo(fileObject);
216+
assertThat(fileObject.equals(fileObject)).isTrue();
217+
}
218+
219+
@DisplayName(".getAbsolutePath() returns the absolute path")
220+
@Test
221+
void getAbsolutePathReturnsTheAbsolutePath() {
222+
// Given
223+
var rootPath = someAbsolutePath();
224+
var relativePath = someRelativePath();
225+
var fileObject = new PathFileObject(someLocation(), rootPath, relativePath);
226+
227+
// Then
228+
assertThat(fileObject.getAbsolutePath())
229+
.isEqualTo(rootPath.resolve(relativePath));
216230
}
217231

218232
@DisplayName(".getAccessLevel() returns null")
@@ -310,6 +324,7 @@ void getCharContentPropagatesEncodingErrors() throws IOException {
310324

311325
@DisplayName(".getFullPath() returns the full path")
312326
@Test
327+
@SuppressWarnings("removal")
313328
void getFullPathReturnsTheFullPath() {
314329
// Given
315330
var rootPath = someAbsolutePath();

0 commit comments

Comments
 (0)