diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/sbom/ShadedPackageNameResolver.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/sbom/ShadedPackageNameResolver.java index ac94755ad..9f1dff816 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/sbom/ShadedPackageNameResolver.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/sbom/ShadedPackageNameResolver.java @@ -218,7 +218,7 @@ private Optional> resolveDirectoriesWithClasses(FileSystem jarFileSyst originalClassFiles.add(fileName.toString()); } }); - Optional> optionalPaths = resolveDirectoryFromClassNameMatching(artifact, originalClassFiles); + Optional> optionalPaths = resolveDirectoriesFromClassNameMatching(artifact, originalClassFiles); if (optionalPaths.isPresent()) { Set paths = optionalPaths.get(); visitedPathToClassFileDirectories.addAll(paths); @@ -261,36 +261,10 @@ private Set notVisitedPaths() { return difference; } - // private Optional resolveDirectoryFromClassNameMatching(ArtifactAdapter artifact, Set originalClassFiles) throws IOException { -// for (Path potentialDirectory : pathToClassFilesDirectories) { // notVisistedPaths -// /* Use AtomicBoolean as a wrapper class to allow updating the value from the anonymous inner class. */ -// AtomicBoolean successfulMatching = new AtomicBoolean(true); -// Files.walkFileTree(potentialDirectory, new SimpleFileVisitor<>() { -// @Override -// public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { -// if (file.toString().endsWith(".class")) { -// String fileName = file.getFileName().toString(); -// if (originalClassFiles.contains(fileName)) { -// return FileVisitResult.CONTINUE; -// } else { -// successfulMatching.set(false); -// return FileVisitResult.TERMINATE; -// } -// } -// return FileVisitResult.CONTINUE; -// } -// }); -// if (successfulMatching.get()) { -// return Optional.of(potentialDirectory); -// } -// } -// System.out.printf("❌ Failed matching for %s:%s\n", artifact.groupId, artifact.artifactId); -// return Optional.empty(); -// } - private Optional> resolveDirectoryFromClassNameMatching(ArtifactAdapter artifact, Set originalClassFiles) throws IOException { + private Optional> resolveDirectoriesFromClassNameMatching(ArtifactAdapter artifact, Set originalClassFiles) throws IOException { Set matchingDirectories = new HashSet<>(); - for (Path potentialDirectory : pathToClassFilesDirectories) { // notVisitedPaths + for (Path potentialDirectory : pathToClassFilesDirectories) { AtomicBoolean successfulMatching = new AtomicBoolean(true); try (DirectoryStream stream = Files.newDirectoryStream(potentialDirectory)) { @@ -314,69 +288,8 @@ private Optional> resolveDirectoryFromClassNameMatching(ArtifactAdapte return Optional.empty(); } return Optional.of(matchingDirectories); - -// // Find the directories with the fewest components -// OptionalInt minComponents = matchingDirectories.stream() -// .mapToInt(Path::getNameCount) -// .min(); -// -// if (minComponents.isPresent()) { -// // Collect all directories that have the minimum number of components -// Set smallestDirectories = matchingDirectories.stream() -// .filter(path -> path.getNameCount() == minComponents.getAsInt()) -// .collect(Collectors.toSet()); -// -// // If there's only one directory with the fewest components, return it -// if (smallestDirectories.size() == 1) { -// return Optional.of(smallestDirectories.iterator().next()); -// } -// -// // If there is a tie, take one of the directories and return its parent -// Path oneOfTheTiedDirectories = smallestDirectories.iterator().next(); -// Path parent = oneOfTheTiedDirectories.getParent(); -// -// // Return the parent, even if it's not part of matchingDirectories -// return Optional.of(parent); -// } -// -// return Optional.empty(); } - -// /** -// * Collects directories that contain .class files using a breadth-first search. -// * Processes files before adding directories to ensure no further directories are visited once a .class file is found. -// */ -// private Set collectPotentialDirectories(Path rootPath) throws IOException { -// Set potentialDirectories = new HashSet<>(); -// Queue directoriesToVisit = new LinkedList<>(); -// directoriesToVisit.add(rootPath); -// -// while (!directoriesToVisit.isEmpty()) { -// Path currentDir = directoriesToVisit.poll(); -// List subdirectories = new ArrayList<>(); -// try (DirectoryStream stream = Files.newDirectoryStream(currentDir)) { -// boolean classFileFound = false; -// -// for (Path entry : stream) { -// if (Files.isDirectory(entry) && !entry.getFileName().toString().equals("META-INF")) { -// subdirectories.add(entry); -// } else if (entry.toString().endsWith(".class")) { -// potentialDirectories.add(currentDir); -// classFileFound = true; -// break; -// } -// } -// -// if (!classFileFound) { -// directoriesToVisit.addAll(subdirectories); -// } -// } -// } -// -// return potentialDirectories; -// } - private Set collectPotentialDirectories(Path rootPath) throws IOException { Set potentialDirectories = new HashSet<>(); Files.walkFileTree(rootPath, new SimpleFileVisitor<>() {