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

8332226: "Invalid package name:" from source launcher #19245

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -32,13 +32,16 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.module.InvalidModuleDescriptorException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.lang.model.SourceVersion;

@@ -115,37 +118,62 @@ public boolean isModular() {
}

public Set<String> computePackageNames() {
try (var stream = Files.find(sourceRootPath, 99, (path, attr) -> attr.isDirectory())) {
var names = new TreeSet<String>();
stream.filter(ProgramDescriptor::containsAtLeastOneRegularFile)
.map(sourceRootPath::relativize)
.filter(ProgramDescriptor::composedOfValidPackageNameElements)
.map(Path::toString)
.filter(string -> !string.isEmpty())
.map(string -> string.replace(File.separatorChar, '.'))
.forEach(names::add);
return names;
} catch (IOException exception) {
throw new UncheckedIOException(exception);
return explodedPackages(sourceRootPath);
}

// -- exploded directories --> based on jdk.internal.module.ModulePath

private static Set<String> explodedPackages(Path dir) {
String separator = dir.getFileSystem().getSeparator();
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
(path, attrs) -> attrs.isRegularFile() && !isHidden(path))) {
return stream.map(dir::relativize)
.map(path -> toPackageName(path, separator))
.flatMap(Optional::stream)
.collect(Collectors.toSet());
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}

private static boolean composedOfValidPackageNameElements(Path path) {
if (path.getNameCount() == 0) return false;
for (var element : path) {
var name = element.toString();
if (!SourceVersion.isIdentifier(name)) {
return false;
/**
* Maps the relative path of an entry in an exploded module to a package
* name.
*
* @throws InvalidModuleDescriptorException if the name is a class file in
* the top-level directory (and it's not module-info.class)
*/
private static Optional<String> toPackageName(Path file, String separator) {
assert file.getRoot() == null;

Path parent = file.getParent();
if (parent == null) {
String name = file.toString();
if (name.endsWith(".class") && !name.equals("module-info.class")) {
String msg = name + " found in top-level directory"
+ " (unnamed package not allowed in module)";
throw new InvalidModuleDescriptorException(msg);
}
return Optional.empty();
}

String pn = parent.toString().replace(separator, ".");
if (SourceVersion.isName(pn)) {
return Optional.of(pn);
} else {
// not a valid package name
return Optional.empty();
}
return true;
}

private static boolean containsAtLeastOneRegularFile(Path directory) {
try (var stream = Files.newDirectoryStream(directory, Files::isRegularFile)) {
return stream.iterator().hasNext();
} catch (IOException exception) {
throw new UncheckedIOException(exception);
/**
* Returns true if the given file exists and is a hidden file
*/
private static boolean isHidden(Path file) {
try {
return Files.isHidden(file);
} catch (IOException ioe) {
return false;
}
}
}