Skip to content

Commit 6f0b5ed

Browse files
committed
Add API to store multi-release class file.
1 parent 2fe1efb commit 6f0b5ed

File tree

1 file changed

+73
-19
lines changed
  • byte-buddy-dep/src/main/java/net/bytebuddy/build

1 file changed

+73
-19
lines changed

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

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ interface Engine {
777777
*/
778778
String PLUGIN_FILE = "META-INF/net.bytebuddy/build.plugins";
779779

780+
/**
781+
* The prefix folder for {@code META-INF/versions/} which contains multi-release files.
782+
*/
783+
String META_INF_VERSIONS = "META-INF/versions/";
784+
780785
/**
781786
* Defines a new Byte Buddy instance for usage for type creation.
782787
*
@@ -3286,6 +3291,15 @@ interface Sink extends Closeable {
32863291
*/
32873292
void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOException;
32883293

3294+
/**
3295+
* Stores the supplied binary representation of types in this sink.
3296+
*
3297+
* @param version The version of the multi-release jar file.
3298+
* @param binaryRepresentations The binary representations to store.
3299+
* @throws IOException If an I/O error occurs.
3300+
*/
3301+
void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException;
3302+
32893303
/**
32903304
* Retains the supplied element in its original form.
32913305
*
@@ -3324,6 +3338,17 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOE
33243338
}
33253339
}
33263340

3341+
/**
3342+
* {@inheritDoc}
3343+
*/
3344+
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3345+
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3346+
outputStream.putNextEntry(new JarEntry(META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION));
3347+
outputStream.write(entry.getValue());
3348+
outputStream.closeEntry();
3349+
}
3350+
}
3351+
33273352
/**
33283353
* {@inheritDoc}
33293354
*/
@@ -3378,6 +3403,13 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
33783403
/* do nothing */
33793404
}
33803405

3406+
/**
3407+
* {@inheritDoc}
3408+
*/
3409+
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3410+
/* do nothing */
3411+
}
3412+
33813413
/**
33823414
* {@inheritDoc}
33833415
*/
@@ -3445,6 +3477,15 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
34453477
}
34463478
}
34473479

3480+
/**
3481+
* {@inheritDoc}
3482+
*/
3483+
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3484+
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3485+
storage.putIfAbsent(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
3486+
}
3487+
}
3488+
34483489
/**
34493490
* {@inheritDoc}
34503491
*/
@@ -3525,40 +3566,58 @@ public ForFolder(File folder) {
35253566
}
35263567

35273568
/**
3528-
* {@inheritDoc}
3569+
* Stores binary representations to a folder.
3570+
*
3571+
* @param folder The base folder.
3572+
* @param binaryRepresentations The binary representations to store.
3573+
* @throws IOException If an I/O exception occurs.
35293574
*/
3530-
public Sink write(@MaybeNull Manifest manifest) throws IOException {
3531-
if (manifest != null) {
3532-
File target = new File(folder, JarFile.MANIFEST_NAME);
3575+
private static void doStore(File folder, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3576+
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3577+
File target = new File(folder, entry.getKey().getInternalName() + CLASS_FILE_EXTENSION);
35333578
if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
35343579
throw new IOException("Could not create directory: " + target.getParent());
35353580
}
35363581
OutputStream outputStream = new FileOutputStream(target);
35373582
try {
3538-
manifest.write(outputStream);
3583+
outputStream.write(entry.getValue());
35393584
} finally {
35403585
outputStream.close();
35413586
}
35423587
}
3543-
return this;
35443588
}
35453589

35463590
/**
35473591
* {@inheritDoc}
35483592
*/
3549-
public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3550-
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3551-
File target = new File(folder, entry.getKey().getInternalName() + CLASS_FILE_EXTENSION);
3593+
public Sink write(@MaybeNull Manifest manifest) throws IOException {
3594+
if (manifest != null) {
3595+
File target = new File(folder, JarFile.MANIFEST_NAME);
35523596
if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
35533597
throw new IOException("Could not create directory: " + target.getParent());
35543598
}
35553599
OutputStream outputStream = new FileOutputStream(target);
35563600
try {
3557-
outputStream.write(entry.getValue());
3601+
manifest.write(outputStream);
35583602
} finally {
35593603
outputStream.close();
35603604
}
35613605
}
3606+
return this;
3607+
}
3608+
3609+
/**
3610+
* {@inheritDoc}
3611+
*/
3612+
public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3613+
doStore(folder, binaryRepresentations);
3614+
}
3615+
3616+
/**
3617+
* {@inheritDoc}
3618+
*/
3619+
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3620+
doStore(new File(folder, META_INF_VERSIONS + version), binaryRepresentations);
35623621
}
35633622

35643623
/**
@@ -3673,7 +3732,7 @@ interface Materializable {
36733732
void materialize(Target.Sink sink,
36743733
List<TypeDescription> transformed,
36753734
Map<TypeDescription,
3676-
List<Throwable>> failed,
3735+
List<Throwable>> failed,
36773736
List<String> unresolved) throws IOException;
36783737

36793738
/**
@@ -3701,7 +3760,7 @@ protected ForTransformedElement(DynamicType dynamicType) {
37013760
public void materialize(Target.Sink sink,
37023761
List<TypeDescription> transformed,
37033762
Map<TypeDescription,
3704-
List<Throwable>> failed,
3763+
List<Throwable>> failed,
37053764
List<String> unresolved) throws IOException {
37063765
sink.store(dynamicType.getAllTypes());
37073766
transformed.add(dynamicType.getTypeDescription());
@@ -3778,7 +3837,7 @@ protected ForFailedElement(Source.Element element, TypeDescription typeDescripti
37783837
public void materialize(Target.Sink sink,
37793838
List<TypeDescription> transformed,
37803839
Map<TypeDescription,
3781-
List<Throwable>> failed,
3840+
List<Throwable>> failed,
37823841
List<String> unresolved) throws IOException {
37833842
sink.retain(element);
37843843
failed.put(typeDescription, errored);
@@ -3817,7 +3876,7 @@ protected ForUnresolvedElement(Source.Element element, String typeName) {
38173876
public void materialize(Target.Sink sink,
38183877
List<TypeDescription> transformed,
38193878
Map<TypeDescription,
3820-
List<Throwable>> failed,
3879+
List<Throwable>> failed,
38213880
List<String> unresolved) throws IOException {
38223881
sink.retain(element);
38233882
unresolved.add(typeName);
@@ -4364,11 +4423,6 @@ public Summary apply(Source source, Target target, Factory... factory) throws IO
43644423
@HashCodeAndEqualsPlugin.Enhance
43654424
class Default extends AbstractBase {
43664425

4367-
/**
4368-
* The prefix folder for {@code META-INF/versions/} which contains multi-release files.
4369-
*/
4370-
private static final String META_INF_VERSIONS = "META-INF/versions/";
4371-
43724426
/**
43734427
* The Byte Buddy instance to use.
43744428
*/

0 commit comments

Comments
 (0)