<div dir="ltr"><div dir="ltr">On Tue, Sep 24, 2024 at 12:44 PM Rafael Winterhalter <<a href="mailto:rafael.wth@gmail.com">rafael.wth@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr">Without getting lost in specifics:</p></blockquote><div>It is also possible to get very lost by lacking specifics :-)</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<p dir="ltr">If the build plugin now wants to create instrumented versions of class files for a Java 17 JVM, while the build is run on a Java 21 JVM, I can support this for JAR files, for folders, but I cannot fetch the adequate resource when the underlying resource accessor is a class loader. I do not think that I can work around this, or do I overlook something?</p></blockquote><div>Thanks, this narrows down your use case considerably and also explains why you would be interested in loading resources of versions different than the runtime version.</div><div><br></div><div>Does the class loader in question return JAR-form URLs? If that's the case, you could use JarURLConnection.getJarFile() and use getInputStream to get the versioned resource of choice:</div><div><br></div><span style="font-family:monospace">ClassLoader loader = new URLClassLoader(new URL[] {zip.toUri().toURL()});</span><br style="font-family:monospace"><span style="font-family:monospace">URL resource = loader.getResource(baseName);</span><br style="font-family:monospace"><span style="font-family:monospace">if (resource.openConnection() instanceof JarURLConnection con) {</span><br style="font-family:monospace"><span style="font-family:monospace">    try (var is = con.getJarFile().</span><span style="font-family:monospace">getInputStream(new ZipEntry(baseName))) {</span><br style="font-family:monospace"><span style="font-family:monospace">        assertArrayEquals("base".</span><span style="font-family:monospace">getBytes(StandardCharsets.UTF_</span><span style="font-family:monospace">8),</span><br style="font-family:monospace"><span style="font-family:monospace">                is.readAllBytes());</span><br style="font-family:monospace"><span style="font-family:monospace">    }</span><br style="font-family:monospace"><div><span style="font-family:monospace">}</span></div><div><br></div><div>Or just parse the URL to find the path to the JAR file and open it using ZipFile/JarFile APIs.</div><div><br></div><div>Is the issue reported to Byte Buffy publicly available?</div><div><br></div><div>Thanks,</div><div>Eirik. </div></div></div>