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();
|
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();
|