Skip to content

Commit

Permalink
Merge pull request #793 from Sineaggi/fix-patched-module-resource-lookup
Browse files Browse the repository at this point in the history
Fix patched module resource lookup
  • Loading branch information
lukehutch committed Oct 13, 2023
2 parents 6d43e40 + e68c674 commit d126d8c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/io/github/classgraph/ClasspathElementModule.java
Expand Up @@ -183,6 +183,20 @@ ClassfileReader openClassfile() throws IOException {
return new ClassfileReader(open(), this);
}

@Override
public URI getURI() {
try {
ModuleReaderProxy localModuleReaderProxy = moduleReaderProxyRecycler.acquire();
try {
return localModuleReaderProxy.find(resourcePath);
} finally {
moduleReaderProxyRecycler.recycle(localModuleReaderProxy);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public InputStream open() throws IOException {
if (skipClasspathElement) {
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/io/github/classgraph/ModuleReaderProxy.java
Expand Up @@ -31,6 +31,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;

Expand Down Expand Up @@ -183,4 +184,27 @@ public void release(final ByteBuffer byteBuffer) {
reflectionUtils.invokeMethod(/* throwException = */ true, moduleReader, "release", ByteBuffer.class,
byteBuffer);
}
}

/**
* Use the proxied ModuleReader to find the named resource as a URI.
*
* @param path
* The path to the resource to open.
* @return A {@link URI} for the resource.
* @throws SecurityException
* If the module cannot be accessed.
*/
public URI find(final String path) {
final Object /* Optional<URI> */ optionalURI = reflectionUtils.invokeMethod(/* throwException = */ true, moduleReader, "find", String.class,
path);
if (optionalURI == null) {
throw new IllegalArgumentException("Got null result from ModuleReader#find(String)");
}
final URI uri = (URI) reflectionUtils.invokeMethod(/* throwException = */ true,
optionalURI, "get");
if (uri == null) {
throw new IllegalArgumentException("Got null result from ModuleReader#find(String).get()");
}
return uri;
}
}

0 comments on commit d126d8c

Please sign in to comment.