Skip to content

Commit 4d4eb6b

Browse files
committed
Prepend actual class file.
1 parent 5cc5c73 commit 4d4eb6b

File tree

1 file changed

+70
-6
lines changed
  • byte-buddy-dep/src/main/java/net/bytebuddy/build

1 file changed

+70
-6
lines changed

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

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.bytebuddy.pool.TypePool;
2929
import net.bytebuddy.utility.CompoundList;
3030
import net.bytebuddy.utility.FileSystem;
31+
import net.bytebuddy.utility.StreamDrainer;
3132
import net.bytebuddy.utility.nullability.AlwaysNull;
3233
import net.bytebuddy.utility.nullability.MaybeNull;
3334

@@ -2959,7 +2960,7 @@ public static Source ofTypes(Collection<? extends Class<?>> types) {
29592960
/**
29602961
* Represents a collection of types as an in-memory source.
29612962
*
2962-
* @param types The types to represent.
2963+
* @param types The types to represent.
29632964
* @param versionedTypes A versioned mapping of types to represent.
29642965
* @return A source representing the supplied types.
29652966
*/
@@ -4884,16 +4885,20 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
48844885
while (name.startsWith("/")) {
48854886
name = name.substring(1);
48864887
}
4887-
if (name.endsWith(ClassFileLocator.CLASS_FILE_EXTENSION) && !name.endsWith(PACKAGE_INFO) && !name.equals(MODULE_INFO)) {
4888+
if (name.endsWith(ClassFileLocator.CLASS_FILE_EXTENSION)
4889+
&& (!name.startsWith("META-INF") && name.startsWith(ClassFileLocator.META_INF_VERSIONS))
4890+
&& !name.endsWith(PACKAGE_INFO)
4891+
&& !name.endsWith(MODULE_INFO)) {
48884892
try {
4893+
String typeName = name.substring(name.startsWith(ClassFileLocator.META_INF_VERSIONS)
4894+
? name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()) + 1
4895+
: 0, name.length() - ClassFileLocator.CLASS_FILE_EXTENSION.length()).replace('/', '.');
48894896
dispatcher.accept(new Preprocessor(element,
4890-
name.substring(name.startsWith(ClassFileLocator.META_INF_VERSIONS)
4891-
? name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()) + 1
4892-
: 0, name.length() - ClassFileLocator.CLASS_FILE_EXTENSION.length()).replace('/', '.'),
4897+
typeName,
48934898
name.startsWith(ClassFileLocator.META_INF_VERSIONS)
48944899
? Integer.parseInt(name.substring(ClassFileLocator.META_INF_VERSIONS.length(), name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length())))
48954900
: 0,
4896-
classFileLocator,
4901+
new SourceEntryPrependingClassFileLocator(typeName, element, classFileLocator),
48974902
typePool,
48984903
listener,
48994904
plugins,
@@ -4946,6 +4951,65 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
49464951
}
49474952
}
49484953

4954+
/**
4955+
* A class file locator that shadows a given {@link Source.Element}'s type with the explicit element.
4956+
* This avoids that caching yields the wrong class file in case of multi-release jars.
4957+
*/
4958+
@HashCodeAndEqualsPlugin.Enhance
4959+
protected static class SourceEntryPrependingClassFileLocator implements ClassFileLocator {
4960+
4961+
/**
4962+
* The name of the represented type.
4963+
*/
4964+
private final String name;
4965+
4966+
/**
4967+
* The corresponding source element.
4968+
*/
4969+
private final Source.Element element;
4970+
4971+
/**
4972+
* The actual class file locator to query for all other types.
4973+
*/
4974+
private final ClassFileLocator delegate;
4975+
4976+
/**
4977+
* Creates a class file locator that prepends a {@link Source.Element}.
4978+
*
4979+
* @param name The name of the represented type.
4980+
* @param element The corresponding source element.
4981+
* @param delegate The actual class file locator to query for all other types.
4982+
*/
4983+
protected SourceEntryPrependingClassFileLocator(String name, Source.Element element, ClassFileLocator delegate) {
4984+
this.name = name;
4985+
this.element = element;
4986+
this.delegate = delegate;
4987+
}
4988+
4989+
/**
4990+
* {@inheritDoc}
4991+
*/
4992+
public Resolution locate(String name) throws IOException {
4993+
if (name.endsWith(this.name)) {
4994+
InputStream inputStream = element.getInputStream();
4995+
try {
4996+
return new Resolution.Explicit(StreamDrainer.DEFAULT.drain(inputStream));
4997+
} finally {
4998+
inputStream.close();
4999+
}
5000+
} else {
5001+
return delegate.locate(name);
5002+
}
5003+
}
5004+
5005+
/**
5006+
* {@inheritDoc}
5007+
*/
5008+
public void close() throws IOException {
5009+
delegate.close();
5010+
}
5011+
}
5012+
49495013
/**
49505014
* A preprocessor for a parallel plugin engine.
49515015
*/

0 commit comments

Comments
 (0)