1 /*
   2  * Copyright (c) 2016, 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.IOException;
  27 import java.lang.module.ModuleDescriptor;
  28 import java.lang.reflect.Layer;
  29 import java.lang.reflect.Module;
  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.Collections;
  36 import java.util.Set;
  37 
  38 public class Main {
  39     public static void main(String... args) throws Exception {
  40         // load another package
  41         p2.T.test();
  42 
  43         // validate the module descriptor
  44         validate(Main.class.getModule());
  45 
  46         // validate the Moduletarget attribute for java.base
  47         ModuleDescriptor md = Layer.boot().findModule("java.base").get()
  48                                    .getDescriptor();
  49         if (!md.osName().isPresent() || !md.osArch().isPresent() ||
  50                 !md.osVersion().isPresent()) {
  51             throw new RuntimeException("java.base: " + md.osName() + " " +
  52                     md.osArch() + " " + md.osVersion());
  53         }
  54     }
  55 
  56     static void validate(Module module) throws IOException {
  57         ModuleDescriptor md = module.getDescriptor();
  58 
  59         // read m1/module-info.class
  60         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
  61                                                   Collections.emptyMap());
  62         Path path = fs.getPath("/", "modules", module.getName(), "module-info.class");
  63         ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path));
  64 
  65 
  66         // check the module descriptor of a system module and read from jimage
  67         checkPackages(md.packages(), "p1", "p2");
  68         checkPackages(md1.packages(), "p1", "p2");
  69 
  70         // check ModuleTarget attribute
  71         checkModuleTargetAttribute(md);
  72         checkModuleTargetAttribute(md1);
  73     }
  74 
  75     static void checkPackages(Set<String> pkgs, String... expected) {
  76         if (!pkgs.equals(Set.of(expected))) {
  77             throw new RuntimeException(pkgs + " expected: " + Set.of(expected));
  78         }
  79     }
  80 
  81     static void checkModuleTargetAttribute(ModuleDescriptor md) {
  82         if (md.osName().isPresent() || md.osArch().isPresent() ||
  83                 md.osVersion().isPresent()) {
  84             throw new RuntimeException(md.osName() + " " + md.osArch() + " " + md.osVersion());
  85         }
  86     }
  87 }