Skip to content

Commit

Permalink
Further work on #673
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Apr 15, 2022
1 parent df4739b commit a223707
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 234 deletions.
20 changes: 12 additions & 8 deletions src/main/java/io/github/classgraph/ClasspathElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import io.github.classgraph.Scanner.ClasspathEntryWorkUnit;
import nonapi.io.github.classgraph.concurrency.WorkQueue;
Expand Down Expand Up @@ -77,7 +75,7 @@ abstract class ClasspathElement implements Comparable<ClasspathElement> {
* a Class-Path entry in the manifest). Set to -1 initially in case the same ClasspathElement is present twice
* in the classpath, as a child of different parent ClasspathElements.
*/
final AtomicInteger classpathElementIdxWithinParent = new AtomicInteger(-1);
final int classpathElementIdxWithinParent;

/**
* The child classpath elements, keyed by the order of the child classpath element within the Class-Path entry
Expand Down Expand Up @@ -105,7 +103,10 @@ abstract class ClasspathElement implements Comparable<ClasspathElement> {
protected final AtomicBoolean scanned = new AtomicBoolean(false);

/** The classloader that this classpath element was obtained from. */
protected AtomicReference<ClassLoader> classLoader = new AtomicReference<>();
protected ClassLoader classLoader;

/** The package root within the jarfile or Path. */
protected String packageRootPrefix;

