Skip to content

Commit

Permalink
Allow class file locator resolution based on class file version.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 24, 2024
1 parent d3a15cb commit ba7e98f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 43 deletions.
36 changes: 21 additions & 15 deletions byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2256,13 +2256,15 @@ interface Origin extends Iterable<Element>, 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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -2715,7 +2719,7 @@ public Origin read() {
/**
* {@inheritDoc}
*/
public ClassFileLocator getClassFileLocator() {
public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) {
return ClassFileLocator.NoOp.INSTANCE;
}

Expand Down Expand Up @@ -2821,10 +2825,10 @@ public Manifest getManifest() throws IOException {
/**
* {@inheritDoc}
*/
public ClassFileLocator getClassFileLocator() throws IOException {
public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) throws IOException {
List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(origins.size());
for (Source.Origin origin : origins) {
classFileLocators.add(origin.getClassFileLocator());
classFileLocators.add(origin.toClassFileLocator(classFileVersion));
}
return new ClassFileLocator.Compound(classFileLocators);
}
Expand Down Expand Up @@ -2980,7 +2984,7 @@ public Origin read() {
/**
* {@inheritDoc}
*/
public ClassFileLocator getClassFileLocator() {
public ClassFileLocator toClassFileLocator(@MaybeNull ClassFileVersion classFileVersion) {
return ClassFileLocator.Simple.ofResources(storage);
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -4749,8 +4755,8 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
}
}
Source.Origin origin = source.read();
try {
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(origin.getClassFileLocator(), this.classFileLocator);
try { // TODO: class file locator.
ClassFileLocator classFileLocator = new ClassFileLocator.Compound(origin.toClassFileLocator(null), this.classFileLocator);
TypePool typePool = poolStrategy.typePool(classFileLocator);
Manifest manifest = origin.getManifest();
listener.onManifest(manifest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void testUnresolved() throws Exception {
Sample.class.getName().replace('.', '/') + ".class",
ClassFileLocator.ForClassLoader.read(Sample.class))) {
@Override
public ClassFileLocator getClassFileLocator() {
public ClassFileLocator toClassFileLocator() {
return ClassFileLocator.NoOp.INSTANCE;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public class PluginEngineSourceCompoundTest {
public void setUp() throws Exception {
when(left.read()).thenReturn(leftOrigin);
when(right.read()).thenReturn(rightOrigin);
when(leftOrigin.getClassFileLocator()).thenReturn(leftLocator);
when(rightOrigin.getClassFileLocator()).thenReturn(rightLocator);
when(leftOrigin.toClassFileLocator(null)).thenReturn(leftLocator);
when(rightOrigin.toClassFileLocator(null)).thenReturn(rightLocator);
when(leftLocator.locate(Mockito.any(String.class))).thenReturn(new ClassFileLocator.Resolution.Illegal(FOO));
when(rightLocator.locate(Mockito.any(String.class))).thenReturn(new ClassFileLocator.Resolution.Illegal(FOO));
when(leftOrigin.iterator()).then(new Answer<Iterator<Plugin.Engine.Source.Element>>() {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Plugin.Engine.Source.Element element = iterator.next();
Expand Down Expand Up @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Plugin.Engine.Source.Element element = iterator.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Plugin.Engine.Source.Element element = iterator.next();
Expand Down Expand Up @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Plugin.Engine.Source.Element element = iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Expand All @@ -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<Plugin.Engine.Source.Element> iterator = origin.iterator();
assertThat(iterator.hasNext(), is(true));
Expand All @@ -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 {
Expand Down

0 comments on commit ba7e98f

Please sign in to comment.