diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java index de8402b186..9554984ebe 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java @@ -3442,17 +3442,20 @@ public void close() { } /** - * A sink that stores all elements in a memory map. In case of multi-release jars, this memory - * storage aims to retain the non-versioned class file. + * A sink that stores all elements in a memory map. */ - @HashCodeAndEqualsPlugin.Enhance class InMemory implements Target, Sink { + @MaybeNull + private final ClassFileVersion classFileVersion; + /** * The map for storing all elements being received. */ private final Map storage; + private final Map versions; + /** * Creates a new in-memory storage. */ @@ -3460,13 +3463,38 @@ public InMemory() { this(new HashMap()); } + /** + * Creates a new in-memory storage. + * + * @param classFileVersion The class file version to consider as the maximum version when accepting + * multi-release classes or {@code null} if multi-release versions should + * be discarded. + */ + public InMemory(@MaybeNull ClassFileVersion classFileVersion) { + this(classFileVersion, new HashMap()); + } + /** * Creates a new in-memory storage. * * @param storage The map for storing all elements being received. */ public InMemory(Map storage) { + this(null, storage); + } + + /** + * Creates a new in-memory storage. + * + * @param classFileVersion The class file version to consider as the maximum version when accepting + * multi-release classes or {@code null} if multi-release versions should + * be discarded. + * @param storage The map for storing all elements being received. + */ + public InMemory(@MaybeNull ClassFileVersion classFileVersion, Map storage) { + this.classFileVersion = classFileVersion; this.storage = storage; + versions = new HashMap(); } /** @@ -3490,7 +3518,10 @@ public Sink write(@MaybeNull Manifest manifest) throws IOException { */ public void store(Map binaryRepresentations) { for (Map.Entry entry : binaryRepresentations.entrySet()) { - storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue()); + String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION; + if (!storage.containsKey(name)) { + storage.put(name, entry.getValue()); + } } } @@ -3498,10 +3529,14 @@ public void store(Map binaryRepresentations) { * {@inheritDoc} */ public void store(int version, Map binaryRepresentations) throws IOException { - for (Map.Entry entry : binaryRepresentations.entrySet()) { - String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION; - if (!storage.containsKey(name)) { - storage.put(name, entry.getValue()); + if (classFileVersion != null && version <= classFileVersion.getJavaVersion()) { + for (Map.Entry entry : binaryRepresentations.entrySet()) { + String name = entry.getKey().getInternalName() + CLASS_FILE_EXTENSION; + Integer current = versions.get(name); + if (current == null || current < version) { + versions.put(name, version); + storage.put(name, entry.getValue()); + } } } }