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