/**
* The name of the module from the {@code module-info.class} module descriptor, if one is present in the root of
Expand All @@ -126,16 +127,19 @@ abstract class ClasspathElement implements Comparable<ClasspathElement> {
* @param scanSpec
* the scan spec
*/
ClasspathElement(final ScanSpec scanSpec) {
ClasspathElement(final ClasspathEntryWorkUnit workUnit, final ScanSpec scanSpec) {
this.packageRootPrefix = workUnit.packageRootPrefix;
this.classpathElementIdxWithinParent = workUnit.classpathElementIdxWithinParent;
this.classLoader = workUnit.classLoader;
this.scanSpec = scanSpec;
}

// -------------------------------------------------------------------------------------------------------------

// Sort in increasing order of classpathElementIdxWithinParent
/** Sort in increasing order of classpathElementIdxWithinParent. */
@Override
public int compareTo(final ClasspathElement other) {
return this.classpathElementIdxWithinParent.get() - other.classpathElementIdxWithinParent.get();
return this.classpathElementIdxWithinParent - other.classpathElementIdxWithinParent;
}

// -------------------------------------------------------------------------------------------------------------
Expand All @@ -146,7 +150,7 @@ public int compareTo(final ClasspathElement other) {
* @return the classloader
*/
ClassLoader getClassLoader() {
return classLoader.get();
return classLoader;
}

/**
Expand Down
54 changes: 21 additions & 33 deletions src/main/java/io/github/classgraph/ClasspathElementFileDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

import io.github.classgraph.Scanner.ClasspathEntryWorkUnit;
import nonapi.io.github.classgraph.classloaderhandler.ClassLoaderHandlerRegistry;
import nonapi.io.github.classgraph.classpath.ClasspathOrder.ClasspathElementAndPackageRoot;
import nonapi.io.github.classgraph.concurrency.WorkQueue;
import nonapi.io.github.classgraph.fastzipfilereader.LogicalZipFile;
import nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler;
Expand All @@ -64,9 +63,6 @@ class ClasspathElementFileDir extends ClasspathElement {
/** The directory at the root of the classpath element. */
private final File classpathEltDir;

/** The directory at the root of the package hierarchy. */
private final File packageRootDir;

/** Used to ensure that recursive scanning doesn't get into an infinite loop due to a link cycle. */
private final Set<String> scannedCanonicalPaths = new HashSet<>();

Expand All @@ -78,16 +74,17 @@ class ClasspathElementFileDir extends ClasspathElement {
*
* @param classpathEltDir
* the classpath element directory
* @param workUnit
* the work unit
* @param nestedJarHandler
* the nested jar handler
* @param scanSpec
* the scan spec
*/
ClasspathElementFileDir(final File classpathEltDir, final String packageRootPrefix,
ClasspathElementFileDir(final File classpathEltDir, final ClasspathEntryWorkUnit workUnit,
final NestedJarHandler nestedJarHandler, final ScanSpec scanSpec) {
super(scanSpec);
super(workUnit, scanSpec);
this.classpathEltDir = classpathEltDir;
this.packageRootDir = new File(classpathEltDir, packageRootPrefix);
this.nestedJarHandler = nestedJarHandler;
}

Expand Down Expand Up @@ -121,28 +118,27 @@ void open(final WorkQueue<ClasspathEntryWorkUnit> workQueue, final LogNode log)
if (log != null) {
log(classpathElementIdx, "Found lib jar: " + file, log);
}
workQueue.addWorkUnit(new ClasspathEntryWorkUnit(
new ClasspathElementAndPackageRoot(file.getPath(), getClassLoader()),
workQueue.addWorkUnit(new ClasspathEntryWorkUnit(file.getPath(), getClassLoader(),
/* parentClasspathElement = */ this,
/* orderWithinParentClasspathElement = */ childClasspathEntryIdx++));
/* orderWithinParentClasspathElement = */ childClasspathEntryIdx++,
/* packageRootPrefix = */ ""));
}
}
}
}
}
// Only look for package roots if the package root is the root of the classpath element
if (packageRootDir.equals(classpathEltDir)) {
// Only look for package roots if the package root is empty
if (packageRootPrefix.isEmpty()) {
for (final String packageRootPrefix : ClassLoaderHandlerRegistry.AUTOMATIC_PACKAGE_ROOT_PREFIXES) {
final File packageRoot = new File(classpathEltDir, packageRootPrefix);
if (FileUtils.canReadAndIsDir(packageRoot)) {
if (log != null) {
log(classpathElementIdx, "Found package root: " + packageRoot, log);
}
workQueue.addWorkUnit(new ClasspathEntryWorkUnit(
new ClasspathElementAndPackageRoot(classpathEltDir, packageRootPrefix,
getClassLoader()),
workQueue.addWorkUnit(new ClasspathEntryWorkUnit(packageRoot, getClassLoader(),
/* parentClasspathElement = */ this,
/* orderWithinParentClasspathElement = */ childClasspathEntryIdx++));
/* orderWithinParentClasspathElement = */ childClasspathEntryIdx++,
packageRootPrefix));
}
}
}
Expand Down Expand Up @@ -186,14 +182,7 @@ public String getPath() {

@Override
public String getPathRelativeToClasspathElement() {
// Relativize resource file to classpath element dir
final File resourceFile = new File(packageRootDir, pathRelativeToPackageRoot);
String pathRelativeToClasspathElt = FastPathResolver
.resolve(resourceFile.getPath().substring(classpathEltDir.getPath().length()));
while (pathRelativeToClasspathElt.startsWith("/")) {
pathRelativeToClasspathElt = pathRelativeToClasspathElt.substring(1);
}
return pathRelativeToClasspathElt;
return packageRootPrefix.isEmpty() ? getPath() : packageRootPrefix + getPath();
}

@Override
Expand Down Expand Up @@ -303,7 +292,7 @@ public void close() {
*/
@Override
Resource getResource(final String pathRelativeToPackageRoot) {
final File resourceFile = new File(packageRootDir, pathRelativeToPackageRoot);
final File resourceFile = new File(classpathEltDir, pathRelativeToPackageRoot);
return FileUtils.canReadAndIsFile(resourceFile)
? newResource(pathRelativeToPackageRoot, resourceFile, nestedJarHandler)
: null;
Expand Down Expand Up @@ -340,7 +329,7 @@ private void scanDirRecursively(final File dir, final LogNode log) {
}

final String dirPath = dir.getPath();
final int ignorePrefixLen = packageRootDir.getPath().length() + 1;
final int ignorePrefixLen = classpathEltDir.getPath().length() + 1;
final String dirRelativePath = ignorePrefixLen > dirPath.length() ? "/" //
: dirPath.substring(ignorePrefixLen).replace(File.separatorChar, '/') + "/";
final boolean isDefaultPackage = "/".equals(dirRelativePath);
Expand Down Expand Up @@ -490,9 +479,9 @@ void scanPaths(final LogNode log) {
}

final LogNode subLog = log == null ? null
: log(classpathElementIdx, "Scanning directory classpath element " + packageRootDir, log);
: log(classpathElementIdx, "Scanning directory classpath element " + classpathEltDir, log);

scanDirRecursively(packageRootDir, subLog);
scanDirRecursively(classpathEltDir, subLog);

finishScanPaths(subLog);
}
Expand Down Expand Up @@ -523,7 +512,7 @@ public File getFile() {
*/
@Override
URI getURI() {
return packageRootDir.toURI();
return classpathEltDir.toURI();
}

@Override
Expand All @@ -538,15 +527,15 @@ List<URI> getAllURIs() {
*/
@Override
public String toString() {
return packageRootDir.toString();
return classpathEltDir.toString();
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(classpathEltDir, packageRootDir);
return Objects.hash(classpathEltDir);
}

/* (non-Javadoc)
Expand All @@ -560,7 +549,6 @@ public boolean equals(final Object obj) {
return false;
}
final ClasspathElementFileDir other = (ClasspathElementFileDir) obj;
return Objects.equals(this.classpathEltDir, other.classpathEltDir)
&& Objects.equals(this.packageRootDir, other.packageRootDir);
return Objects.equals(this.classpathEltDir, other.classpathEltDir);
}
}
12 changes: 5 additions & 7 deletions src/main/java/io/github/classgraph/ClasspathElementModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ class ClasspathElementModule extends ClasspathElement {
*
* @param moduleRef
* the module ref
* @param workUnit
* the work unit
* @param moduleRefToModuleReaderProxyRecyclerMap
* the module ref to module reader proxy recycler map
* @param scanSpec
* the scan spec
*/
ClasspathElementModule(final ModuleRef moduleRef,
final SingletonMap<ModuleRef, Recycler<ModuleReaderProxy, IOException>, IOException> //
moduleRefToModuleReaderProxyRecyclerMap, final ScanSpec scanSpec) {
super(scanSpec);
moduleRefToModuleReaderProxyRecyclerMap, final ClasspathEntryWorkUnit workUnit,
final ScanSpec scanSpec) {
super(workUnit, scanSpec);
this.moduleRefToModuleReaderProxyRecyclerMap = moduleRefToModuleReaderProxyRecyclerMap;
this.moduleRef = moduleRef;
}
Expand Down Expand Up @@ -141,11 +144,6 @@ public String getPath() {
return resourcePath;
}

@Override
public String getPathRelativeToClasspathElement() {
return resourcePath;
}

@Override
public long getLastModified() {
return 0L; // Unknown
Expand Down

0 comments on commit a223707

Please sign in to comment.