Skip to content

Commit 57a160a

Browse files
committed
Allow for linking targeted files.
1 parent 10a7122 commit 57a160a

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,13 +3713,26 @@ class ForFolder implements Target, Sink {
37133713
*/
37143714
private final File folder;
37153715

3716+
private final boolean link;
3717+
37163718
/**
37173719
* Creates a new target for a folder.
37183720
*
37193721
* @param folder The folder that is represented by this instance.
37203722
*/
37213723
public ForFolder(File folder) {
3724+
this(folder, false);
3725+
}
3726+
3727+
/**
3728+
* Creates a new target for a folder.
3729+
*
3730+
* @param folder The folder that is represented by this instance.
3731+
* @param link {@code true} if retained files should be linked and not copied.
3732+
*/
3733+
public ForFolder(File folder, boolean link) {
37223734
this.folder = folder;
3735+
this.link = link;
37233736
}
37243737

37253738
/**
@@ -3789,7 +3802,11 @@ public void retain(Source.Element element) throws IOException {
37893802
} else if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
37903803
throw new IOException("Could not create directory: " + target.getParent());
37913804
} else if (resolved != null && !resolved.equals(target)) {
3792-
FileSystem.getInstance().copy(resolved, target);
3805+
if (link) {
3806+
FileSystem.getInstance().link(resolved, target);
3807+
} else {
3808+
FileSystem.getInstance().copy(resolved, target);
3809+
}
37933810
} else if (!target.equals(resolved)) {
37943811
InputStream inputStream = element.getInputStream();
37953812
try {
@@ -3889,7 +3906,7 @@ interface Materializable {
38893906
void materialize(Target.Sink sink,
38903907
List<TypeDescription> transformed,
38913908
Map<TypeDescription,
3892-
List<Throwable>> failed,
3909+
List<Throwable>> failed,
38933910
List<String> unresolved) throws IOException;
38943911

38953912
/**
@@ -3925,7 +3942,7 @@ protected ForTransformedElement(@MaybeNull ClassFileVersion classFileVersion, Dy
39253942
public void materialize(Target.Sink sink,
39263943
List<TypeDescription> transformed,
39273944
Map<TypeDescription,
3928-
List<Throwable>> failed,
3945+
List<Throwable>> failed,
39293946
List<String> unresolved) throws IOException {
39303947
if (classFileVersion == null) {
39313948
sink.store(dynamicType.getAllTypes());
@@ -3961,7 +3978,7 @@ protected ForRetainedElement(Source.Element element) {
39613978
public void materialize(Target.Sink sink,
39623979
List<TypeDescription> transformed,
39633980
Map<TypeDescription,
3964-
List<Throwable>> failed,
3981+
List<Throwable>> failed,
39653982
List<String> unresolved) throws IOException {
39663983
sink.retain(element);
39673984
}
@@ -4006,7 +4023,7 @@ protected ForFailedElement(Source.Element element, TypeDescription typeDescripti
40064023
public void materialize(Target.Sink sink,
40074024
List<TypeDescription> transformed,
40084025
Map<TypeDescription,
4009-
List<Throwable>> failed,
4026+
List<Throwable>> failed,
40104027
List<String> unresolved) throws IOException {
40114028
sink.retain(element);
40124029
failed.put(typeDescription, errored);
@@ -4045,7 +4062,7 @@ protected ForUnresolvedElement(Source.Element element, String typeName) {
40454062
public void materialize(Target.Sink sink,
40464063
List<TypeDescription> transformed,
40474064
Map<TypeDescription,
4048-
List<Throwable>> failed,
4065+
List<Throwable>> failed,
40494066
List<String> unresolved) throws IOException {
40504067
sink.retain(element);
40514068
unresolved.add(typeName);

byte-buddy-dep/src/main/java/net/bytebuddy/utility/FileSystem.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ private static <T> T doPrivileged(PrivilegedAction<T> action) {
6565
*/
6666
public abstract void copy(File source, File target) throws IOException;
6767

68+
/**
69+
* Links a file as a hard-link. If linking is not supported, a copy is made.
70+
*
71+
* @param source The source file.
72+
* @param target The target file.
73+
* @throws IOException If an I/O exception occurs.
74+
*/
75+
public void link(File source, File target) throws IOException {
76+
copy(source, target);
77+
}
78+
6879
/**
6980
* Moves a file.
7081
*
@@ -150,6 +161,11 @@ public void copy(File source, File target) throws IOException {
150161
FILES.copy(DISPATCHER.toPath(source), DISPATCHER.toPath(target), option);
151162
}
152163

164+
@Override
165+
public void link(File source, File target) throws IOException {
166+
FILES.createLink(FILES.deleteIfExists(DISPATCHER.toPath(target)), DISPATCHER.toPath(source));
167+
}
168+
153169
@Override
154170
public void move(File source, File target) throws IOException {
155171
Object[] option = STANDARD_COPY_OPTION.toArray(1);
@@ -193,6 +209,18 @@ Object copy(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
193209
@JavaDispatcher.Proxied("java.nio.file.Path") Object target,
194210
@JavaDispatcher.Proxied("java.nio.file.CopyOption") Object[] option) throws IOException;
195211

212+
/**
213+
* Links a file.
214+
*
215+
* @param source The source {@code java.nio.file.Path}.
216+
* @param target The target {@code java.nio.file.Path}.
217+
* @return The copied file.
218+
* @throws IOException If an I/O exception occurs.
219+
*/
220+
@JavaDispatcher.IsStatic
221+
Object createLink(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
222+
@JavaDispatcher.Proxied("java.nio.file.Path") Object target) throws IOException;
223+
196224
/**
197225
* Moves a file.
198226
*
@@ -206,6 +234,16 @@ Object copy(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
206234
Object move(@JavaDispatcher.Proxied("java.nio.file.Path") Object source,
207235
@JavaDispatcher.Proxied("java.nio.file.Path") Object target,
208236
@JavaDispatcher.Proxied("java.nio.file.CopyOption") Object[] option) throws IOException;
237+
238+
/**
239+
* Deletes a file if it exists.
240+
*
241+
* @param file The {@code java.nio.file.Path} to delete if it exists.
242+
* @return The supplied file.
243+
* @throws IOException If an I/O exception occurs.
244+
*/
245+
@JavaDispatcher.IsStatic
246+
Object deleteIfExists(@JavaDispatcher.Proxied("java.nio.file.Path") Object file) throws IOException;
209247
}
210248

211249
/**

0 commit comments

Comments
 (0)