Skip to content

Commit e06d8ea

Browse files
committed
Adjust memory implementation of in memory target to support multi-release jars.
1 parent 258ea66 commit e06d8ea

File tree

1 file changed

+43
-8
lines changed
  • byte-buddy-dep/src/main/java/net/bytebuddy/build

1 file changed

+43
-8
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,31 +3442,59 @@ public void close() {
34423442
}
34433443

34443444
/**
3445-
* A sink that stores all elements in a memory map. In case of multi-release jars, this memory
3446-
* storage aims to retain the non-versioned class file.
3445+
* A sink that stores all elements in a memory map.
34473446
*/
3448-
@HashCodeAndEqualsPlugin.Enhance
34493447
class InMemory implements Target, Sink {
34503448

3449+
@MaybeNull
3450+
private final ClassFileVersion classFileVersion;
3451+
34513452
/**
34523453
* The map for storing all elements being received.
34533454
*/
34543455
private final Map<String, byte[]> storage;
34553456

3457+
private final Map<String, Integer> versions;
3458+
34563459
/**
34573460
* Creates a new in-memory storage.
34583461
*/
34593462
public InMemory() {
34603463
this(new HashMap<String, byte[]>());
34613464
}
34623465

3466+
/**
3467+
* Creates a new in-memory storage.
3468+
*
3469+
* @param classFileVersion The class file version to consider as the maximum version when accepting
3470+
* multi-release classes or {@code null} if multi-release versions should
3471+
* be discarded.
3472+
*/
3473+
public InMemory(@MaybeNull ClassFileVersion classFileVersion) {
3474+
this(classFileVersion, new HashMap<String, byte[]>());
3475+
}
3476+
34633477
/**
34643478
* Creates a new in-memory storage.
34653479
*
34663480
* @param storage The map for storing all elements being received.
34673481
*/
34683482
public InMemory(Map<String, byte[]> storage) {
3483+
this(null, storage);
3484+
}
3485+
3486+
/**
3487+
* Creates a new in-memory storage.
3488+
*
3489+
* @param classFileVersion The class file version to consider as the maximum version when accepting
3490+
* multi-release classes or {@code null} if multi-release versions should
3491+
* be discarded.
3492+
* @param storage The map for storing all elements being received.
3493+
*/
3494+
public InMemory(@MaybeNull ClassFileVersion classFileVersion, Map<String, byte[]> storage) {
3495+
this.classFileVersion = classFileVersion;
34693496
this.storage = storage;
3497+
versions = new HashMap<String, Integer>();
34703498
}
34713499

34723500
/**
@@ -3490,18 +3518,25 @@ public Sink write(@MaybeNull Manifest manifest) throws IOException {
34903518
*/
34913519
public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
34923520
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3493-
storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
3521+
String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION;
3522+
if (!storage.containsKey(name)) {
3523+
storage.put(name, entry.getValue());
3524+
}
34943525
}
34953526
}
34963527

34973528
/**
34983529
* {@inheritDoc}
34993530
*/
35003531
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3501-
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3502-
String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION;
3503-
if (!storage.containsKey(name)) {
3504-
storage.put(name, entry.getValue());
3532+
if (classFileVersion != null && version <= classFileVersion.getJavaVersion()) {
3533+
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3534+
String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION;
3535+
Integer current = versions.get(name);
3536+
if (current == null || current < version) {
3537+
versions.put(name, version);
3538+
storage.put(name, entry.getValue());
3539+
}
35053540
}
35063541
}
35073542
}

0 commit comments

Comments
 (0)