1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package p4;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.module.ModuleFinder;
  30 import java.net.URI;
  31 import java.nio.file.FileSystem;
  32 import java.nio.file.FileSystems;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.util.Map;
  36 import java.util.Set;
  37 
  38 import jdk.internal.module.ModuleInfo;
  39 import jdk.internal.module.ModuleInfo.Attributes;
  40 
  41 public class Main {
  42     private static boolean hasModuleTarget(InputStream in) throws IOException {
  43         ModuleInfo.Attributes attrs = ModuleInfo.read(in, null);
  44         return attrs.target() != null;
  45     }
  46 
  47     private static boolean hasModuleTarget(String modName) throws IOException {
  48         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of());
  49         Path path = fs.getPath("/", "modules", modName, "module-info.class");
  50         try (InputStream in = Files.newInputStream(path)) {
  51             return hasModuleTarget(in);
  52         }
  53     }
  54 
  55     // the system module plugin by default drops ModuleTarget attribute
  56     private static boolean expectModuleTarget = false;
  57     public static void main(String... args) throws IOException {
  58         if (args.length > 0) {
  59             if (!args[0].equals("retainModuleTarget")) {
  60                 throw new IllegalArgumentException(args[0]);
  61             }
  62 
  63             expectModuleTarget = true;
  64         }
  65 
  66         // java.base is packaged with ModuleTarget
  67         if (!hasModuleTarget("java.base")) {
  68             throw new RuntimeException("ModuleTarget absent for java.base");
  69         }
  70 
  71         // verify module-info.class for m1 and m4
  72         checkModule("m1", "p1", "p2");
  73         checkModule("m4", "p4");
  74     }
  75 
  76     private static void checkModule(String mn, String... packages) throws IOException {
  77         // verify ModuleDescriptor from the runtime module
  78         ModuleDescriptor md = ModuleLayer.boot().findModule(mn).get()
  79                                    .getDescriptor();
  80         checkModuleDescriptor(md, packages);
  81 
  82         // verify ModuleDescriptor from module-info.class read from ModuleReader
  83         try (InputStream in = ModuleFinder.ofSystem().find(mn).get()
  84             .open().open("module-info.class").get()) {
  85             checkModuleDescriptor(ModuleDescriptor.read(in), packages);
  86         }
  87 
  88         // verify ModuleDescriptor from module-info.class read from jimage
  89         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of());
  90         Path path = fs.getPath("/", "modules", mn, "module-info.class");
  91         checkModuleDescriptor(ModuleDescriptor.read(Files.newInputStream(path)), packages);
  92     }
  93 
  94     static void checkModuleDescriptor(ModuleDescriptor md, String... packages) throws IOException {
  95         String mainClass = md.name().replace('m', 'p') + ".Main";
  96         if (!md.mainClass().get().equals(mainClass)) {
  97             throw new RuntimeException(md.mainClass().toString());
  98         }
  99 
 100         // ModuleTarget attribute should be present
 101         if (!hasModuleTarget(md.name())) {
 102             throw new RuntimeException("ModuleTarget missing for " + md.name());
 103         }
 104 
 105         Set<String> pkgs = md.packages();
 106         if (!pkgs.equals(Set.of(packages))) {
 107             throw new RuntimeException(pkgs + " expected: " + Set.of(packages));
 108         }
 109     }
 110 }