1 /*
   2  * Copyright (c) 2016, 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 p1;
  25 
  26 import java.io.InputStream;
  27 import java.io.IOException;
  28 import java.lang.module.ModuleDescriptor;
  29 import java.net.URI;
  30 import java.nio.file.FileSystem;
  31 import java.nio.file.FileSystems;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.util.Collections;
  35 import java.util.Set;
  36 
  37 import jdk.internal.module.ModuleInfo;
  38 import jdk.internal.module.ModuleInfo.Attributes;
  39 
  40 public class Main {
  41     private static boolean hasModuleTarget(InputStream in) throws IOException {
  42         ModuleInfo.Attributes attrs = ModuleInfo.read(in, null);
  43         return attrs.target() != null;
  44     }
  45 
  46     public static void main(String... args) throws Exception {
  47         // load another package
  48         p2.T.test();
  49 
  50         // validate the module descriptor
  51         validate(Main.class.getModule());
  52 
  53         // validate the Moduletarget attribute for java.base
  54         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
  55                                                   Collections.emptyMap());
  56         Path path = fs.getPath("/", "modules", "java.base", "module-info.class");
  57         try (InputStream in = Files.newInputStream(path)) {
  58             if (! hasModuleTarget(in)) {
  59                 throw new RuntimeException("Missing ModuleTarget for java.base");
  60             }
  61         }
  62     }
  63 
  64     static void validate(Module module) throws IOException {
  65         ModuleDescriptor md = module.getDescriptor();
  66 
  67         // read m1/module-info.class
  68         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
  69                                                   Collections.emptyMap());
  70         Path path = fs.getPath("/", "modules", module.getName(), "module-info.class");
  71         ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path));
  72 
  73 
  74         // check the module descriptor of a system module and read from jimage
  75         checkPackages(md.packages(), "p1", "p2");
  76         checkPackages(md1.packages(), "p1", "p2");
  77 
  78         try (InputStream in = Files.newInputStream(path)) {
  79             checkModuleTargetAttribute(in, "p1");
  80         }
  81     }
  82 
  83     static void checkPackages(Set<String> pkgs, String... expected) {
  84         if (!pkgs.equals(Set.of(expected))) {
  85             throw new RuntimeException(pkgs + " expected: " + Set.of(expected));
  86         }
  87     }
  88 
  89     static void checkModuleTargetAttribute(InputStream in, String modName) throws IOException {
  90         if (hasModuleTarget(in)) {
  91             throw new RuntimeException("ModuleTarget present for " + modName);
  92         }
  93     }
  94 }