Skip to content

Commit a436dab

Browse files
authored
Merge pull request #725 from ascopes/task/GH-721-add-new-overloads
GH-721: Add overloads for workspaces APIs to support list types.
2 parents 013caa1 + bae79df commit a436dab

File tree

13 files changed

+217
-33
lines changed

13 files changed

+217
-33
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.github.ascopes.jct.workspaces.impl.WrappingDirectoryImpl;
2929
import java.nio.file.Files;
3030
import java.util.HashMap;
31+
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Set;
3334
import javax.tools.JavaFileManager.Location;
@@ -186,7 +187,7 @@ private PackageContainerGroup newPackageGroup(ModuleLocation moduleLocation) {
186187
var group = new AbstractPackageContainerGroup(moduleLocation, release) {};
187188
var pathWrapper = new WrappingDirectoryImpl(
188189
getPackages().iterator().next().getPathRoot(),
189-
moduleLocation.getModuleName()
190+
List.of(moduleLocation.getModuleName())
190191
);
191192
uncheckedIo(() -> Files.createDirectories(pathWrapper.getPath()));
192193
group.addPackage(pathWrapper);

java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/FileUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static URL retrieveRequiredUrl(Path path) {
9595
* @param parts the parts to resolve.
9696
* @return the resolved path.
9797
*/
98-
public static Path resolvePathRecursively(Path root, String... parts) {
98+
public static Path resolvePathRecursively(Path root, List<String> parts) {
9999
return resolve(root, parts);
100100
}
101101

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/DirectoryBuilder.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.nio.file.FileSystem;
2020
import java.nio.file.Path;
21+
import java.util.List;
2122
import org.apiguardian.api.API;
2223
import org.apiguardian.api.API.Status;
2324

@@ -31,6 +32,38 @@
3132
@API(since = "0.0.1", status = Status.STABLE)
3233
public interface DirectoryBuilder {
3334

35+
/**
36+
* Copy the contents of the directory at the given path recursively into this directory.
37+
*
38+
* <p>Symbolic links will not be followed.
39+
*
40+
* <p>This uses the default file system. If you want to use a different {@link FileSystem}
41+
* as your source, then use {@link #copyContentsFrom(Path)} instead.
42+
*
43+
* <p>Examples:
44+
*
45+
* <pre><code>
46+
* // Letting JCT infer the correct path separators to use (recommended).
47+
* directoryBuilder.copyContentsFrom(List.of("foo", "bar", "baz"));
48+
*
49+
* // Using POSIX platform-specific separators (may cause issues if your tests run on Windows)
50+
* directoryBuilder.copyContentsFrom(List.of("foo/bar/baz"));
51+
*
52+
* // Using Windows platform-specific separators (may cause issues if your tests run on POSIX)
53+
* directoryBuilder.copyContentsFrom(List.of("foo\\bar\\baz"));
54+
* </code></pre>
55+
*
56+
* @param fragments parts of the path.
57+
* @return the root managed directory for further configuration.
58+
* @throws IllegalArgumentException if no path fragments are provided.
59+
* @throws NullPointerException if any null path fragments are provided.
60+
* @see #copyContentsFrom(Path)
61+
* @see #copyContentsFrom(String...)
62+
* @since 4.0.0
63+
*/
64+
@API(since = "4.0.0", status = Status.STABLE)
65+
ManagedDirectory copyContentsFrom(List<String> fragments);
66+
3467
/**
3568
* Copy the contents of the directory at the given path recursively into this directory.
3669
*
@@ -57,8 +90,11 @@ public interface DirectoryBuilder {
5790
* @throws IllegalArgumentException if no path fragments are provided.
5891
* @throws NullPointerException if any null path fragments are provided.
5992
* @see #copyContentsFrom(Path)
93+
* @see #copyContentsFrom(List)
6094
*/
61-
ManagedDirectory copyContentsFrom(String... fragments);
95+
default ManagedDirectory copyContentsFrom(String... fragments) {
96+
return copyContentsFrom(List.of(fragments));
97+
}
6298

6399
/**
64100
* Copy the contents of the directory at the given path recursively into this directory.

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/FileBuilder.java

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import java.io.File;
1919
import java.io.InputStream;
2020
import java.net.URL;
21+
import java.nio.ByteBuffer;
2122
import java.nio.charset.Charset;
2223
import java.nio.file.Path;
24+
import java.util.List;
2325
import org.apiguardian.api.API;
2426
import org.apiguardian.api.API.Status;
2527

@@ -176,7 +178,6 @@ public interface FileBuilder {
176178
*/
177179
ManagedDirectory thatIsEmpty();
178180

179-
180181
/**
181182
* Create the file with the given byte contents.
182183
*
@@ -211,9 +212,53 @@ public interface FileBuilder {
211212
*
212213
* @param contents the bytes to write.
213214
* @return the root managed directory for further configuration.
215+
* @see #withContents(ByteBuffer)
214216
*/
215217
ManagedDirectory withContents(byte[] contents);
216218

219+
/**
220+
* An overload of {@link #withContents(byte[])} that consumes a NIO byte buffer.
221+
*
222+
* @param buffer the byte buffer to consume.
223+
* @return the managed directory.
224+
* @since 4.0.0
225+
*/
226+
@API(since = "4.0.0", status = Status.STABLE)
227+
default ManagedDirectory withContents(ByteBuffer buffer) {
228+
var array = new byte[buffer.remaining()];
229+
buffer.get(array);
230+
return withContents(array);
231+
}
232+
233+
/**
234+
* Create the file with the given contents.
235+
*
236+
* <pre><code>
237+
* directory
238+
* .createFile("org", "example", "HelloWorld.java")
239+
* .withContents(StandardCharsets.US_ASCII, List.of(
240+
* "package org.example;",
241+
* "",
242+
* "public class HelloWorld {",
243+
* " public static void main(String[] args) {",
244+
* " System.out.println(\"Hello, World!\");",
245+
* " }",
246+
* "}"
247+
* ));
248+
* </code></pre>
249+
*
250+
* @param charset the character encoding to use.
251+
* @param lines the lines to write.
252+
* @return the root managed directory for further configuration.
253+
* @see #withContents(Charset, String...)
254+
* @see #withContents(String...)
255+
* @see #withContents(List)
256+
* @see #withContents(byte[])
257+
* @since 4.0.0
258+
*/
259+
@API(since = "4.0.0", status = Status.STABLE)
260+
ManagedDirectory withContents(Charset charset, List<String> lines);
261+
217262
/**
218263
* Create the file with the given contents.
219264
*
@@ -253,10 +298,44 @@ public interface FileBuilder {
253298
* @param charset the character encoding to use.
254299
* @param lines the lines to write.
255300
* @return the root managed directory for further configuration.
301+
* @see #withContents(List)
302+
* @see #withContents(Charset, List)
303+
* @see #withContents(String...)
304+
* @see #withContents(byte[])
305+
*/
306+
default ManagedDirectory withContents(Charset charset, String... lines) {
307+
return withContents(charset, List.of(lines));
308+
}
309+
310+
/**
311+
* Create the file with the given contents as UTF-8.
312+
*
313+
* <p>If you are using multi-line strings, an example of usage would be:
314+
*
315+
* <pre><code>
316+
* directory
317+
* .createFile("org", "example", "HelloWorld.java")
318+
* .withContents(List.of(
319+
* "package org.example;",
320+
* "",
321+
* "public class HelloWorld {",
322+
* " public static void main(String[] args) {",
323+
* " System.out.println(\"Hello, World!\");",
324+
* " }",
325+
* "}"
326+
* ));
327+
* </code></pre>
328+
*
329+
* @param lines the lines to write using the default charset.
330+
* @return the root managed directory for further configuration.
331+
* @see #withContents(Charset, String...)
256332
* @see #withContents(String...)
333+
* @see #withContents(Charset, List)
257334
* @see #withContents(byte[])
335+
* @since 4.0.0
258336
*/
259-
ManagedDirectory withContents(Charset charset, String... lines);
337+
@API(since = "4.0.0", status = Status.STABLE)
338+
ManagedDirectory withContents(List<String> lines);
260339

261340
/**
262341
* Create the file with the given contents as UTF-8.
@@ -298,7 +377,11 @@ public interface FileBuilder {
298377
* @param lines the lines to write using the default charset.
299378
* @return the root managed directory for further configuration.
300379
* @see #withContents(Charset, String...)
380+
* @see #withContents(List)
381+
* @see #withContents(Charset, List)
301382
* @see #withContents(byte[])
302383
*/
303-
ManagedDirectory withContents(String... lines);
384+
default ManagedDirectory withContents(String... lines) {
385+
return withContents(List.of(lines));
386+
}
304387
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/ManagedDirectory.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.Closeable;
1919
import java.io.IOException;
20+
import java.util.List;
2021
import org.apiguardian.api.API;
2122
import org.apiguardian.api.API.Status;
2223

@@ -101,7 +102,31 @@ default ManagedDirectory and() {
101102
* @throws IllegalArgumentException if no path fragments are provided.
102103
* @throws NullPointerException if any of the path fragments are {@code null}.
103104
*/
104-
DirectoryBuilder createDirectory(String... fragments);
105+
default DirectoryBuilder createDirectory(String... fragments) {
106+
return createDirectory(List.of(fragments));
107+
}
108+
109+
/**
110+
* Create a directory builder for the given path in this RAM file system.
111+
*
112+
* <p>Examples:
113+
*
114+
* <pre><code>
115+
* // Using platform-specific separators.
116+
* dir.createDirectory(List.of("foo/bar/baz"))...;
117+
*
118+
* // Letting JCT infer the correct path separators to use (recommended).
119+
* dir.createDirectory(List.of("foo", "bar", "baz"))...;
120+
* </code></pre>
121+
*
122+
* @param fragments the parts of the path.
123+
* @return the directory builder.
124+
* @throws IllegalArgumentException if no path fragments are provided.
125+
* @throws NullPointerException if any of the path fragments are {@code null}.
126+
* @since 4.0.0
127+
*/
128+
@API(since = "4.0.0", status = Status.STABLE)
129+
DirectoryBuilder createDirectory(List<String> fragments);
105130

106131
/**
107132
* Create a file builder for the given path in this RAM file system.
@@ -119,7 +144,29 @@ default ManagedDirectory and() {
119144
* @throws IllegalArgumentException if no path fragments are provided.
120145
* @throws NullPointerException if any of the path fragments are {@code null}.
121146
*/
122-
FileBuilder createFile(String... fragments);
147+
default FileBuilder createFile(String... fragments) {
148+
return createFile(List.of(fragments));
149+
}
150+
151+
/**
152+
* Create a file builder for the given path in this RAM file system.
153+
*
154+
* <pre><code>
155+
* // Using platform-specific separators.
156+
* dir.createFile(List.of("foo/bar/baz.txt"))...;
157+
*
158+
* // Letting JCT infer the correct path separators to use (recommended).
159+
* dir.createFile(List.of("foo", "bar", "baz.txt"))...;
160+
* </code></pre>
161+
*
162+
* @param fragments the parts of the path.
163+
* @return the file builder.
164+
* @throws IllegalArgumentException if no path fragments are provided.
165+
* @throws NullPointerException if any of the path fragments are {@code null}.
166+
* @since 4.0.0
167+
*/
168+
@API(since = "4.0.0", status = Status.STABLE)
169+
FileBuilder createFile(List<String> fragments);
123170

124171
/**
125172
* Get the identifying name of the temporary file system.

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/Workspace.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ default <T extends Throwable> void use(ThrowingWorkspaceConsumer<T> consumer) th
880880
* @since 3.2.0
881881
*/
882882
@API(since = "3.2.0", status = Status.STABLE)
883-
public interface ThrowingWorkspaceConsumer<T extends Throwable> {
883+
interface ThrowingWorkspaceConsumer<T extends Throwable> {
884884

885885
/**
886886
* Consume a workspace.

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/impl/AbstractManagedDirectory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URI;
3232
import java.net.URL;
3333
import java.nio.file.Path;
34+
import java.util.List;
3435
import org.apiguardian.api.API;
3536
import org.apiguardian.api.API.Status;
3637
import org.jspecify.annotations.Nullable;
@@ -112,21 +113,21 @@ public String getName() {
112113
}
113114

114115
@Override
115-
public FileBuilder createFile(String... fragments) {
116+
public FileBuilder createFile(List<String> fragments) {
116117
requireNonNullValues(fragments, "fragments");
117118
requireAtLeastOne(fragments, "fragments");
118119
return new FileBuilderImpl(this, fragments);
119120
}
120121

121122
@Override
122-
public DirectoryBuilder createDirectory(String... fragments) {
123+
public DirectoryBuilder createDirectory(List<String> fragments) {
123124
requireNonNullValues(fragments, "fragments");
124125
requireAtLeastOne(fragments, "fragments");
125126
return new DirectoryBuilderImpl(this, fragments);
126127
}
127128

128129
@Override
129-
public ManagedDirectory copyContentsFrom(String... fragments) {
130+
public ManagedDirectory copyContentsFrom(List<String> fragments) {
130131
requireNonNullValues(fragments, "fragments");
131132
requireAtLeastOne(fragments, "fragments");
132133
return rootDirectory().copyContentsFrom(fragments);
@@ -167,6 +168,6 @@ public String toString() {
167168
}
168169

169170
private DirectoryBuilder rootDirectory() {
170-
return new DirectoryBuilderImpl(this, "");
171+
return new DirectoryBuilderImpl(this, List.of(""));
171172
}
172173
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/impl/DirectoryBuilderImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.file.Path;
3030
import java.nio.file.SimpleFileVisitor;
3131
import java.nio.file.attribute.BasicFileAttributes;
32+
import java.util.List;
3233
import java.util.StringJoiner;
3334
import org.apiguardian.api.API;
3435
import org.apiguardian.api.API.Status;
@@ -55,7 +56,7 @@ public final class DirectoryBuilderImpl implements DirectoryBuilder {
5556
* @param parent the parent managed directory to chain calls back onto.
5657
* @param fragments parts of the directory path.
5758
*/
58-
public DirectoryBuilderImpl(ManagedDirectory parent, String... fragments) {
59+
public DirectoryBuilderImpl(ManagedDirectory parent, List<String> fragments) {
5960
requireNonNullValues(fragments, "fragments");
6061
requireAtLeastOne(fragments, "fragments");
6162

@@ -64,14 +65,15 @@ public DirectoryBuilderImpl(ManagedDirectory parent, String... fragments) {
6465
}
6566

6667
@Override
67-
public ManagedDirectory copyContentsFrom(String... fragments) {
68+
public ManagedDirectory copyContentsFrom(List<String> fragments) {
6869
requireNonNullValues(fragments, "fragments");
6970
requireAtLeastOne(fragments, "fragments");
7071

7172
// Path.of is fine here as it is for the default file system.
72-
var path = Path.of(fragments[0]);
73-
for (var i = 1; i < fragments.length; ++i) {
74-
path = path.resolve(fragments[i]);
73+
var iter = fragments.iterator();
74+
var path = Path.of(iter.next());
75+
while (iter.hasNext()) {
76+
path = path.resolve(iter.next());
7577
}
7678

7779
return copyContentsFrom(path);

0 commit comments

Comments
 (0)