<div dir="ltr"><div>This would likely work for those cases. The Byte Buddy plugins for Gradle and Maven discover today what release version is configured in the compile step, and uses that version for configuring the class file locators that it can configure. If a release version is for example set to Java 17, the class file locators would discover MR class files for Java 17 on both a version 17 and a version 21 VM, where possible. Jar files are then initialized using this version, and the same goes for reading from folders. If I could supply a version to a URLClassLoader, too, I would also set that version and could supply URLs to it as it is done today. To some degree, those who implement their custom class loaders would anyways be responsible to implement such a mechanism for those. Mainly I was wondering if the API should be standardized via a method, but your suggested approach would of course work, too, in practice.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 24. Sept. 2024 um 20:11 Uhr schrieb Alan Bateman <<a href="mailto:alan.bateman@oracle.com">alan.bateman@oracle.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On 24/09/2024 17:47, Rafael Winterhalter wrote:<br>
> Byte Buddy attempts to avoid class loading during build <br>
> instrumentation, as this might have side-effects when types are <br>
> loaded, and described, using the reflection API. Therefore Byte Buddy <br>
> parses class files itself and offers its own representation. 99.9% of <br>
> users will be able to provide all relevant class files by pointing to <br>
> a jar file or a folder with classes, and Byte Buddy will treat this as <br>
> a form of class path to describe the dependency tree to the build time <br>
> instrumentation, without loading any classes. As an option for the <br>
> reminding 0.1%, dependencies can also be provided using a ClassLoader. <br>
> A case I have encountered more than once is that a custom ClassLoader <br>
> generates classes on demand if queried. To support this, Byte Buddy <br>
> then queries the ClassLoader for ".class" files also, without <br>
> processing a jar file or folder directly.<br>
><br>
> As of today, I have no way to tell the class loader that the target <br>
> version of the build plugin is for example Java 17, and that I want <br>
> the Java 17 version of a class file. I can query for META-INF/versions <br>
> 8-17 to see if there is a class file for that name (rather <br>
> inefficient). But if the build is run on a Java 21 JVM and there is a <br>
> META-INF/versions/21 version of a class, the Java 17 "original" class <br>
> file is not available to me. This is why I was wondering if there <br>
> should be a method to query a resource "as if" it was requested for a <br>
> particular version that is not the current JVM (or <br>
> JarFile-constructor-supplied) version.<br>
><br>
> Does this describe my problem better? I understand that this really is <br>
> an edge case. Then again I think this is a legitimate problem that <br>
> should be solvable. And with a shift towards build-time <br>
> instrumentation and likely growing adoption of MR JAR files, I would <br>
> of course want to solve this in Byte Buddy, if given the opportunity.<br>
><br>
Thanks, I think the scenario is a much clearer now but I don't think <br>
adding a method to ClassLoader to aid this scenario is the right thing <br>
to do.<br>
<br>
Instead, this feels like a ClassLoader implementation that is created <br>
with a Runtime.Version and excels at serving up resources. That's <br>
effectively what you get with using JDK tools like jdeps with the <br>
--multi-release N option. That ClassLoader implementation would create <br>
JarFiles with the Runtime.Version so it will locate the resources in MR <br>
JARs for that version. If you had such a ClassLoader implementation, <br>
could you fit into the build steps?<br>
<br>
-Alan<br>
</blockquote></div>