Skip to content

Commit 1e38dd0

Browse files
committed
Adjust class file handling.
1 parent 0bc0178 commit 1e38dd0

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

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

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,12 +3399,12 @@ interface Sink extends Closeable {
33993399
/**
34003400
* Stores the supplied binary representation of types in this sink.
34013401
*
3402-
* @param version The version of the multi-release jar file, which should at least be {@code 8} as previous
3402+
* @param classFileVersion The version of the multi-release jar file, which should at least be {@code 8} as previous
34033403
* versions are not recognized by regular class loaders.
34043404
* @param binaryRepresentations The binary representations to store.
34053405
* @throws IOException If an I/O error occurs.
34063406
*/
3407-
void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException;
3407+
void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException;
34083408

34093409
/**
34103410
* Retains the supplied element in its original form.
@@ -3447,9 +3447,13 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOE
34473447
/**
34483448
* {@inheritDoc}
34493449
*/
3450-
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3450+
public void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
34513451
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3452-
outputStream.putNextEntry(new JarEntry(ClassFileLocator.META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION));
3452+
outputStream.putNextEntry(new JarEntry(ClassFileLocator.META_INF_VERSIONS
3453+
+ classFileVersion.getJavaVersion()
3454+
+ "/"
3455+
+ entry.getKey().getInternalName()
3456+
+ ClassFileLocator.CLASS_FILE_EXTENSION));
34533457
outputStream.write(entry.getValue());
34543458
outputStream.closeEntry();
34553459
}
@@ -3512,7 +3516,7 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
35123516
/**
35133517
* {@inheritDoc}
35143518
*/
3515-
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3519+
public void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
35163520
/* do nothing */
35173521
}
35183522

