Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClasspathElementDir review adjustments #851

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/main/java/io/github/classgraph/ClasspathElementDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -409,11 +410,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
// Only scan files in directory if directory is not only an ancestor of an accepted path
if (parentMatchStatus != ScanSpecPathMatch.ANCESTOR_OF_ACCEPTED_PATH) {
// Do preorder traversal (files in dir, then subdirs), to reduce filesystem cache misses
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
// Process files in dir before recursing
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Path subPathRelative = classpathEltPath.relativize(subPath);
final String subPathRelativeStr = FastPathResolver.resolve(subPathRelative.toString());
// If this is a modular jar, ignore all classfiles other than "module-info.class" in the
Expand Down Expand Up @@ -452,11 +455,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
}
} else if (scanSpec.enableClassInfo && dirRelativePathStr.equals("/")) {
// Always check for module descriptor in package root, even if package root isn't in accept
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
if (subPath.getFileName().toString().equals("module-info.class")) {
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Resource resource = newResource(subPath, fileAttributes);
addAcceptedResource(resource, parentMatchStatus, /* isClassfileOnly = */ true, subLog);
try {
Expand All @@ -472,7 +477,7 @@ private void scanPathRecursively(final Path path, final LogNode log) {
// Recurse into subdirectories
for (final Path subPath : pathsInDir) {
try {
if (FileUtils.isDir(subPath)) {
if (getFileAttributes.get(subPath).isDirectory()) {
scanPathRecursively(subPath, subLog);
}
} catch (final SecurityException e) {
Expand Down