Skip to content

Commit

Permalink
Polish QuarkusClassLoaderHandler and FastPathResolver.
Browse files Browse the repository at this point in the history
This is a follow up on issue classgraph#891 and the preceding PR classgraph#893 and brings two changes:

* The `QuarkusClassLoaderHandler` is a bit more explicit now about how the elements returned from the class loader are treated
* The `FastPathResolver` can be simplified using only one pattern
  • Loading branch information
michael-simons committed Nov 11, 2024
1 parent 9e18854 commit 329f492
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import nonapi.io.github.classgraph.classpath.ClassLoaderOrder;
Expand All @@ -53,6 +55,15 @@ class QuarkusClassLoaderHandler implements ClassLoaderHandler {
// Classloader since Quarkus 1.13
private static final String RUNNER_CLASSLOADER = "io.quarkus.bootstrap.runner.RunnerClassLoader";

// Class path elements prior to Quarkus 3.11
private static final Map<String, String> PRE_311_RESOURCE_BASED_ELEMENTS;
static {
Map<String, String> hlp = new HashMap<>();
hlp.put("io.quarkus.bootstrap.classloading.JarClassPathElement", "file");
hlp.put("io.quarkus.bootstrap.classloading.DirectoryClassPathElement", "root");
PRE_311_RESOURCE_BASED_ELEMENTS = Collections.unmodifiableMap(hlp);
}

/**
* Class cannot be constructed.
*/
Expand Down Expand Up @@ -119,13 +130,12 @@ private static void findClasspathOrderForQuarkusClassloader(final ClassLoader cl
final ClasspathOrder classpathOrder, final ScanSpec scanSpec, final LogNode log) {

Collection<Object> elements = findQuarkusClassLoaderElements(classLoader, classpathOrder);

for (final Object element : elements) {
final String elementClassName = element.getClass().getName();
if ("io.quarkus.bootstrap.classloading.JarClassPathElement".equals(elementClassName)) {
classpathOrder.addClasspathEntry(classpathOrder.reflectionUtils.getFieldVal(false, element, "file"),
classLoader, scanSpec, log);
} else if ("io.quarkus.bootstrap.classloading.DirectoryClassPathElement".equals(elementClassName)) {
classpathOrder.addClasspathEntry(classpathOrder.reflectionUtils.getFieldVal(false, element, "root"),
final String fieldName = PRE_311_RESOURCE_BASED_ELEMENTS.get(elementClassName);
if (fieldName != null) {
classpathOrder.addClasspathEntry(classpathOrder.reflectionUtils.getFieldVal(false, element, fieldName),
classLoader, scanSpec, log);
} else {
final Object rootPath = classpathOrder.reflectionUtils.invokeMethod(false, element, "getRoot");
Expand All @@ -137,7 +147,7 @@ private static void findClasspathOrderForQuarkusClassloader(final ClassLoader cl
}

@SuppressWarnings("unchecked")
private static Collection<Object> findQuarkusClassLoaderElements(ClassLoader classLoader, ClasspathOrder classpathOrder) {
private static Collection<Object> findQuarkusClassLoaderElements(final ClassLoader classLoader, final ClasspathOrder classpathOrder) {
Collection<Object> elements = (Collection<Object>) classpathOrder.reflectionUtils.getFieldVal(false,
classLoader, "elements");
if (elements == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public final class FastPathResolver {
/** Match %-encoded characters in URLs. */
private static final Pattern percentMatcher = Pattern.compile("([%][0-9a-fA-F][0-9a-fA-F])+");

/** Match custom URLs that are followed by two slashes. */
private static final Pattern schemeTwoSlashMatcher = Pattern.compile("^[a-zA-Z+\\-.]+://");

/** Match custom URLs that are followed by one slash. */
private static final Pattern schemeOneSlashMatcher = Pattern.compile("^[a-zA-Z+\\-.]+:/");
/** Match custom URLs that are followed by one or two slashes. */
private static final Pattern schemeOneOrTwoSlashMatcher = Pattern.compile("^[a-zA-Z+\\-.]+:/{1,2}");

/**
* Constructor.
Expand Down Expand Up @@ -236,24 +233,15 @@ public static String resolve(final String resolveBasePath, final String relative
} else {
// Preserve the number of slashes on custom URL schemes (#420)
final String relPath = startIdx == 0 ? relativePath : relativePath.substring(startIdx);
final Matcher m2 = schemeTwoSlashMatcher.matcher(relPath);
final Matcher m2 = schemeOneOrTwoSlashMatcher.matcher(relPath);
if (m2.find()) {
matchedPrefix = true;
final String m2Match = m2.group();
startIdx += m2Match.length();
prefix += m2Match;
final String match = m2.group();
startIdx += match.length();
prefix += match;
// Treat the part after the protocol as an absolute path, so the rest of the URL is not treated
// as a directory relative to the current directory.
isAbsolutePath = true;
} else {
final Matcher m1 = schemeOneSlashMatcher.matcher(relPath);
if (m1.find()) {
matchedPrefix = true;
final String m1Match = m1.group();
startIdx += m1Match.length();
prefix += m1Match;
isAbsolutePath = true;
}
}
}
} while (matchedPrefix);
Expand Down

0 comments on commit 329f492

Please sign in to comment.