jdk/test/tools/jlink/plugins/SystemModuleDescriptors/src/m1/p1/Main.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 44359 c6761862ca0b
child 45004 ea3137042a61
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
package p1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    26
import java.io.InputStream;
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    27
import java.io.IOException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.FileSystem;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.FileSystems;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.Path;
37536
dcd80bbbb686 8154498: fix to 8154403 results in failure of UserModuleTest.java on all platforms
sherman
parents: 36511
diff changeset
    34
import java.util.Collections;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    37
import jdk.internal.module.ClassFileAttributes;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    38
import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    39
import jdk.internal.module.ClassFileConstants;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    40
import jdk.internal.org.objectweb.asm.Attribute;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    41
import jdk.internal.org.objectweb.asm.ClassReader;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassVisitor;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    43
import jdk.internal.org.objectweb.asm.Opcodes;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    44
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public class Main {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    46
    private static boolean hasModuleTarget(InputStream in) throws IOException {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    47
        ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1];
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    48
        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    49
            @Override
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    50
            public void visitAttribute(Attribute attr) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    51
                if (attr instanceof ModuleTargetAttribute) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    52
                    modTargets[0] = (ModuleTargetAttribute)attr;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    53
                }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    54
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    55
        };
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    56
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    57
        // prototype of attributes that should be parsed
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    58
        Attribute[] attrs = new Attribute[] {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    59
            new ModuleTargetAttribute()
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    60
        };
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    61
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    62
        // parse module-info.class
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    63
        ClassReader cr = new ClassReader(in);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    64
        cr.accept(cv, attrs, 0);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    65
        return modTargets[0] != null &&
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    66
            (modTargets[0].osName() != null || modTargets[0].osArch() != null);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    67
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    68
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    public static void main(String... args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        // load another package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        p2.T.test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    73
        // validate the module descriptor
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    74
        validate(Main.class.getModule());
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    75
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    76
        // validate the Moduletarget attribute for java.base
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    77
        FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    78
                                                  Collections.emptyMap());
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    79
        Path path = fs.getPath("/", "modules", "java.base", "module-info.class");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    80
        try (InputStream in = Files.newInputStream(path)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    81
            if (! hasModuleTarget(in)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    82
                throw new RuntimeException("Missing ModuleTarget for java.base");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
    83
            }
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    84
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    85
    }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    86
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    87
    static void validate(Module module) throws IOException {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    88
        ModuleDescriptor md = module.getDescriptor();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        // read m1/module-info.class
37536
dcd80bbbb686 8154498: fix to 8154403 results in failure of UserModuleTest.java on all platforms
sherman
parents: 36511
diff changeset
    91
        FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
dcd80bbbb686 8154498: fix to 8154403 results in failure of UserModuleTest.java on all platforms
sherman
parents: 36511
diff changeset
    92
                                                  Collections.emptyMap());
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    93
        Path path = fs.getPath("/", "modules", module.getName(), "module-info.class");
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    94
        ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path));
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    95
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    97
        // check the module descriptor of a system module and read from jimage
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        checkPackages(md.packages(), "p1", "p2");
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
    99
        checkPackages(md1.packages(), "p1", "p2");
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   100
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   101
        try (InputStream in = Files.newInputStream(path)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   102
            checkModuleTargetAttribute(in, "p1");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   103
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    static void checkPackages(Set<String> pkgs, String... expected) {
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   107
        if (!pkgs.equals(Set.of(expected))) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   108
            throw new RuntimeException(pkgs + " expected: " + Set.of(expected));
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   109
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   110
    }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42338
diff changeset
   111
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   112
    static void checkModuleTargetAttribute(InputStream in, String modName) throws IOException {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   113
        if (hasModuleTarget(in)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43336
diff changeset
   114
            throw new RuntimeException("ModuleTarget present for " + modName);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
}