--- old/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java 2017-02-07 13:13:46.264125853 +0000 +++ new/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java 2017-02-07 13:13:46.095114247 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,6 +31,7 @@ import java.io.InputStream; import java.io.UncheckedIOException; import java.lang.module.ModuleDescriptor; +import java.lang.module.ModuleDescriptor.Builder; import java.lang.module.ModuleReader; import java.lang.module.ModuleReference; import java.net.MalformedURLException; @@ -54,6 +55,7 @@ import java.util.stream.Stream; import jdk.internal.loader.Resource; +import jdk.internal.loader.ResourceHelper; import jdk.internal.misc.JavaLangModuleAccess; import jdk.internal.misc.SharedSecrets; import sun.net.www.ParseUtil; @@ -108,8 +110,11 @@ if (paths == null) return mref; - // scan the JAR file or directory tree to get the set of packages + // Scan the JAR file or directory tree to get the set of packages. + // For automatic modules then packages that do not contain class files + // must be ignored. Set packages = new HashSet<>(); + boolean isAutomatic = descriptor.isAutomatic(); try { for (Path file : paths) { if (Files.isRegularFile(file)) { @@ -118,8 +123,10 @@ // is not supported by the boot class loader try (JarFile jf = new JarFile(file.toFile())) { jf.stream() + .filter(e -> !e.isDirectory() + && (!isAutomatic || e.getName().endsWith(".class"))) .map(e -> toPackageName(file, e)) - .filter(Checks::isJavaIdentifier) + .filter(Checks::isPackageName) .forEach(packages::add); } @@ -129,8 +136,10 @@ Path top = file; Files.find(top, Integer.MAX_VALUE, ((path, attrs) -> attrs.isRegularFile())) + .filter(path -> !isAutomatic + || path.toString().endsWith(".class")) .map(path -> toPackageName(top, path)) - .filter(Checks::isJavaIdentifier) + .filter(Checks::isPackageName) .forEach(packages::add); } @@ -141,10 +150,30 @@ } // if there are new packages then we need a new ModuleDescriptor - Set original = descriptor.packages(); - packages.addAll(original); - if (packages.size() > original.size()) { - descriptor = JLMA.newModuleDescriptor(descriptor, packages); + packages.removeAll(descriptor.packages()); + if (!packages.isEmpty()) { + Builder builder = JLMA.newModuleBuilder(descriptor.name(), + /*strict*/ false, + descriptor.modifiers()); + if (!descriptor.isAutomatic()) { + descriptor.requires().forEach(builder::requires); + descriptor.exports().forEach(builder::exports); + descriptor.opens().forEach(builder::opens); + descriptor.uses().forEach(builder::uses); + } + descriptor.provides().forEach(builder::provides); + + descriptor.version().ifPresent(builder::version); + descriptor.mainClass().ifPresent(builder::mainClass); + descriptor.osName().ifPresent(builder::osName); + descriptor.osArch().ifPresent(builder::osArch); + descriptor.osVersion().ifPresent(builder::osVersion); + + // original + new packages + builder.packages(descriptor.packages()); + builder.packages(packages); + + descriptor = builder.build(); } // return a module reference to the patched module @@ -471,23 +500,14 @@ @Override public Resource find(String name) throws IOException { - Path file = Paths.get(name.replace('/', File.separatorChar)); - if (file.getRoot() == null) { - file = dir.resolve(file); - } else { - // drop the root component so that the resource is - // located relative to the module directory - int n = file.getNameCount(); - if (n == 0) - return null; - file = dir.resolve(file.subpath(0, n)); - } - - if (Files.isRegularFile(file)) { - return newResource(name, dir, file); - } else { - return null; + Path path = ResourceHelper.toFilePath(name); + if (path != null) { + Path file = dir.resolve(path); + if (Files.isRegularFile(file)) { + return newResource(name, dir, file); + } } + return null; } private Resource newResource(String name, Path top, Path file) {