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.InputStream; 27 import java.io.IOException; 28 import java.lang.module.ModuleDescriptor; 29 import java.lang.reflect.Layer; 30 import java.lang.reflect.Module; 31 import java.net.URI; 32 import java.nio.file.FileSystem; 33 import java.nio.file.FileSystems; 34 import java.nio.file.Files; 35 import java.nio.file.Path; 36 import java.util.Collections; 37 import java.util.Set; 38 39 import jdk.internal.module.ClassFileAttributes; 40 import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute; 41 import jdk.internal.module.ClassFileConstants; 42 import jdk.internal.org.objectweb.asm.Attribute; 43 import jdk.internal.org.objectweb.asm.ClassReader; 44 import jdk.internal.org.objectweb.asm.ClassVisitor; 45 import jdk.internal.org.objectweb.asm.Opcodes; 46 47 public class Main { 48 private static boolean hasModuleTarget(InputStream in) throws IOException { 49 ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1]; 50 ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) { 51 @Override 52 public void visitAttribute(Attribute attr) { 53 if (attr instanceof ModuleTargetAttribute) { 54 modTargets[0] = (ModuleTargetAttribute)attr; 55 } 56 } 57 }; 58 59 // prototype of attributes that should be parsed 60 Attribute[] attrs = new Attribute[] { 61 new ModuleTargetAttribute() 62 }; 63 64 // parse module-info.class 65 ClassReader cr = new ClassReader(in); 66 cr.accept(cv, attrs, 0); 67 return modTargets[0] != null && 68 (modTargets[0].osName() != null || modTargets[0].osArch() != null); 69 } 70 71 public static void main(String... args) throws Exception { 72 // load another package 73 p2.T.test(); 74 75 // validate the module descriptor 76 validate(Main.class.getModule()); 77 78 // validate the Moduletarget attribute for java.base 79 FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), 80 Collections.emptyMap()); 81 Path path = fs.getPath("/", "modules", "java.base", "module-info.class"); 82 try (InputStream in = Files.newInputStream(path)) { 83 if (! hasModuleTarget(in)) { 84 throw new RuntimeException("Missing ModuleTarget for java.base"); 85 } 86 } 87 } 88 89 static void validate(Module module) throws IOException { 90 ModuleDescriptor md = module.getDescriptor(); 91 92 // read m1/module-info.class 93 FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), 94 Collections.emptyMap()); 95 Path path = fs.getPath("/", "modules", module.getName(), "module-info.class"); 96 ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path)); 97 98 99 // check the module descriptor of a system module and read from jimage 100 checkPackages(md.packages(), "p1", "p2"); 101 checkPackages(md1.packages(), "p1", "p2"); 102 103 try (InputStream in = Files.newInputStream(path)) { 104 checkModuleTargetAttribute(in, "p1"); 105 } 106 } 107 108 static void checkPackages(Set<String> pkgs, String... expected) { 109 if (!pkgs.equals(Set.of(expected))) { 110 throw new RuntimeException(pkgs + " expected: " + Set.of(expected)); 111 } 112 } 113 114 static void checkModuleTargetAttribute(InputStream in, String modName) throws IOException { 115 if (hasModuleTarget(in)) { 116 throw new RuntimeException("ModuleTarget present for " + modName); 117 } 118 } 119 }