< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java

Print this page

        

@@ -72,10 +72,11 @@
 import java.util.Map;
 import java.util.MissingResourceException;
 import java.util.Optional;
 import java.util.ResourceBundle;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.function.Supplier;
 import java.util.jar.JarEntry;

@@ -495,10 +496,11 @@
             try (InputStream in = miSupplier.get()) {
                 ModuleInfoExtender extender = ModuleInfoExtender.newExtender(in);
 
                 // Add (or replace) the Packages attribute
                 if (packages != null) {
+                    validatePackages(descriptor, packages);
                     extender.packages(packages);
                 }
 
                 // --main-class
                 if (mainClass != null)

@@ -528,10 +530,28 @@
                 // write the (possibly extended or modified) module-info.class
                 out.writeEntry(extender.toByteArray(), Section.CLASSES, MODULE_INFO);
             }
         }
 
+        private void validatePackages(ModuleDescriptor descriptor, Set<String> packages) {
+            Set<String> nonExistPackages = new TreeSet<>();
+            descriptor.exports().stream()
+                .map(Exports::source)
+                .filter(pn -> !packages.contains(pn))
+                .forEach(nonExistPackages::add);
+
+            descriptor.opens().stream()
+                .map(Opens::source)
+                .filter(pn -> !packages.contains(pn))
+                .forEach(nonExistPackages::add);
+
+            if (!nonExistPackages.isEmpty()) {
+                throw new CommandException("err.missing.export.or.open.packages",
+                    descriptor.name(), nonExistPackages);
+            }
+        }
+
         /*
          * Hasher resolves a module graph using the --hash-modules PATTERN
          * as the roots.
          *
          * The jmod file is being created and does not exist in the
< prev index next >