@@ -3585,9 +3589,13 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) {
35853589
/**
35863590
* {@inheritDoc}
35873591
*/
3588-
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3592+
public void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
35893593
for (Map.Entry<TypeDescription, byte[]> entry : binaryRepresentations.entrySet()) {
3590-
storage.put(ClassFileLocator.META_INF_VERSIONS + version + "/" + entry.getKey().getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
3594+
storage.put(ClassFileLocator.META_INF_VERSIONS
3595+
+ classFileVersion.getJavaVersion()
3596+
+ "/"
3597+
+ entry.getKey().getInternalName()
3598+
+ ClassFileLocator.CLASS_FILE_EXTENSION, entry.getValue());
35913599
}
35923600
}
35933601

@@ -3761,8 +3769,8 @@ public void store(Map<TypeDescription, byte[]> binaryRepresentations) throws IOE
37613769
/**
37623770
* {@inheritDoc}
37633771
*/
3764-
public void store(int version, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3765-
doStore(new File(folder, ClassFileLocator.META_INF_VERSIONS + version), binaryRepresentations);
3772+
public void store(ClassFileVersion classFileVersion, Map<TypeDescription, byte[]> binaryRepresentations) throws IOException {
3773+
doStore(new File(folder, ClassFileLocator.META_INF_VERSIONS + classFileVersion.getJavaVersion()), binaryRepresentations);
37663774
}
37673775

37683776
/**
@@ -3877,7 +3885,7 @@ interface Materializable {
38773885
void materialize(Target.Sink sink,
38783886
List<TypeDescription> transformed,
38793887
Map<TypeDescription,
3880-
List<Throwable>> failed,
3888+
List<Throwable>> failed,
38813889
List<String> unresolved) throws IOException;
38823890

38833891
/**
@@ -3886,9 +3894,10 @@ void materialize(Target.Sink sink,
38863894
class ForTransformedElement implements Materializable {
38873895

38883896
/**
3889-
* The multi-release Java version number or {@code 0}.
3897+
* The multi-release class file version number or {@code null} if a regular class.
38903898
*/
3891-
private final int version;
3899+
@MaybeNull
3900+
private final ClassFileVersion classFileVersion;
38923901

38933902
/**
38943903
* The type that has been transformed.
@@ -3898,11 +3907,11 @@ class ForTransformedElement implements Materializable {
38983907
/**
38993908
* Creates a new materializable for a successfully transformed type.
39003909
*
3901-
* @param version The multi-release Java version number or {@code 0}.
3902-
* @param dynamicType The type that has been transformed.
3910+
* @param classFileVersion The multi-release class file version number or {@code null} if a regular class.
3911+
* @param dynamicType The type that has been transformed.
39033912
*/
3904-
protected ForTransformedElement(int version, DynamicType dynamicType) {
3905-
this.version = version;
3913+
protected ForTransformedElement(@MaybeNull ClassFileVersion classFileVersion, DynamicType dynamicType) {
3914+
this.classFileVersion = classFileVersion;
39063915
this.dynamicType = dynamicType;
39073916
}
39083917

@@ -3912,12 +3921,12 @@ protected ForTransformedElement(int version, DynamicType dynamicType) {
39123921
public void materialize(Target.Sink sink,
39133922
List<TypeDescription> transformed,
39143923
Map<TypeDescription,
3915-
List<Throwable>> failed,
3924+
List<Throwable>> failed,
39163925
List<String> unresolved) throws IOException {
3917-
if (version == 0) {
3926+
if (classFileVersion == null) {
39183927
sink.store(dynamicType.getAllTypes());
39193928
} else {
3920-
sink.store(version, dynamicType.getAllTypes());
3929+
sink.store(classFileVersion, dynamicType.getAllTypes());
39213930
}
39223931
transformed.add(dynamicType.getTypeDescription());
39233932
}
@@ -3948,7 +3957,7 @@ protected ForRetainedElement(Source.Element element) {
39483957
public void materialize(Target.Sink sink,
39493958
List<TypeDescription> transformed,
39503959
Map<TypeDescription,
3951-
List<Throwable>> failed,
3960+
List<Throwable>> failed,
39523961
List<String> unresolved) throws IOException {
39533962
sink.retain(element);
39543963
}
@@ -3993,7 +4002,7 @@ protected ForFailedElement(Source.Element element, TypeDescription typeDescripti
39934002
public void materialize(Target.Sink sink,
39944003
List<TypeDescription> transformed,
39954004
Map<TypeDescription,
3996-
List<Throwable>> failed,
4005+
List<Throwable>> failed,
39974006
List<String> unresolved) throws IOException {
39984007
sink.retain(element);
39994008
failed.put(typeDescription, errored);
@@ -4032,7 +4041,7 @@ protected ForUnresolvedElement(Source.Element element, String typeName) {
40324041
public void materialize(Target.Sink sink,
40334042
List<TypeDescription> transformed,
40344043
Map<TypeDescription,
4035-
List<Throwable>> failed,
4044+
List<Throwable>> failed,
40364045
List<String> unresolved) throws IOException {
40374046
sink.retain(element);
40384047
unresolved.add(typeName);
@@ -4951,20 +4960,20 @@ public Summary apply(Source source, Target target, List<? extends Plugin.Factory
49514960
&& !name.endsWith(PACKAGE_INFO)
49524961
&& !name.endsWith(MODULE_INFO)) {
49534962
try {
4954-
int version = name.startsWith(ClassFileLocator.META_INF_VERSIONS)
4955-
? Math.max(7, Integer.parseInt(name.substring(ClassFileLocator.META_INF_VERSIONS.length(), name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()))))
4956-
: 0;
4957-
if (version == 0 || version > 7
4958-
&& classFileVersion != null
4959-
&& classFileVersion.isAtLeast(ClassFileVersion.JAVA_V9)
4960-
&& version <= classFileVersion.getJavaVersion()) {
4963+
ClassFileVersion classFileVersion = name.startsWith(ClassFileLocator.META_INF_VERSIONS)
4964+
? ClassFileVersion.ofJavaVersion(Integer.parseInt(name.substring(ClassFileLocator.META_INF_VERSIONS.length(), name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()))))
4965+
: null;
4966+
if (classFileVersion == null || classFileVersion.isAtLeast(ClassFileVersion.JAVA_V8)
4967+
&& this.classFileVersion != null
4968+
&& this.classFileVersion.isAtLeast(ClassFileVersion.JAVA_V9)
4969+
&& classFileVersion.isAtMost(this.classFileVersion)) {
49614970
String typeName = name.substring(name.startsWith(ClassFileLocator.META_INF_VERSIONS)
49624971
? name.indexOf('/', ClassFileLocator.META_INF_VERSIONS.length()) + 1
49634972
: 0, name.length() - ClassFileLocator.CLASS_FILE_EXTENSION.length()).replace('/', '.');
49644973
dispatcher.accept(new Preprocessor(element,
49654974
typeName,
4966-
version,
49674975
new SourceEntryPrependingClassFileLocator(typeName, element, classFileLocator),
4976+
classFileVersion,
49684977
typePool,
49694978
listener,
49704979
plugins,
@@ -5096,14 +5105,16 @@ private class Preprocessor implements Callable<Callable<? extends Dispatcher.Mat
50965105
private final String typeName;
50975106

50985107
/**
5099-
* The multi-release Java version number or {@code 0}.
5108+
* The class file locator to use.
51005109
*/
5101-
private final int version;
5110+
private final ClassFileLocator classFileLocator;
51025111

51035112
/**
5104-
* The class file locator to use.
5113+
* The multi-release class file version or {@code null} for a regular class.
51055114
*/
5106-
private final ClassFileLocator classFileLocator;
5115+
@MaybeNull
5116+
@HashCodeAndEqualsPlugin.ValueHandling(HashCodeAndEqualsPlugin.ValueHandling.Sort.REVERSE_NULLABILITY)
5117+
private final ClassFileVersion classFileVersion;
51075118

51085119
/**
51095120
* The type pool to use.
@@ -5130,25 +5141,25 @@ private class Preprocessor implements Callable<Callable<? extends Dispatcher.Mat
51305141
*
51315142
* @param element The processed element.
51325143
* @param typeName The name of the processed type.
5133-
* @param version The multi-release Java version number or {@code 0}.
51345144
* @param classFileLocator The class file locator to use.
5145+
* @param classFileVersion The multi-release class file version or {@code null} for a regular class.
51355146
* @param typePool The type pool to use.
51365147
* @param listener The listener to notify.
51375148
* @param plugins The plugins to apply.
51385149
* @param preprocessors The plugins with preprocessors to preprocess.
51395150
*/
51405151
private Preprocessor(Source.Element element,
51415152
String typeName,
5142-
int version,
51435153
ClassFileLocator classFileLocator,
5154+
@MaybeNull ClassFileVersion classFileVersion,
51445155
TypePool typePool,
51455156
Listener listener,
51465157
List<Plugin> plugins,
51475158
List<WithPreprocessor> preprocessors) {
51485159
this.element = element;
51495160
this.typeName = typeName;
5150-
this.version = version;
51515161
this.classFileLocator = classFileLocator;
5162+
this.classFileVersion = classFileVersion;
51525163
this.typePool = typePool;
51535164
this.listener = listener;
51545165
this.plugins = plugins;
@@ -5168,7 +5179,7 @@ public Callable<Dispatcher.Materializable> call() throws Exception {
51685179
for (WithPreprocessor preprocessor : preprocessors) {
51695180
preprocessor.onPreprocess(typeDescription, classFileLocator);
51705181
}
5171-
return new Resolved(version, typeDescription);
5182+
return new Resolved(classFileVersion, typeDescription);
51725183
} else {
51735184
return new Ignored(typeDescription);
51745185
}
@@ -5193,9 +5204,10 @@ public Callable<Dispatcher.Materializable> call() throws Exception {
51935204
private class Resolved implements Callable<Dispatcher.Materializable> {
51945205

51955206
/**
5196-
* The multi-release Java version number or {@code 0}.
5207+
* The multi-release Java version number or {@code null} if a regular class.
51975208
*/
5198-
private final int version;
5209+
@MaybeNull
5210+
private final ClassFileVersion classFileVersion;
51995211

52005212
/**
52015213
* A description of the resolved type.
@@ -5205,11 +5217,11 @@ private class Resolved implements Callable<Dispatcher.Materializable> {
52055217
/**
52065218
* Creates a new resolved materializable.
52075219
*
5208-
* @param version The multi-release Java version number or {@code 0}.
5209-
* @param typeDescription A description of the resolved type.
5220+
* @param classFileVersion The multi-release Java version number or {@code null} if a regular class.
5221+
* @param typeDescription A description of the resolved type.
52105222
*/
5211-
private Resolved(int version, TypeDescription typeDescription) {
5212-
this.version = version;
5223+
private Resolved(@MaybeNull ClassFileVersion classFileVersion, TypeDescription typeDescription) {
5224+
this.classFileVersion = classFileVersion;
52135225
this.typeDescription = typeDescription;
52145226
}
52155227

@@ -5248,7 +5260,7 @@ public Dispatcher.Materializable call() {
52485260
listener.onLiveInitializer(typeDescription, entry.getKey());
52495261
}
52505262
}
5251-
return new Dispatcher.Materializable.ForTransformedElement(version, dynamicType);
5263+
return new Dispatcher.Materializable.ForTransformedElement(classFileVersion, dynamicType);
52525264
} catch (Throwable throwable) {
52535265
errored.add(throwable);
52545266
listener.onError(typeDescription, errored);

byte-buddy-dep/src/test/java/net/bytebuddy/build/PluginEngineTargetInMemoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public void testMultiVersion() throws Exception {
7575
Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory();
7676
Plugin.Engine.Target.Sink sink = target.write(Plugin.Engine.Source.Origin.NO_MANIFEST);
7777
sink.store(Collections.singletonMap(TypeDescription.ForLoadedType.of(Object.class), new byte[]{1, 2, 3}));
78-
sink.store(11, Collections.singletonMap(TypeDescription.ForLoadedType.of(Object.class), new byte[]{4, 5, 6}));
79-
sink.store(17, Collections.singletonMap(TypeDescription.ForLoadedType.of(Object.class), new byte[]{7, 8, 9}));
78+
sink.store(ClassFileVersion.JAVA_V11, Collections.singletonMap(TypeDescription.ForLoadedType.of(Object.class), new byte[]{4, 5, 6}));
79+
sink.store(ClassFileVersion.JAVA_V17, Collections.singletonMap(TypeDescription.ForLoadedType.of(Object.class), new byte[]{7, 8, 9}));
8080
sink.close();
8181
assertThat(target.getStorage().size(), is(3));
8282
assertThat(target.getStorage().get(TypeDescription.ForLoadedType.of(Object.class).getInternalName() + ClassFileLocator.CLASS_FILE_EXTENSION), is(new byte[]{1, 2, 3}));

0 commit comments

Comments
 (0)