From ba7e98fd1fdbfc50ea9250352a2a22c0b57987e6 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 24 Sep 2024 23:07:15 +0200 Subject: [PATCH] Allow class file locator resolution based on class file version. --- .../main/java/net/bytebuddy/build/Plugin.java | 36 +++++++++++-------- .../build/PluginEngineDefaultTest.java | 2 +- .../build/PluginEngineSourceCompoundTest.java | 6 ++-- .../build/PluginEngineSourceEmptyTest.java | 2 +- .../PluginEngineSourceFilteringTest.java | 4 +-- .../PluginEngineSourceForFolderTest.java | 14 ++++---- .../PluginEngineSourceForJarFileTest.java | 13 ++++--- .../build/PluginEngineSourceInMemoryTest.java | 14 ++++---- 8 files changed, 48 insertions(+), 43 deletions(-) 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 dab880538fe..cb8d9bd8f5e 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 @@ -2256,13 +2256,15 @@ interface Origin extends Iterable, Closeable { Manifest getManifest() throws IOException; /** - * Returns a class file locator for the represented source. If the class file locator needs to be closed, it is the responsibility - * of this origin to close the locator or its underlying resources. + * Creates a class file locator for the represented source. If the class file locator needs to be closed, + * it is the responsibility of this origin to close the locator or its underlying resources. * + * @param classFileVersion The class file version to consider for multi-release jars or {@code null} + * if multi-release jars should not be considered. * @return A class file locator for locating class files of this instance. * @throws IOException If an I/O exception occurs. */ - ClassFileLocator getClassFileLocator() throws IOException; + ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException; /** * An origin implementation for a jar file. @@ -2294,8 +2296,10 @@ public Manifest getManifest() throws IOException { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() { - return new ClassFileLocator.ForJarFile(file); + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException { + return classFileVersion == null + ? new ClassFileLocator.ForJarFile(file) + : ClassFileLocator.ForJarFile.of(file, classFileVersion); } /** @@ -2409,8 +2413,8 @@ public Manifest getManifest() throws IOException { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() throws IOException { - return delegate.getClassFileLocator(); + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException { + return delegate.toClassFileLocator(classFileVersion); } /** @@ -2715,7 +2719,7 @@ public Origin read() { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() { + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) { return ClassFileLocator.NoOp.INSTANCE; } @@ -2821,10 +2825,10 @@ public Manifest getManifest() throws IOException { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() throws IOException { + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException { List classFileLocators = new ArrayList(origins.size()); for (Source.Origin origin : origins) { - classFileLocators.add(origin.getClassFileLocator()); + classFileLocators.add(origin.toClassFileLocator(classFileVersion)); } return new ClassFileLocator.Compound(classFileLocators); } @@ -2980,7 +2984,7 @@ public Origin read() { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() { + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) { return ClassFileLocator.Simple.ofResources(storage); } @@ -3086,8 +3090,10 @@ public Origin read() { /** * {@inheritDoc} */ - public ClassFileLocator getClassFileLocator() { - return new ClassFileLocator.ForFolder(folder); + public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException { + return classFileVersion == null + ? new ClassFileLocator.ForFolder(folder) + : ClassFileLocator.ForFolder.of(folder, classFileVersion); } /** @@ -4749,8 +4755,8 @@ public Summary apply(Source source, Target target, List>() { @@ -81,7 +81,7 @@ public void testEmptyCompound() throws Exception { @Test public void testClassFileLocator() throws Exception { - assertThat(new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().getClassFileLocator().locate(FOO).isResolved(), is(false)); + assertThat(new Plugin.Engine.Source.Compound(Arrays.asList(left, right)).read().toClassFileLocator(null).locate(FOO).isResolved(), is(false)); verify(leftLocator).locate(FOO); verify(rightLocator).locate(FOO); } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceEmptyTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceEmptyTest.java index 83c38a28614..ada2ccd244f 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceEmptyTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceEmptyTest.java @@ -13,7 +13,7 @@ public class PluginEngineSourceEmptyTest { @Test public void testNonOperational() throws Exception { - assertThat(Plugin.Engine.Source.Empty.INSTANCE.getClassFileLocator(), is((ClassFileLocator) ClassFileLocator.NoOp.INSTANCE)); + assertThat(Plugin.Engine.Source.Empty.INSTANCE.toClassFileLocator(null), is((ClassFileLocator) ClassFileLocator.NoOp.INSTANCE)); assertThat(Plugin.Engine.Source.Empty.INSTANCE.getManifest(), nullValue(Manifest.class)); assertThat(Plugin.Engine.Source.Empty.INSTANCE.iterator().hasNext(), is(false)); } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceFilteringTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceFilteringTest.java index e1865bb7323..37b6ea2ca11 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceFilteringTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceFilteringTest.java @@ -80,8 +80,8 @@ public void testClassFileLocator() throws Exception { ClassFileLocator classFileLocator = mock(ClassFileLocator.class); when(source.read()).thenReturn(origin); - when(origin.getClassFileLocator()).thenReturn(classFileLocator); + when(origin.toClassFileLocator(null)).thenReturn(classFileLocator); - assertThat(new Plugin.Engine.Source.Filtering(source, matcher).read().getClassFileLocator(), is(classFileLocator)); + assertThat(new Plugin.Engine.Source.Filtering(source, matcher).read().toClassFileLocator(null), is(classFileLocator)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForFolderTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForFolderTest.java index f913a0bcc5e..f3de007f24b 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForFolderTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForFolderTest.java @@ -35,7 +35,7 @@ public void testEmpty() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForFolder(folder).read(); try { assertThat(origin.getManifest(), nullValue(Manifest.class)); - assertThat(origin.getClassFileLocator().locate(Object.class.getName()).isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate(Object.class.getName()).isResolved(), is(false)); assertThat(origin.iterator().hasNext(), is(false)); } finally { origin.close(); @@ -54,9 +54,9 @@ public void testFile() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForFolder(folder).read(); try { assertThat(origin.getManifest(), nullValue(Manifest.class)); - assertThat(origin.getClassFileLocator().locate("Foo").isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate("Foo").resolve(), is(new byte[]{1, 2, 3})); - assertThat(origin.getClassFileLocator().locate("Bar").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate("Foo").isResolved(), is(true)); + assertThat(origin.toClassFileLocator(null).locate("Foo").resolve(), is(new byte[]{1, 2, 3})); + assertThat(origin.toClassFileLocator(null).locate("Bar").isResolved(), is(false)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); Plugin.Engine.Source.Element element = iterator.next(); @@ -89,9 +89,9 @@ public void testFileInSubFolder() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForFolder(folder).read(); try { assertThat(origin.getManifest(), nullValue(Manifest.class)); - assertThat(origin.getClassFileLocator().locate("bar.Foo").isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate("bar.Foo").resolve(), is(new byte[]{1, 2, 3})); - assertThat(origin.getClassFileLocator().locate("Bar").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate("bar.Foo").isResolved(), is(true)); + assertThat(origin.toClassFileLocator(null).locate("bar.Foo").resolve(), is(new byte[]{1, 2, 3})); + assertThat(origin.toClassFileLocator(null).locate("Bar").isResolved(), is(false)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); Plugin.Engine.Source.Element element = iterator.next(); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForJarFileTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForJarFileTest.java index 35aa598d5fe..bc7b79c5c17 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForJarFileTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceForJarFileTest.java @@ -1,7 +1,6 @@ package net.bytebuddy.build; import net.bytebuddy.utility.StreamDrainer; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -41,9 +40,9 @@ public void testFile() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForJarFile(this.file).read(); try { assertThat(origin.getManifest(), nullValue(Manifest.class)); - assertThat(origin.getClassFileLocator().locate("Foo").isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate("Foo").resolve(), is(new byte[]{1, 2, 3})); - assertThat(origin.getClassFileLocator().locate("Bar").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate("Foo").isResolved(), is(true)); + assertThat(origin.toClassFileLocator(null).locate("Foo").resolve(), is(new byte[]{1, 2, 3})); + assertThat(origin.toClassFileLocator(null).locate("Bar").isResolved(), is(false)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); Plugin.Engine.Source.Element element = iterator.next(); @@ -75,9 +74,9 @@ public void testFileInSubFolder() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForJarFile(this.file).read(); try { assertThat(origin.getManifest(), nullValue(Manifest.class)); - assertThat(origin.getClassFileLocator().locate("bar.Foo").isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate("bar.Foo").resolve(), is(new byte[]{1, 2, 3})); - assertThat(origin.getClassFileLocator().locate("Bar").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate("bar.Foo").isResolved(), is(true)); + assertThat(origin.toClassFileLocator(null).locate("bar.Foo").resolve(), is(new byte[]{1, 2, 3})); + assertThat(origin.toClassFileLocator(null).locate("Bar").isResolved(), is(false)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); Plugin.Engine.Source.Element element = iterator.next(); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceInMemoryTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceInMemoryTest.java index f029703b31a..18a1d03ddf5 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceInMemoryTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineSourceInMemoryTest.java @@ -20,9 +20,9 @@ public class PluginEngineSourceInMemoryTest { public void testNoManifest() throws Exception { Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.InMemory(Collections.singletonMap("foo/Bar.class", new byte[]{1, 2, 3})).read(); try { - assertThat(origin.getClassFileLocator().locate("foo.Bar").isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate("foo.Bar").resolve(), is(new byte[]{1, 2, 3})); - assertThat(origin.getClassFileLocator().locate("qux.Baz").isResolved(), is(false)); + assertThat(origin.toClassFileLocator().locate("foo.Bar").isResolved(), is(true)); + assertThat(origin.toClassFileLocator().locate("foo.Bar").resolve(), is(new byte[]{1, 2, 3})); + assertThat(origin.toClassFileLocator().locate("qux.Baz").isResolved(), is(false)); assertThat(origin.getManifest(), nullValue(Manifest.class)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); @@ -40,9 +40,9 @@ public void testNoManifest() throws Exception { public void testOfTypes() throws Exception { Plugin.Engine.Source.Origin origin = Plugin.Engine.Source.InMemory.ofTypes(Foo.class).read(); try { - assertThat(origin.getClassFileLocator().locate(Foo.class.getName()).isResolved(), is(true)); - assertThat(origin.getClassFileLocator().locate(Foo.class.getName()).resolve(), is(ClassFileLocator.ForClassLoader.read(Foo.class))); - assertThat(origin.getClassFileLocator().locate("qux.Baz").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate(Foo.class.getName()).isResolved(), is(true)); + assertThat(origin.toClassFileLocator(null).locate(Foo.class.getName()).resolve(), is(ClassFileLocator.ForClassLoader.read(Foo.class))); + assertThat(origin.toClassFileLocator(null).locate("qux.Baz").isResolved(), is(false)); assertThat(origin.getManifest(), nullValue(Manifest.class)); Iterator iterator = origin.iterator(); assertThat(iterator.hasNext(), is(true)); @@ -64,7 +64,7 @@ public void testManifest() throws Exception { manifest.write(outputStream); Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.InMemory(Collections.singletonMap(JarFile.MANIFEST_NAME, outputStream.toByteArray())).read(); try { - assertThat(origin.getClassFileLocator().locate("foo.Bar").isResolved(), is(false)); + assertThat(origin.toClassFileLocator(null).locate("foo.Bar").isResolved(), is(false)); assertThat(origin.getManifest(), notNullValue(Manifest.class)); assertThat(origin.getManifest().getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION), is((Object) "1.0")); } finally {