Skip to content

Commit

Permalink
Improve in-memory source for build plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 25, 2024
1 parent 40707d8 commit 27d4c5e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
73 changes: 58 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 @@ -2947,7 +2947,7 @@ public InMemory(Map<String, byte[]> 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.
Expand All @@ -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<? extends Class<?>> types) {
return ofTypes(types, Collections.<ClassFileVersion, Collection<Class<?>>>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<? extends Class<?>> types, Map<ClassFileVersion, Collection<Class<?>>> versionedTypes) {
Map<ClassFileVersion, Map<TypeDescription, byte[]>> versionedBinaryRepresentations = new HashMap<ClassFileVersion, Map<TypeDescription, byte[]>>();
for (Map.Entry<ClassFileVersion, Collection<Class<?>>> entry : versionedTypes.entrySet()) {
Map<TypeDescription, byte[]> binaryRepresentations = new HashMap<TypeDescription, byte[]>();
for (Class<?> type : entry.getValue()) {
binaryRepresentations.put(TypeDescription.ForLoadedType.of(type), ClassFileLocator.ForClassLoader.read(type));
}
versionedBinaryRepresentations.put(entry.getKey(), binaryRepresentations);
}
Map<TypeDescription, byte[]> binaryRepresentations = new HashMap<TypeDescription, byte[]>();
for (Class<?> type : types) {
binaryRepresentations.put(TypeDescription.ForLoadedType.of(type), ClassFileLocator.ForClassLoader.read(type));
}
return ofTypes(binaryRepresentations);
return ofTypes(binaryRepresentations, versionedBinaryRepresentations);
}

/**
Expand All @@ -2977,10 +2996,29 @@ public static Source ofTypes(Collection<? extends Class<?>> types) {
* @return A source representing the supplied types.
*/
public static Source ofTypes(Map<TypeDescription, byte[]> binaryRepresentations) {
return ofTypes(binaryRepresentations, Collections.<ClassFileVersion, Map<TypeDescription, byte[]>>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<TypeDescription, byte[]> binaryRepresentations,
Map<ClassFileVersion, Map<TypeDescription, byte[]>> versionedBinaryRepresentations
) {
Map<String, byte[]> storage = new HashMap<String, byte[]>();
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
storage.put(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
}
for (Map.Entry<ClassFileVersion, Map<TypeDescription, byte[]>> versioned : versionedBinaryRepresentations.entrySet()) {
for (Map.Entry<TypeDescription, byte[]> entry : versioned.getValue().entrySet()) {
storage.put(META_INF_VERSIONS + versioned.getKey().getJavaVersion() + "/" + entry.getKey().getInternalName() + CLASS_FILE_EXTENSION, entry.getValue());
}
}
return new InMemory(storage);
}

Expand Down Expand Up @@ -4857,18 +4895,23 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
name = name.substring(1);
}
if (name.endsWith(CLASS_FILE_EXTENSION) && !name.endsWith(PACKAGE_INFO) && !name.equals(MODULE_INFO)) {
dispatcher.accept(new Preprocessor(element,
name.substring(name.startsWith(META_INF_VERSIONS)
? name.indexOf('/', META_INF_VERSIONS.length()) + 1
: 0, name.length() - CLASS_FILE_EXTENSION.length()).replace('/', '.'),
name.startsWith(META_INF_VERSIONS)
? Integer.parseInt(name.substring(META_INF_VERSIONS.length(), name.indexOf('/', META_INF_VERSIONS.length())))
: 0,
classFileLocator,
typePool,
listener,
plugins,
preprocessors), preprocessors.isEmpty());
try {
dispatcher.accept(new Preprocessor(element,
name.substring(name.startsWith(META_INF_VERSIONS)
? name.indexOf('/', META_INF_VERSIONS.length()) + 1
: 0, name.length() - CLASS_FILE_EXTENSION.length()).replace('/', '.'),
name.startsWith(META_INF_VERSIONS)
? Integer.parseInt(name.substring(META_INF_VERSIONS.length(), name.indexOf('/', META_INF_VERSIONS.length())))
: 0,
classFileLocator,
typePool,
listener,
plugins,
preprocessors), preprocessors.isEmpty());
} catch (NumberFormatException ignored) {
listener.onResource(name);
sink.retain(element);
}
} else if (!name.equals(JarFile.MANIFEST_NAME)) {
listener.onResource(name);
sink.retain(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ private static ClassFileLocator of(JarFile jarFile, ClassFileVersion classFileVe
} else {
Manifest manifest = jarFile.getManifest();
int[] version;
if (Boolean.parseBoolean(manifest.getMainAttributes().getValue(MultiReleaseAware.MULTI_RELEASE_ATTRIBUTE))) {
if (manifest != null && Boolean.parseBoolean(manifest.getMainAttributes().getValue(MultiReleaseAware.MULTI_RELEASE_ATTRIBUTE))) {
SortedSet<Integer> versions = new TreeSet<Integer>();
Enumeration<JarEntry> enumeration = jarFile.entries();
while (enumeration.hasMoreElements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 27d4c5e

Please sign in to comment.