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 e29e2b340d..c9c1f61de3 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 @@ -2947,7 +2947,7 @@ public InMemory(Map storage) { } /** - * Represents a collection of types as a in-memory source. + * Represents a collection of types as an in-memory source. * * @param type The types to represent. * @return A source representing the supplied types. @@ -2957,17 +2957,36 @@ public static Source ofTypes(Class... type) { } /** - * Represents a collection of types as a in-memory source. + * Represents a collection of types as an in-memory source. * * @param types The types to represent. * @return A source representing the supplied types. */ public static Source ofTypes(Collection> types) { + return ofTypes(types, Collections.>>emptyMap()); + } + + /** + * Represents a collection of types as an in-memory source. + * + * @param types The types to represent. + * @param versionedTypes A versioned mapping of types to represent. + * @return A source representing the supplied types. + */ + public static Source ofTypes(Collection> types, Map>> versionedTypes) { + Map> versionedBinaryRepresentations = new HashMap>(); + for (Map.Entry>> entry : versionedTypes.entrySet()) { + Map binaryRepresentations = new HashMap(); + for (Class type : entry.getValue()) { + binaryRepresentations.put(TypeDescription.ForLoadedType.of(type), ClassFileLocator.ForClassLoader.read(type)); + } + versionedBinaryRepresentations.put(entry.getKey(), binaryRepresentations); + } Map binaryRepresentations = new HashMap(); for (Class type : types) { binaryRepresentations.put(TypeDescription.ForLoadedType.of(type), ClassFileLocator.ForClassLoader.read(type)); } - return ofTypes(binaryRepresentations); + return ofTypes(binaryRepresentations, versionedBinaryRepresentations); } /** @@ -2977,10 +2996,29 @@ public static Source ofTypes(Collection> types) { * @return A source representing the supplied types. */ public static Source ofTypes(Map binaryRepresentations) { + return ofTypes(binaryRepresentations, Collections.>emptyMap()); + } + + /** + * Represents a map of type names to their binary representation as an in-memory source. + * + * @param binaryRepresentations A mapping of type names to their binary representation. + * @param versionedBinaryRepresentations A versioned mapping of type names to their binary representation. + * @return A source representing the supplied types. + */ + public static Source ofTypes( + Map binaryRepresentations, + Map> versionedBinaryRepresentations + ) { Map storage = new HashMap(); for (Map.Entry entry : binaryRepresentations.entrySet()) { storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue()); } + for (Map.Entry> versioned : versionedBinaryRepresentations.entrySet()) { + for (Map.Entry entry : versioned.getValue().entrySet()) { + storage.put(META_INF_VERSIONS + versioned.getKey().getJavaVersion() + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue()); + } + } return new InMemory(storage); } @@ -4857,18 +4895,23 @@ public Summary apply(Source source, Target target, List versions = new TreeSet(); Enumeration enumeration = jarFile.entries(); while (enumeration.hasMoreElements()) { diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineDefaultTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineDefaultTest.java index e435ed862b..571f67dda0 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineDefaultTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineDefaultTest.java @@ -223,10 +223,9 @@ public void testSimpleTransformationMultiRelease() throws Exception { Plugin plugin = eager ? new SimplePlugin() : new PreprocessingPlugin(new SimplePlugin()); - Plugin.Engine.Source source = new Plugin.Engine.Source.InMemory(Collections.singletonMap( - "META-INF/versions/11/" + Sample.class.getName().replace('.', '/') + Plugin.Engine.CLASS_FILE_EXTENSION, - ClassFileLocator.ForClassLoader.read(Sample.class) - )); + Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Collections.emptyList(), Collections.singletonMap( + ClassFileVersion.JAVA_V11, + Collections.singletonList(Sample.class))); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener)