src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java
author alanb
Tue, 19 Nov 2019 11:35:29 +0000
changeset 59133 580fb715b29d
parent 52814 abccada595dd
permissions -rw-r--r--
8233922: Service binding augments module graph with observable incubator modules Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     1
/*
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52814
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     4
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    10
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    15
 * accompanied this code).
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    16
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    20
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    23
 * questions.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    24
 */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal.plugins;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    26
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    27
import java.io.ByteArrayInputStream;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    29
import java.io.IOException;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    30
import java.io.InputStream;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    31
import java.lang.module.Configuration;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor;
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    33
import java.lang.module.ModuleDescriptor.Exports;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    34
import java.lang.module.ModuleDescriptor.Opens;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    35
import java.lang.module.ModuleDescriptor.Provides;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    36
import java.lang.module.ModuleDescriptor.Requires;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    37
import java.lang.module.ModuleDescriptor.Version;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    38
import java.lang.module.ModuleFinder;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    39
import java.lang.module.ModuleReader;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    40
import java.lang.module.ModuleReference;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    41
import java.lang.module.ResolvedModule;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    42
import java.net.URI;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    43
import java.util.ArrayList;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    44
import java.util.Collection;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    45
import java.util.Collections;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    46
import java.util.EnumSet;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    47
import java.util.HashMap;
42770
d363e7c3584a 8171400: Move checking of duplicate packages in the boot layer to link time
redestad
parents: 42752
diff changeset
    48
import java.util.HashSet;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    49
import java.util.LinkedHashMap;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    50
import java.util.List;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    51
import java.util.Map;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    52
import java.util.Objects;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    53
import java.util.Optional;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    54
import java.util.Set;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
    55
import java.util.TreeMap;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    56
import java.util.TreeSet;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    57
import java.util.function.IntSupplier;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    58
import java.util.function.Supplier;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    59
import java.util.stream.Collectors;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    60
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    61
import jdk.internal.module.Checks;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    62
import jdk.internal.module.DefaultRoots;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
    63
import jdk.internal.module.IllegalAccessMaps;
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52814
diff changeset
    64
import jdk.internal.module.Modules;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    65
import jdk.internal.module.ModuleHashes;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    66
import jdk.internal.module.ModuleInfo.Attributes;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    67
import jdk.internal.module.ModuleInfoExtender;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    68
import jdk.internal.module.ModuleReferenceImpl;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    69
import jdk.internal.module.ModuleResolution;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
    70
import jdk.internal.module.ModuleTarget;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    71
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
    72
import jdk.internal.org.objectweb.asm.ClassReader;
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
    73
import jdk.internal.org.objectweb.asm.ClassVisitor;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    74
import jdk.internal.org.objectweb.asm.ClassWriter;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    75
import jdk.internal.org.objectweb.asm.MethodVisitor;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    76
import jdk.internal.org.objectweb.asm.ModuleVisitor;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    77
import jdk.internal.org.objectweb.asm.Opcodes;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    78
import static jdk.internal.org.objectweb.asm.Opcodes.*;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    79
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
    80
import jdk.tools.jlink.internal.ModuleSorter;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    81
import jdk.tools.jlink.plugin.Plugin;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    82
import jdk.tools.jlink.plugin.PluginException;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    83
import jdk.tools.jlink.plugin.ResourcePool;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    84
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    85
import jdk.tools.jlink.plugin.ResourcePoolEntry;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    86
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    87
/**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    88
 * Jlink plugin to reconstitute module descriptors and other attributes for system
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    89
 * modules. The plugin generates implementations of SystemModules to avoid parsing
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    90
 * module-info.class files at startup. It also generates SystemModulesMap to return
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    91
 * the SystemModules implementation for a specific initial module.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    92
 *
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    93
 * As a side effect, the plugin adds the ModulePackages class file attribute to the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    94
 * module-info.class files that don't have the attribute.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    95
 *
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    96
 * @see jdk.internal.module.SystemModuleFinders
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    97
 * @see jdk.internal.module.SystemModules
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    98
 */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
    99
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   100
public final class SystemModulesPlugin implements Plugin {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   101
    private static final String NAME = "system-modules";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   102
    private static final String DESCRIPTION =
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   103
            PluginsResourceBundle.getDescription(NAME);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   104
    private static final String SYSTEM_MODULES_MAP_CLASS =
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   105
            "jdk/internal/module/SystemModulesMap";
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   106
    private static final String SYSTEM_MODULES_CLASS_PREFIX =
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   107
            "jdk/internal/module/SystemModules$";
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   108
    private static final String ALL_SYSTEM_MODULES_CLASS =
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   109
            SYSTEM_MODULES_CLASS_PREFIX + "all";
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   110
    private static final String DEFAULT_SYSTEM_MODULES_CLASS =
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   111
            SYSTEM_MODULES_CLASS_PREFIX + "default";
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   112
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   113
    private boolean enabled;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   114
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   115
    public SystemModulesPlugin() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   116
        this.enabled = true;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   117
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   118
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   119
    @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   120
    public String getName() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   121
        return NAME;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   122
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   123
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   124
    @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   125
    public String getDescription() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   126
        return DESCRIPTION;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   127
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   128
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   129
    @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   130
    public Set<State> getState() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   131
        return enabled ? EnumSet.of(State.AUTO_ENABLED, State.FUNCTIONAL)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   132
                       : EnumSet.of(State.DISABLED);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   133
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   134
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   135
    @Override
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   136
    public boolean hasArguments() {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   137
        return true;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   138
    }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   139
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   140
    @Override
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   141
    public String getArgumentsDescription() {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   142
        return PluginsResourceBundle.getArgument(NAME);
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   143
    }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   144
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   145
    @Override
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   146
    public void configure(Map<String, String> config) {
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   147
        String arg = config.get(NAME);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   148
        if (arg != null) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   149
            throw new IllegalArgumentException(NAME + ": " + arg);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   150
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   151
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   152
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   153
    @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   154
    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   155
        if (!enabled) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   156
            throw new PluginException(NAME + " was set");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   157
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   158
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   159
        // validate, transform (if needed), and add the module-info.class files
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   160
        List<ModuleInfo> moduleInfos = transformModuleInfos(in, out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   161
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   162
        // generate and add the SystemModuleMap and SystemModules classes
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   163
        Set<String> generated = genSystemModulesClasses(moduleInfos, out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   164
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   165
        // pass through all other resources
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   166
        in.entries()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   167
            .filter(data -> !data.path().endsWith("/module-info.class")
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   168
                    && !generated.contains(data.path()))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   169
            .forEach(data -> out.add(data));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   170
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   171
        return out.build();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   172
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   173
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   174
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   175
     * Validates and transforms the module-info.class files in the modules, adding
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   176
     * the ModulePackages class file attribute if needed.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   177
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   178
     * @return the list of ModuleInfo objects, the first element is java.base
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   179
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   180
    List<ModuleInfo> transformModuleInfos(ResourcePool in, ResourcePoolBuilder out) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   181
        List<ModuleInfo> moduleInfos = new ArrayList<>();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   182
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   183
        // Sort modules in the topological order so that java.base is always first.
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   184
        new ModuleSorter(in.moduleView()).sorted().forEach(module -> {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   185
            ResourcePoolEntry data = module.findEntry("module-info.class").orElseThrow(
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   186
                // automatic modules not supported
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   187
                () ->  new PluginException("module-info.class not found for " +
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   188
                        module.name() + " module")
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   189
            );
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   190
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   191
            assert module.name().equals(data.moduleName());
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   192
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   193
            try {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   194
                byte[] content = data.contentBytes();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   195
                Set<String> packages = module.packages();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   196
                ModuleInfo moduleInfo = new ModuleInfo(content, packages);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   197
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   198
                // link-time validation
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   199
                moduleInfo.validateNames();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   200
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   201
                // check if any exported or open package is not present
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   202
                moduleInfo.validatePackages();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   203
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   204
                // module-info.class may be overridden to add ModulePackages
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   205
                if (moduleInfo.shouldRewrite()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   206
                    data = data.copyWithContent(moduleInfo.getBytes());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   207
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   208
                moduleInfos.add(moduleInfo);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   209
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   210
                // add resource pool entry
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   211
                out.add(data);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   212
            } catch (IOException e) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   213
                throw new PluginException(e);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   214
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   215
        });
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   216
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   217
        return moduleInfos;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   218
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   219
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   220
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   221
     * Generates the SystemModules classes (at least one) and the SystemModulesMap
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   222
     * class to map initial modules to a SystemModules class.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   223
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   224
     * @return the resource names of the resources added to the pool
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   225
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   226
    private Set<String> genSystemModulesClasses(List<ModuleInfo> moduleInfos,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   227
                                                ResourcePoolBuilder out) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   228
        int moduleCount = moduleInfos.size();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   229
        ModuleFinder finder = finderOf(moduleInfos);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   230
        assert finder.findAll().size() == moduleCount;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   231
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   232
        // map of initial module name to SystemModules class name
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   233
        Map<String, String> map = new LinkedHashMap<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   234
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   235
        // the names of resources written to the pool
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   236
        Set<String> generated = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   237
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   238
        // generate the SystemModules implementation to reconstitute all modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   239
        Set<String> allModuleNames = moduleInfos.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   240
                .map(ModuleInfo::moduleName)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   241
                .collect(Collectors.toSet());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   242
        String rn = genSystemModulesClass(moduleInfos,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   243
                                          resolve(finder, allModuleNames),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   244
                                          ALL_SYSTEM_MODULES_CLASS,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   245
                                          out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   246
        generated.add(rn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   247
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   248
        // generate, if needed, a SystemModules class to reconstitute the modules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   249
        // needed for the case that the initial module is the unnamed module.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   250
        String defaultSystemModulesClassName;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   251
        Configuration cf = resolve(finder, DefaultRoots.compute(finder));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   252
        if (cf.modules().size() == moduleCount) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   253
            // all modules are resolved so no need to generate a class
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   254
            defaultSystemModulesClassName = ALL_SYSTEM_MODULES_CLASS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   255
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   256
            defaultSystemModulesClassName = DEFAULT_SYSTEM_MODULES_CLASS;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   257
            rn = genSystemModulesClass(sublist(moduleInfos, cf),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   258
                                       cf,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   259
                                       defaultSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   260
                                       out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   261
            generated.add(rn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   262
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   263
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   264
        // Generate a SystemModules class for each module with a main class
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   265
        int suffix = 0;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   266
        for (ModuleInfo mi : moduleInfos) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   267
            if (mi.descriptor().mainClass().isPresent()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   268
                String moduleName = mi.moduleName();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   269
                cf = resolve(finder, Set.of(moduleName));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   270
                if (cf.modules().size() == moduleCount) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   271
                    // resolves all modules so no need to generate a class
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   272
                    map.put(moduleName, ALL_SYSTEM_MODULES_CLASS);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   273
                } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   274
                    String cn = SYSTEM_MODULES_CLASS_PREFIX + (suffix++);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   275
                    rn = genSystemModulesClass(sublist(moduleInfos, cf), cf, cn, out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   276
                    map.put(moduleName, cn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   277
                    generated.add(rn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   278
                }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   279
            }
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   280
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   281
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   282
        // generate SystemModulesMap
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   283
        rn = genSystemModulesMapClass(ALL_SYSTEM_MODULES_CLASS,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   284
                                      defaultSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   285
                                      map,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   286
                                      out);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   287
        generated.add(rn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   288
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   289
        // return the resource names of the generated classes
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   290
        return generated;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   291
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   292
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   293
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   294
     * Resolves a collection of root modules, with service binding, to create
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52814
diff changeset
   295
     * a Configuration for the boot layer.
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   296
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   297
    private Configuration resolve(ModuleFinder finder, Set<String> roots) {
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 52814
diff changeset
   298
        return Modules.newBootLayerConfiguration(finder, roots, null);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   299
    }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   300
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   301
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   302
     * Returns the list of ModuleInfo objects that correspond to the modules in
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   303
     * the given configuration.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   304
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   305
    private List<ModuleInfo> sublist(List<ModuleInfo> moduleInfos, Configuration cf) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   306
        Set<String> names = cf.modules()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   307
                .stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   308
                .map(ResolvedModule::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   309
                .collect(Collectors.toSet());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   310
        return moduleInfos.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   311
                .filter(mi -> names.contains(mi.moduleName()))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   312
                .collect(Collectors.toList());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   313
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   314
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   315
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   316
     * Generate a SystemModules implementation class and add it as a resource.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   317
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   318
     * @return the name of the class resource added to the pool
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   319
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   320
    private String genSystemModulesClass(List<ModuleInfo> moduleInfos,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   321
                                         Configuration cf,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   322
                                         String className,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   323
                                         ResourcePoolBuilder out) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   324
        SystemModulesClassGenerator generator
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   325
            = new SystemModulesClassGenerator(className, moduleInfos);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   326
        byte[] bytes = generator.getClassWriter(cf).toByteArray();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   327
        String rn = "/java.base/" + className + ".class";
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   328
        ResourcePoolEntry e = ResourcePoolEntry.create(rn, bytes);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   329
        out.add(e);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   330
        return rn;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   331
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   332
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   333
    static class ModuleInfo {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   334
        private final ByteArrayInputStream bais;
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   335
        private final Attributes attrs;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   336
        private final Set<String> packages;
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   337
        private final boolean addModulePackages;
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   338
        private ModuleDescriptor descriptor;  // may be different that the original one
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   339
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   340
        ModuleInfo(byte[] bytes, Set<String> packages) throws IOException {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   341
            this.bais = new ByteArrayInputStream(bytes);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   342
            this.packages = packages;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   343
            this.attrs = jdk.internal.module.ModuleInfo.read(bais, null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   344
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   345
            // If ModulePackages attribute is present, the packages from this
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   346
            // module descriptor returns the packages in that attribute.
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   347
            // If it's not present, ModuleDescriptor::packages only contains
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   348
            // the exported and open packages from module-info.class
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   349
            this.descriptor = attrs.descriptor();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   350
            if (descriptor.isAutomatic()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   351
                throw new InternalError("linking automatic module is not supported");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   352
            }
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   353
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   354
            // add ModulePackages attribute if this module contains some packages
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   355
            // and ModulePackages is not present
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   356
            this.addModulePackages = packages.size() > 0 && !hasModulePackages();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   357
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   358
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   359
        String moduleName() {
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   360
            return attrs.descriptor().name();
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   361
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   362
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   363
        ModuleDescriptor descriptor() {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   364
            return descriptor;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   365
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   366
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   367
        Set<String> packages() {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   368
            return packages;
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   369
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   370
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   371
        ModuleTarget target() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   372
            return attrs.target();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   373
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   374
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   375
        ModuleHashes recordedHashes() {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   376
            return attrs.recordedHashes();
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   377
        }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   378
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   379
        ModuleResolution moduleResolution() {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   380
            return attrs.moduleResolution();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   381
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   382
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   383
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   384
         * Validates names in ModuleDescriptor
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   385
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   386
        void validateNames() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   387
            Checks.requireModuleName(descriptor.name());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   388
            for (Requires req : descriptor.requires()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   389
                Checks.requireModuleName(req.name());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   390
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   391
            for (Exports e : descriptor.exports()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   392
                Checks.requirePackageName(e.source());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   393
                if (e.isQualified())
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   394
                    e.targets().forEach(Checks::requireModuleName);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   395
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   396
            for (Opens opens : descriptor.opens()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   397
                Checks.requirePackageName(opens.source());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   398
                if (opens.isQualified())
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   399
                    opens.targets().forEach(Checks::requireModuleName);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   400
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   401
            for (Provides provides : descriptor.provides()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   402
                Checks.requireServiceTypeName(provides.service());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   403
                provides.providers().forEach(Checks::requireServiceProviderName);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   404
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   405
            for (String service : descriptor.uses()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   406
                Checks.requireServiceTypeName(service);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   407
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   408
            for (String pn : descriptor.packages()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   409
                Checks.requirePackageName(pn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   410
            }
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   411
            for (String pn : packages) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   412
                Checks.requirePackageName(pn);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   413
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   414
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   415
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   416
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   417
         * Validates if exported and open packages are present
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   418
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   419
        void validatePackages() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   420
            Set<String> nonExistPackages = new TreeSet<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   421
            descriptor.exports().stream()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   422
                .map(Exports::source)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   423
                .filter(pn -> !packages.contains(pn))
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   424
                .forEach(nonExistPackages::add);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   425
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   426
            descriptor.opens().stream()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   427
                .map(Opens::source)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   428
                .filter(pn -> !packages.contains(pn))
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   429
                .forEach(nonExistPackages::add);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   430
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   431
            if (!nonExistPackages.isEmpty()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   432
                throw new PluginException("Packages that are exported or open in "
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   433
                    + descriptor.name() + " are not present: " + nonExistPackages);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   434
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   435
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   436
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   437
        boolean hasModulePackages() throws IOException {
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   438
            Set<String> packages = new HashSet<>();
52515
746df0ae4fe1 8213480: update internal ASM version to 7.0
vromero
parents: 47489
diff changeset
   439
            ClassVisitor cv = new ClassVisitor(Opcodes.ASM7) {
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   440
                @Override
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   441
                public ModuleVisitor visitModule(String name,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   442
                                                 int flags,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   443
                                                 String version) {
52515
746df0ae4fe1 8213480: update internal ASM version to 7.0
vromero
parents: 47489
diff changeset
   444
                    return new ModuleVisitor(Opcodes.ASM7) {
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   445
                        public void visitPackage(String pn) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   446
                            packages.add(pn);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   447
                        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   448
                    };
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   449
                }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   450
            };
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   451
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   452
            try (InputStream in = getInputStream()) {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   453
                // parse module-info.class
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   454
                ClassReader cr = new ClassReader(in);
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   455
                cr.accept(cv, 0);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   456
                return packages.size() > 0;
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   457
            }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   458
        }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   459
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   460
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   461
         * Returns true if module-info.class should be rewritten to add the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   462
         * ModulePackages attribute.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   463
         */
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   464
        boolean shouldRewrite() {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   465
            return addModulePackages;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   466
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   467
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   468
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   469
         * Returns the bytes for the (possibly updated) module-info.class.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   470
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   471
        byte[] getBytes() throws IOException {
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   472
            try (InputStream in = getInputStream()) {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   473
                if (shouldRewrite()) {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   474
                    ModuleInfoRewriter rewriter = new ModuleInfoRewriter(in);
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   475
                    if (addModulePackages) {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   476
                        rewriter.addModulePackages(packages);
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   477
                    }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   478
                    // rewritten module descriptor
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   479
                    byte[] bytes = rewriter.getBytes();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   480
                    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   481
                        this.descriptor = ModuleDescriptor.read(bais);
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   482
                    }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   483
                    return bytes;
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   484
                } else {
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   485
                    return in.readAllBytes();
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   486
                }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   487
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   488
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   489
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   490
        /*
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   491
         * Returns the input stream of the module-info.class
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   492
         */
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   493
        InputStream getInputStream() {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   494
            bais.reset();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   495
            return bais;
43732
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   496
        }
358d327a8220 8174740: RuntimeException: Module m's descriptor returns inconsistent package set
mchung
parents: 43712
diff changeset
   497
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   498
        class ModuleInfoRewriter extends ByteArrayOutputStream {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   499
            final ModuleInfoExtender extender;
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   500
            ModuleInfoRewriter(InputStream in) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   501
                this.extender = ModuleInfoExtender.newExtender(in);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   502
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   503
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   504
            void addModulePackages(Set<String> packages) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   505
                // Add ModulePackages attribute
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   506
                if (packages.size() > 0) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   507
                    extender.packages(packages);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   508
                }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   509
            }
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   510
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   511
            byte[] getBytes() throws IOException {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   512
                extender.write(this);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   513
                return buf;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   514
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   515
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   516
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   517
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   518
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   519
     * Generates a SystemModules class to reconstitute the ModuleDescriptor
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   520
     * and other attributes of system modules.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   521
     */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   522
    static class SystemModulesClassGenerator {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   523
        private static final String MODULE_DESCRIPTOR_BUILDER =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   524
            "jdk/internal/module/Builder";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   525
        private static final String MODULE_DESCRIPTOR_ARRAY_SIGNATURE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   526
            "[Ljava/lang/module/ModuleDescriptor;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   527
        private static final String REQUIRES_MODIFIER_CLASSNAME =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   528
            "java/lang/module/ModuleDescriptor$Requires$Modifier";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   529
        private static final String EXPORTS_MODIFIER_CLASSNAME =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   530
            "java/lang/module/ModuleDescriptor$Exports$Modifier";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   531
        private static final String OPENS_MODIFIER_CLASSNAME =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   532
            "java/lang/module/ModuleDescriptor$Opens$Modifier";
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   533
        private static final String MODULE_TARGET_CLASSNAME  =
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   534
            "jdk/internal/module/ModuleTarget";
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   535
        private static final String MODULE_TARGET_ARRAY_SIGNATURE  =
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   536
            "[Ljdk/internal/module/ModuleTarget;";
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   537
        private static final String MODULE_HASHES_ARRAY_SIGNATURE  =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   538
            "[Ljdk/internal/module/ModuleHashes;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   539
        private static final String MODULE_RESOLUTION_CLASSNAME  =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   540
            "jdk/internal/module/ModuleResolution";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   541
        private static final String MODULE_RESOLUTIONS_ARRAY_SIGNATURE  =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   542
            "[Ljdk/internal/module/ModuleResolution;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   543
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   544
        private static final int MAX_LOCAL_VARS = 256;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   545
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   546
        private final int BUILDER_VAR    = 0;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   547
        private final int MD_VAR         = 1;  // variable for ModuleDescriptor
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   548
        private final int MT_VAR         = 1;  // variable for ModuleTarget
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   549
        private final int MH_VAR         = 1;  // variable for ModuleHashes
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   550
        private int nextLocalVar         = 2;  // index to next local variable
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   551
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   552
        // Method visitor for generating the SystemModules::modules() method
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   553
        private MethodVisitor mv;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   554
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   555
        // name of class to generate
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   556
        private final String className;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   557
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   558
        // list of all ModuleDescriptorBuilders, invoked in turn when building.
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   559
        private final List<ModuleInfo> moduleInfos;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   560
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   561
        // A builder to create one single Set instance for a given set of
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   562
        // names or modifiers to reduce the footprint
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   563
        // e.g. target modules of qualified exports
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   564
        private final DedupSetBuilder dedupSetBuilder
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   565
            = new DedupSetBuilder(this::getNextLocalVar);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   566
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   567
        public SystemModulesClassGenerator(String className,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   568
                                           List<ModuleInfo> moduleInfos) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   569
            this.className = className;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   570
            this.moduleInfos = moduleInfos;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   571
            moduleInfos.forEach(mi -> dedups(mi.descriptor()));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   572
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   573
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   574
        private int getNextLocalVar() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   575
            return nextLocalVar++;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   576
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   577
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   578
        /*
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   579
         * Adds the given ModuleDescriptor to the system module list.
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   580
         * It performs link-time validation and prepares mapping from various
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   581
         * Sets to SetBuilders to emit an optimized number of sets during build.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   582
         */
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   583
        private void dedups(ModuleDescriptor md) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   584
            // exports
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   585
            for (Exports e : md.exports()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   586
                dedupSetBuilder.stringSet(e.targets());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   587
                dedupSetBuilder.exportsModifiers(e.modifiers());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   588
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   589
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   590
            // opens
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   591
            for (Opens opens : md.opens()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   592
                dedupSetBuilder.stringSet(opens.targets());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   593
                dedupSetBuilder.opensModifiers(opens.modifiers());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   594
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   595
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   596
            // requires
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   597
            for (Requires r : md.requires()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   598
                dedupSetBuilder.requiresModifiers(r.modifiers());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   599
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   600
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   601
            // uses
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   602
            dedupSetBuilder.stringSet(md.uses());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   603
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   604
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   605
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   606
         * Generate SystemModules class
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   607
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   608
        public ClassWriter getClassWriter(Configuration cf) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   609
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   610
                                             + ClassWriter.COMPUTE_FRAMES);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   611
            cw.visit(Opcodes.V1_8,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   612
                     ACC_FINAL+ACC_SUPER,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   613
                     className,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   614
                     null,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   615
                     "java/lang/Object",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   616
                     new String[] { "jdk/internal/module/SystemModules" });
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   617
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   618
            // generate <init>
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   619
            genConstructor(cw);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   620
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   621
            // generate hasSplitPackages
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   622
            genHasSplitPackages(cw);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   623
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   624
            // generate hasIncubatorModules
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   625
            genIncubatorModules(cw);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   626
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   627
            // generate moduleDescriptors
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   628
            genModuleDescriptorsMethod(cw);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   629
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   630
            // generate moduleTargets
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   631
            genModuleTargetsMethod(cw);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   632
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   633
            // generate moduleHashes
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   634
            genModuleHashesMethod(cw);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   635
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   636
            // generate moduleResolutions
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   637
            genModuleResolutionsMethod(cw);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   638
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   639
            // generate moduleReads
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   640
            genModuleReads(cw, cf);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   641
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   642
            // generate concealedPackagesToOpen and exportedPackagesToOpen
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   643
            genXXXPackagesToOpenMethods(cw);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   644
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   645
            return cw;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   646
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   647
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   648
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   649
         * Generate byteccode for no-arg constructor
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   650
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   651
        private void genConstructor(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   652
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   653
            mv.visitVarInsn(ALOAD, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   654
            mv.visitMethodInsn(INVOKESPECIAL,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   655
                               "java/lang/Object",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   656
                               "<init>",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   657
                               "()V",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   658
                               false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   659
            mv.visitInsn(RETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   660
            mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   661
            mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   662
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   663
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   664
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   665
         * Generate bytecode for hasSplitPackages method
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   666
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   667
        private void genHasSplitPackages(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   668
            boolean distinct = moduleInfos.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   669
                    .map(ModuleInfo::packages)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   670
                    .flatMap(Set::stream)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   671
                    .allMatch(new HashSet<>()::add);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   672
            boolean hasSplitPackages = !distinct;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   673
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   674
            mv = cw.visitMethod(ACC_PUBLIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   675
                                "hasSplitPackages",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   676
                                "()Z",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   677
                                "()Z",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   678
                                null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   679
            mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   680
            if (hasSplitPackages) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   681
                mv.visitInsn(ICONST_1);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   682
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   683
                mv.visitInsn(ICONST_0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   684
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   685
            mv.visitInsn(IRETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   686
            mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   687
            mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   688
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   689
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   690
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   691
         * Generate bytecode for hasIncubatorModules method
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   692
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   693
        private void genIncubatorModules(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   694
            boolean hasIncubatorModules = moduleInfos.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   695
                    .map(ModuleInfo::moduleResolution)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   696
                    .filter(mres -> (mres != null && mres.hasIncubatingWarning()))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   697
                    .findFirst()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   698
                    .isPresent();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   699
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   700
            mv = cw.visitMethod(ACC_PUBLIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   701
                                "hasIncubatorModules",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   702
                                "()Z",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   703
                                "()Z",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   704
                                null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   705
            mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   706
            if (hasIncubatorModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   707
                mv.visitInsn(ICONST_1);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   708
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   709
                mv.visitInsn(ICONST_0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   710
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   711
            mv.visitInsn(IRETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   712
            mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   713
            mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   714
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   715
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   716
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   717
         * Generate bytecode for moduleDescriptors method
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   718
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   719
        private void genModuleDescriptorsMethod(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   720
            this.mv = cw.visitMethod(ACC_PUBLIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   721
                                     "moduleDescriptors",
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   722
                                     "()" + MODULE_DESCRIPTOR_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   723
                                     "()" + MODULE_DESCRIPTOR_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   724
                                     null);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   725
            mv.visitCode();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   726
            pushInt(mv, moduleInfos.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   727
            mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   728
            mv.visitVarInsn(ASTORE, MD_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   729
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   730
            for (int index = 0; index < moduleInfos.size(); index++) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   731
                ModuleInfo minfo = moduleInfos.get(index);
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   732
                new ModuleDescriptorBuilder(minfo.descriptor(),
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   733
                                            minfo.packages(),
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   734
                                            index).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   735
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   736
            mv.visitVarInsn(ALOAD, MD_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   737
            mv.visitInsn(ARETURN);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   738
            mv.visitMaxs(0, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   739
            mv.visitEnd();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   740
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   741
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   742
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   743
         * Generate bytecode for moduleTargets method
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   744
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   745
        private void genModuleTargetsMethod(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   746
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   747
                                              "moduleTargets",
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   748
                                              "()" + MODULE_TARGET_ARRAY_SIGNATURE,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   749
                                              "()" + MODULE_TARGET_ARRAY_SIGNATURE,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   750
                                              null);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   751
            mv.visitCode();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   752
            pushInt(mv, moduleInfos.size());
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   753
            mv.visitTypeInsn(ANEWARRAY, MODULE_TARGET_CLASSNAME);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   754
            mv.visitVarInsn(ASTORE, MT_VAR);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   755
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   756
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   757
            // if java.base has a ModuleTarget attribute then generate the array
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   758
            // with one element, all other elements will be null.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   759
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   760
            ModuleInfo base = moduleInfos.get(0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   761
            if (!base.moduleName().equals("java.base"))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   762
                throw new InternalError("java.base should be first module in list");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   763
            ModuleTarget target = base.target();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   764
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   765
            int count;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   766
            if (target != null && target.targetPlatform() != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   767
                count = 1;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   768
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   769
                count = moduleInfos.size();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   770
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   771
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   772
            for (int index = 0; index < count; index++) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   773
                ModuleInfo minfo = moduleInfos.get(index);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   774
                if (minfo.target() != null) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   775
                    mv.visitVarInsn(ALOAD, MT_VAR);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   776
                    pushInt(mv, index);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   777
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   778
                    // new ModuleTarget(String)
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   779
                    mv.visitTypeInsn(NEW, MODULE_TARGET_CLASSNAME);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   780
                    mv.visitInsn(DUP);
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   781
                    mv.visitLdcInsn(minfo.target().targetPlatform());
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   782
                    mv.visitMethodInsn(INVOKESPECIAL, MODULE_TARGET_CLASSNAME,
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   783
                                       "<init>", "(Ljava/lang/String;)V", false);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   784
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   785
                    mv.visitInsn(AASTORE);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   786
                }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   787
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   788
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   789
            mv.visitVarInsn(ALOAD, MT_VAR);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   790
            mv.visitInsn(ARETURN);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   791
            mv.visitMaxs(0, 0);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   792
            mv.visitEnd();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   793
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   794
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   795
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   796
         * Generate bytecode for moduleHashes method
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   797
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   798
        private void genModuleHashesMethod(ClassWriter cw) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   799
            MethodVisitor hmv =
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   800
                cw.visitMethod(ACC_PUBLIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   801
                               "moduleHashes",
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   802
                               "()" + MODULE_HASHES_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   803
                               "()" + MODULE_HASHES_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   804
                               null);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   805
            hmv.visitCode();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   806
            pushInt(hmv, moduleInfos.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   807
            hmv.visitTypeInsn(ANEWARRAY, "jdk/internal/module/ModuleHashes");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   808
            hmv.visitVarInsn(ASTORE, MH_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   809
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   810
            for (int index = 0; index < moduleInfos.size(); index++) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   811
                ModuleInfo minfo = moduleInfos.get(index);
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   812
                if (minfo.recordedHashes() != null) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   813
                    new ModuleHashesBuilder(minfo.recordedHashes(),
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   814
                                            index,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   815
                                            hmv).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   816
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   817
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   818
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   819
            hmv.visitVarInsn(ALOAD, MH_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   820
            hmv.visitInsn(ARETURN);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   821
            hmv.visitMaxs(0, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   822
            hmv.visitEnd();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   823
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   824
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
   825
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   826
         * Generate bytecode for moduleResolutions method
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   827
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   828
        private void genModuleResolutionsMethod(ClassWriter cw) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   829
            MethodVisitor mresmv =
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   830
                cw.visitMethod(ACC_PUBLIC,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   831
                               "moduleResolutions",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   832
                               "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   833
                               "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   834
                               null);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   835
            mresmv.visitCode();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   836
            pushInt(mresmv, moduleInfos.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   837
            mresmv.visitTypeInsn(ANEWARRAY, MODULE_RESOLUTION_CLASSNAME);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   838
            mresmv.visitVarInsn(ASTORE, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   839
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   840
            for (int index=0; index < moduleInfos.size(); index++) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   841
                ModuleInfo minfo = moduleInfos.get(index);
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   842
                if (minfo.moduleResolution() != null) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   843
                    mresmv.visitVarInsn(ALOAD, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   844
                    pushInt(mresmv, index);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   845
                    mresmv.visitTypeInsn(NEW, MODULE_RESOLUTION_CLASSNAME);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   846
                    mresmv.visitInsn(DUP);
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
   847
                    mresmv.visitLdcInsn(minfo.moduleResolution().value());
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   848
                    mresmv.visitMethodInsn(INVOKESPECIAL,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   849
                                           MODULE_RESOLUTION_CLASSNAME,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   850
                                           "<init>",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   851
                                           "(I)V", false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   852
                    mresmv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   853
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   854
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   855
            mresmv.visitVarInsn(ALOAD, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   856
            mresmv.visitInsn(ARETURN);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   857
            mresmv.visitMaxs(0, 0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   858
            mresmv.visitEnd();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   859
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   860
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   861
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   862
         * Generate bytecode for moduleReads method
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   863
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   864
        private void genModuleReads(ClassWriter cw, Configuration cf) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   865
            // module name -> names of modules that it reads
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   866
            Map<String, Set<String>> map = cf.modules().stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   867
                    .collect(Collectors.toMap(
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   868
                            ResolvedModule::name,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   869
                            m -> m.reads().stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   870
                                    .map(ResolvedModule::name)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   871
                                    .collect(Collectors.toSet())));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   872
            generate(cw, "moduleReads", map, true);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   873
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   874
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   875
        /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   876
         * Generate concealedPackagesToOpen and exportedPackagesToOpen methods.
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   877
         */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   878
        private void genXXXPackagesToOpenMethods(ClassWriter cw) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   879
            ModuleFinder finder = finderOf(moduleInfos);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   880
            IllegalAccessMaps maps = IllegalAccessMaps.generate(finder);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   881
            generate(cw, "concealedPackagesToOpen", maps.concealedPackagesToOpen(), false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   882
            generate(cw, "exportedPackagesToOpen", maps.exportedPackagesToOpen(), false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   883
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   884
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   885
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   886
         * Generate method to return {@code Map<String, Set<String>>}.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   887
         *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   888
         * If {@code dedup} is true then the values are de-duplicated.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   889
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   890
        private void generate(ClassWriter cw,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   891
                              String methodName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   892
                              Map<String, Set<String>> map,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   893
                              boolean dedup) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   894
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   895
                                              methodName,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   896
                                              "()Ljava/util/Map;",
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   897
                                              "()Ljava/util/Map;",
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   898
                                              null);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   899
            mv.visitCode();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   900
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   901
            // map of Set -> local
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   902
            Map<Set<String>, Integer> locals;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   903
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   904
            // generate code to create the sets that are duplicated
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   905
            if (dedup) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   906
                Collection<Set<String>> values = map.values();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   907
                Set<Set<String>> duplicateSets = values.stream()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   908
                        .distinct()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   909
                        .filter(s -> Collections.frequency(values, s) > 1)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   910
                        .collect(Collectors.toSet());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   911
                locals = new HashMap<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   912
                int index = 1;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   913
                for (Set<String> s : duplicateSets) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   914
                    genImmutableSet(mv, s);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   915
                    mv.visitVarInsn(ASTORE, index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   916
                    locals.put(s, index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   917
                    if (++index >= MAX_LOCAL_VARS) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   918
                        break;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   919
                    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   920
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   921
            } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   922
                locals = Map.of();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   923
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   924
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   925
            // new Map$Entry[size]
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   926
            pushInt(mv, map.size());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   927
            mv.visitTypeInsn(ANEWARRAY, "java/util/Map$Entry");
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   928
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   929
            int index = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
   930
            for (var e : new TreeMap<>(map).entrySet()) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   931
                String name = e.getKey();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   932
                Set<String> s = e.getValue();
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   933
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   934
                mv.visitInsn(DUP);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   935
                pushInt(mv, index);
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   936
                mv.visitLdcInsn(name);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   937
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   938
                // if de-duplicated then load the local, otherwise generate code
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   939
                Integer varIndex = locals.get(s);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   940
                if (varIndex == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   941
                    genImmutableSet(mv, s);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   942
                } else {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   943
                    mv.visitVarInsn(ALOAD, varIndex);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   944
                }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   945
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   946
                String desc = "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/Map$Entry;";
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   947
                mv.visitMethodInsn(INVOKESTATIC,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   948
                                   "java/util/Map",
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   949
                                   "entry",
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   950
                                   desc,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   951
                                   true);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   952
                mv.visitInsn(AASTORE);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   953
                index++;
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   954
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   955
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   956
            // invoke Map.ofEntries(Map$Entry[])
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   957
            mv.visitMethodInsn(INVOKESTATIC, "java/util/Map", "ofEntries",
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   958
                    "([Ljava/util/Map$Entry;)Ljava/util/Map;", true);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   959
            mv.visitInsn(ARETURN);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   960
            mv.visitMaxs(0, 0);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   961
            mv.visitEnd();
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   962
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
   963
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   964
        /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   965
         * Generate code to generate an immutable set.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   966
         */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   967
        private void genImmutableSet(MethodVisitor mv, Set<String> set) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   968
            int size = set.size();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   969
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   970
            // use Set.of(Object[]) when there are more than 2 elements
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   971
            // use Set.of(Object) or Set.of(Object, Object) when fewer
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   972
            if (size > 2) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   973
                pushInt(mv, size);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   974
                mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   975
                int i = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
   976
                for (String element : sorted(set)) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   977
                    mv.visitInsn(DUP);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   978
                    pushInt(mv, i);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   979
                    mv.visitLdcInsn(element);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   980
                    mv.visitInsn(AASTORE);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   981
                    i++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   982
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   983
                mv.visitMethodInsn(INVOKESTATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   984
                        "java/util/Set",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   985
                        "of",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   986
                        "([Ljava/lang/Object;)Ljava/util/Set;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   987
                        true);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   988
            } else {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   989
                StringBuilder sb = new StringBuilder("(");
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
   990
                for (String element : sorted(set)) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   991
                    mv.visitLdcInsn(element);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   992
                    sb.append("Ljava/lang/Object;");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   993
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   994
                sb.append(")Ljava/util/Set;");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   995
                mv.visitMethodInsn(INVOKESTATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   996
                        "java/util/Set",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   997
                        "of",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   998
                        sb.toString(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
   999
                        true);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1000
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1001
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1002
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1003
        class ModuleDescriptorBuilder {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1004
            static final String BUILDER_TYPE = "Ljdk/internal/module/Builder;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1005
            static final String EXPORTS_TYPE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1006
                "Ljava/lang/module/ModuleDescriptor$Exports;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1007
            static final String OPENS_TYPE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1008
                "Ljava/lang/module/ModuleDescriptor$Opens;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1009
            static final String PROVIDES_TYPE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1010
                "Ljava/lang/module/ModuleDescriptor$Provides;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1011
            static final String REQUIRES_TYPE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1012
                "Ljava/lang/module/ModuleDescriptor$Requires;";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1013
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1014
            // method signature for static Builder::newExports, newOpens,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1015
            // newProvides, newRequires methods
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1016
            static final String EXPORTS_MODIFIER_SET_STRING_SET_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1017
                "(Ljava/util/Set;Ljava/lang/String;Ljava/util/Set;)"
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1018
                    + EXPORTS_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1019
            static final String EXPORTS_MODIFIER_SET_STRING_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1020
                "(Ljava/util/Set;Ljava/lang/String;)" + EXPORTS_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1021
            static final String OPENS_MODIFIER_SET_STRING_SET_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1022
                "(Ljava/util/Set;Ljava/lang/String;Ljava/util/Set;)"
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1023
                    + OPENS_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1024
            static final String OPENS_MODIFIER_SET_STRING_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1025
                "(Ljava/util/Set;Ljava/lang/String;)" + OPENS_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1026
            static final String PROVIDES_STRING_LIST_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1027
                "(Ljava/lang/String;Ljava/util/List;)" + PROVIDES_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1028
            static final String REQUIRES_SET_STRING_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1029
                "(Ljava/util/Set;Ljava/lang/String;)" + REQUIRES_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1030
            static final String REQUIRES_SET_STRING_STRING_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1031
                "(Ljava/util/Set;Ljava/lang/String;Ljava/lang/String;)" + REQUIRES_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1032
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1033
            // method signature for Builder instance methods that
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1034
            // return this Builder instance
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1035
            static final String EXPORTS_ARRAY_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1036
                "([" + EXPORTS_TYPE + ")" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1037
            static final String OPENS_ARRAY_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1038
                "([" + OPENS_TYPE + ")" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1039
            static final String PROVIDES_ARRAY_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1040
                "([" + PROVIDES_TYPE + ")" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1041
            static final String REQUIRES_ARRAY_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1042
                "([" + REQUIRES_TYPE + ")" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1043
            static final String SET_SIG = "(Ljava/util/Set;)" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1044
            static final String STRING_SIG = "(Ljava/lang/String;)" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1045
            static final String BOOLEAN_SIG = "(Z)" + BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1046
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1047
            final ModuleDescriptor md;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1048
            final Set<String> packages;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1049
            final int index;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43732
diff changeset
  1050
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1051
            ModuleDescriptorBuilder(ModuleDescriptor md, Set<String> packages, int index) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1052
                if (md.isAutomatic()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1053
                    throw new InternalError("linking automatic module is not supported");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1054
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1055
                this.md = md;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1056
                this.packages = packages;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1057
                this.index = index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1058
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1059
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1060
            void build() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1061
                // new jdk.internal.module.Builder
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1062
                newBuilder();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1063
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1064
                // requires
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1065
                requires(md.requires());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1066
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1067
                // exports
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1068
                exports(md.exports());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1069
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1070
                // opens
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1071
                opens(md.opens());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1072
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1073
                // uses
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1074
                uses(md.uses());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1075
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1076
                // provides
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1077
                provides(md.provides());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1078
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1079
                // all packages
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1080
                packages(packages);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1081
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1082
                // version
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1083
                md.version().ifPresent(this::version);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1084
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1085
                // main class
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1086
                md.mainClass().ifPresent(this::mainClass);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1087
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1088
                putModuleDescriptor();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1089
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1090
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1091
            void newBuilder() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1092
                mv.visitTypeInsn(NEW, MODULE_DESCRIPTOR_BUILDER);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1093
                mv.visitInsn(DUP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1094
                mv.visitLdcInsn(md.name());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1095
                mv.visitMethodInsn(INVOKESPECIAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1096
                    "<init>", "(Ljava/lang/String;)V", false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1097
                mv.visitVarInsn(ASTORE, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1098
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1099
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1100
                if (md.isOpen()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1101
                    setModuleBit("open", true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1102
                }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
  1103
                if (md.modifiers().contains(ModuleDescriptor.Modifier.SYNTHETIC)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1104
                    setModuleBit("synthetic", true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1105
                }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
  1106
                if (md.modifiers().contains(ModuleDescriptor.Modifier.MANDATED)) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
  1107
                    setModuleBit("mandated", true);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
  1108
                }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1109
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1110
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1111
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1112
             * Invoke Builder.<methodName>(boolean value)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1113
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1114
            void setModuleBit(String methodName, boolean value) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1115
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1116
                if (value) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1117
                    mv.visitInsn(ICONST_1);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1118
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1119
                    mv.visitInsn(ICONST_0);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1120
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1121
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1122
                    methodName, BOOLEAN_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1123
                mv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1124
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1125
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1126
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1127
             * Put ModuleDescriptor into the modules array
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1128
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1129
            void putModuleDescriptor() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1130
                mv.visitVarInsn(ALOAD, MD_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1131
                pushInt(mv, index);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1132
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1133
                mv.visitLdcInsn(md.hashCode());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1134
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1135
                    "build", "(I)Ljava/lang/module/ModuleDescriptor;",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1136
                    false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1137
                mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1138
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1139
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1140
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1141
             * Call Builder::newRequires to create Requires instances and
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1142
             * then pass it to the builder by calling:
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1143
             *      Builder.requires(Requires[])
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1144
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1145
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1146
            void requires(Set<Requires> requires) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1147
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1148
                pushInt(mv, requires.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1149
                mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor$Requires");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1150
                int arrayIndex = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1151
                for (Requires require : sorted(requires)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1152
                    String compiledVersion = null;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1153
                    if (require.compiledVersion().isPresent()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1154
                        compiledVersion = require.compiledVersion().get().toString();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1155
                    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1156
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1157
                    mv.visitInsn(DUP);               // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1158
                    pushInt(mv, arrayIndex++);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1159
                    newRequires(require.modifiers(), require.name(), compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1160
                    mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1161
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1162
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1163
                    "requires", REQUIRES_ARRAY_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1164
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1165
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1166
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1167
             * Invoke Builder.newRequires(Set<Modifier> mods, String mn, String compiledVersion)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1168
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1169
             * Set<Modifier> mods = ...
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1170
             * Builder.newRequires(mods, mn, compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1171
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1172
            void newRequires(Set<Requires.Modifier> mods, String name, String compiledVersion) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1173
                int varIndex = dedupSetBuilder.indexOfRequiresModifiers(mods);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1174
                mv.visitVarInsn(ALOAD, varIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1175
                mv.visitLdcInsn(name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1176
                if (compiledVersion != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1177
                    mv.visitLdcInsn(compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1178
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1179
                        "newRequires", REQUIRES_SET_STRING_STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1180
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1181
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1182
                        "newRequires", REQUIRES_SET_STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1183
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1184
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1185
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1186
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1187
             * Call Builder::newExports to create Exports instances and
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1188
             * then pass it to the builder by calling:
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1189
             *      Builder.exports(Exports[])
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1190
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1191
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1192
            void exports(Set<Exports> exports) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1193
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1194
                pushInt(mv, exports.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1195
                mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor$Exports");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1196
                int arrayIndex = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1197
                for (Exports export : sorted(exports)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1198
                    mv.visitInsn(DUP);    // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1199
                    pushInt(mv, arrayIndex++);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1200
                    newExports(export.modifiers(), export.source(), export.targets());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1201
                    mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1202
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1203
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1204
                    "exports", EXPORTS_ARRAY_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1205
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1206
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1207
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1208
             * Invoke
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1209
             *     Builder.newExports(Set<Exports.Modifier> ms, String pn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1210
             *                        Set<String> targets)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1211
             * or
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1212
             *     Builder.newExports(Set<Exports.Modifier> ms, String pn)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1213
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1214
             * Set<String> targets = new HashSet<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1215
             * targets.add(t);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1216
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1217
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1218
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1219
             * Set<Modifier> mods = ...
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1220
             * Builder.newExports(mods, pn, targets);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1221
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1222
            void newExports(Set<Exports.Modifier> ms, String pn, Set<String> targets) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1223
                int modifiersSetIndex = dedupSetBuilder.indexOfExportsModifiers(ms);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1224
                if (!targets.isEmpty()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1225
                    int stringSetIndex = dedupSetBuilder.indexOfStringSet(targets);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1226
                    mv.visitVarInsn(ALOAD, modifiersSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1227
                    mv.visitLdcInsn(pn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1228
                    mv.visitVarInsn(ALOAD, stringSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1229
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1230
                        "newExports", EXPORTS_MODIFIER_SET_STRING_SET_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1231
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1232
                    mv.visitVarInsn(ALOAD, modifiersSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1233
                    mv.visitLdcInsn(pn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1234
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1235
                        "newExports", EXPORTS_MODIFIER_SET_STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1236
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1237
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1238
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1239
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1240
            /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1241
             * Call Builder::newOpens to create Opens instances and
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1242
             * then pass it to the builder by calling:
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1243
             * Builder.opens(Opens[])
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1244
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1245
            void opens(Set<Opens> opens) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1246
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1247
                pushInt(mv, opens.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1248
                mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor$Opens");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1249
                int arrayIndex = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1250
                for (Opens open : sorted(opens)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1251
                    mv.visitInsn(DUP);    // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1252
                    pushInt(mv, arrayIndex++);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1253
                    newOpens(open.modifiers(), open.source(), open.targets());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1254
                    mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1255
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1256
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1257
                    "opens", OPENS_ARRAY_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1258
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1259
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1260
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1261
             * Invoke
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1262
             *     Builder.newOpens(Set<Opens.Modifier> ms, String pn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1263
             *                        Set<String> targets)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1264
             * or
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1265
             *     Builder.newOpens(Set<Opens.Modifier> ms, String pn)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1266
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1267
             * Set<String> targets = new HashSet<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1268
             * targets.add(t);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1269
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1270
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1271
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1272
             * Set<Modifier> mods = ...
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1273
             * Builder.newOpens(mods, pn, targets);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1274
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1275
            void newOpens(Set<Opens.Modifier> ms, String pn, Set<String> targets) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1276
                int modifiersSetIndex = dedupSetBuilder.indexOfOpensModifiers(ms);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1277
                if (!targets.isEmpty()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1278
                    int stringSetIndex = dedupSetBuilder.indexOfStringSet(targets);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1279
                    mv.visitVarInsn(ALOAD, modifiersSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1280
                    mv.visitLdcInsn(pn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1281
                    mv.visitVarInsn(ALOAD, stringSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1282
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1283
                        "newOpens", OPENS_MODIFIER_SET_STRING_SET_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1284
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1285
                    mv.visitVarInsn(ALOAD, modifiersSetIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1286
                    mv.visitLdcInsn(pn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1287
                    mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1288
                        "newOpens", OPENS_MODIFIER_SET_STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1289
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1290
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1291
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1292
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1293
             * Invoke Builder.uses(Set<String> uses)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1294
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1295
            void uses(Set<String> uses) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1296
                int varIndex = dedupSetBuilder.indexOfStringSet(uses);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1297
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1298
                mv.visitVarInsn(ALOAD, varIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1299
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1300
                    "uses", SET_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1301
                mv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1302
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1303
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1304
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1305
            * Call Builder::newProvides to create Provides instances and
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1306
            * then pass it to the builder by calling:
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1307
            *      Builder.provides(Provides[] provides)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1308
            *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1309
            */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1310
            void provides(Collection<Provides> provides) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1311
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1312
                pushInt(mv, provides.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1313
                mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor$Provides");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1314
                int arrayIndex = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1315
                for (Provides provide : sorted(provides)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1316
                    mv.visitInsn(DUP);    // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1317
                    pushInt(mv, arrayIndex++);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1318
                    newProvides(provide.service(), provide.providers());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1319
                    mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1320
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1321
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1322
                    "provides", PROVIDES_ARRAY_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1323
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1324
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1325
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1326
             * Invoke Builder.newProvides(String service, Set<String> providers)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1327
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1328
             * Set<String> providers = new HashSet<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1329
             * providers.add(impl);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1330
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1331
             * :
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1332
             * Builder.newProvides(service, providers);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1333
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1334
            void newProvides(String service, List<String> providers) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1335
                mv.visitLdcInsn(service);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1336
                pushInt(mv, providers.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1337
                mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1338
                int arrayIndex = 0;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1339
                for (String provider : providers) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1340
                    mv.visitInsn(DUP);    // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1341
                    pushInt(mv, arrayIndex++);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1342
                    mv.visitLdcInsn(provider);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1343
                    mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1344
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1345
                mv.visitMethodInsn(INVOKESTATIC, "java/util/List",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1346
                    "of", "([Ljava/lang/Object;)Ljava/util/List;", true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1347
                mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1348
                    "newProvides", PROVIDES_STRING_LIST_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1349
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1350
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1351
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1352
             * Invoke Builder.packages(String pn)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1353
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1354
            void packages(Set<String> packages) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1355
                int varIndex = dedupSetBuilder.newStringSet(packages);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1356
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1357
                mv.visitVarInsn(ALOAD, varIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1358
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1359
                    "packages", SET_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1360
                mv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1361
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1362
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1363
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1364
             * Invoke Builder.mainClass(String cn)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1365
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1366
            void mainClass(String cn) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1367
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1368
                mv.visitLdcInsn(cn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1369
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1370
                    "mainClass", STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1371
                mv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1372
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1373
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1374
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1375
             * Invoke Builder.version(Version v);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1376
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1377
            void version(Version v) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1378
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1379
                mv.visitLdcInsn(v.toString());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1380
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1381
                    "version", STRING_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1382
                mv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1383
            }
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1384
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1385
            void invokeBuilderMethod(String methodName, String value) {
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1386
                mv.visitVarInsn(ALOAD, BUILDER_VAR);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1387
                mv.visitLdcInsn(value);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1388
                mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER,
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1389
                    methodName, STRING_SIG, false);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1390
                mv.visitInsn(POP);
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42770
diff changeset
  1391
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1392
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1393
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1394
        class ModuleHashesBuilder {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1395
            private static final String MODULE_HASHES_BUILDER =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1396
                "jdk/internal/module/ModuleHashes$Builder";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1397
            private static final String MODULE_HASHES_BUILDER_TYPE =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1398
                "L" + MODULE_HASHES_BUILDER + ";";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1399
            static final String STRING_BYTE_ARRAY_SIG =
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1400
                "(Ljava/lang/String;[B)" + MODULE_HASHES_BUILDER_TYPE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1401
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1402
            final ModuleHashes recordedHashes;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1403
            final MethodVisitor hmv;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1404
            final int index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1405
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1406
            ModuleHashesBuilder(ModuleHashes hashes, int index, MethodVisitor hmv) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1407
                this.recordedHashes = hashes;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1408
                this.hmv = hmv;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1409
                this.index = index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1410
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1411
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1412
            /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1413
             * Build ModuleHashes
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1414
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1415
            void build() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1416
                if (recordedHashes == null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1417
                    return;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1418
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1419
                // new jdk.internal.module.ModuleHashes.Builder
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1420
                newModuleHashesBuilder();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1421
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1422
                // Invoke ModuleHashes.Builder::hashForModule
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1423
                recordedHashes
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1424
                    .names()
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1425
                    .stream()
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1426
                    .sorted()
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1427
                    .forEach(mn -> hashForModule(mn, recordedHashes.hashFor(mn)));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1428
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1429
                // Put ModuleHashes into the hashes array
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1430
                pushModuleHashes();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1431
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1432
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1433
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1434
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1435
             * Create ModuleHashes.Builder instance
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1436
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1437
            void newModuleHashesBuilder() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1438
                hmv.visitTypeInsn(NEW, MODULE_HASHES_BUILDER);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1439
                hmv.visitInsn(DUP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1440
                hmv.visitLdcInsn(recordedHashes.algorithm());
42752
844691c73832 8171373: Reduce copying during initialization of ModuleHashes
redestad
parents: 42703
diff changeset
  1441
                pushInt(hmv, ((4 * recordedHashes.names().size()) / 3) + 1);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1442
                hmv.visitMethodInsn(INVOKESPECIAL, MODULE_HASHES_BUILDER,
42752
844691c73832 8171373: Reduce copying during initialization of ModuleHashes
redestad
parents: 42703
diff changeset
  1443
                    "<init>", "(Ljava/lang/String;I)V", false);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1444
                hmv.visitVarInsn(ASTORE, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1445
                hmv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1446
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1447
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1448
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1449
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1450
             * Invoke ModuleHashes.Builder::build and put the returned
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1451
             * ModuleHashes to the hashes array
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1452
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1453
            void pushModuleHashes() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1454
                hmv.visitVarInsn(ALOAD, MH_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1455
                pushInt(hmv, index);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1456
                hmv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1457
                hmv.visitMethodInsn(INVOKEVIRTUAL, MODULE_HASHES_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1458
                    "build", "()Ljdk/internal/module/ModuleHashes;",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1459
                    false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1460
                hmv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1461
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1462
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1463
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1464
             * Invoke ModuleHashes.Builder.hashForModule(String name, byte[] hash);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1465
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1466
            void hashForModule(String name, byte[] hash) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1467
                hmv.visitVarInsn(ALOAD, BUILDER_VAR);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1468
                hmv.visitLdcInsn(name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1469
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1470
                pushInt(hmv, hash.length);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1471
                hmv.visitIntInsn(NEWARRAY, T_BYTE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1472
                for (int i = 0; i < hash.length; i++) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1473
                    hmv.visitInsn(DUP);              // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1474
                    pushInt(hmv, i);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1475
                    hmv.visitIntInsn(BIPUSH, hash[i]);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1476
                    hmv.visitInsn(BASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1477
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1478
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1479
                hmv.visitMethodInsn(INVOKEVIRTUAL, MODULE_HASHES_BUILDER,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1480
                    "hashForModule", STRING_BYTE_ARRAY_SIG, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1481
                hmv.visitInsn(POP);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1482
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1483
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1484
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1485
        /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1486
         * Wraps set creation, ensuring identical sets are properly deduplicated.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1487
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1488
        class DedupSetBuilder {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1489
            // map Set<String> to a specialized builder to allow them to be
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1490
            // deduplicated as they are requested
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1491
            final Map<Set<String>, SetBuilder<String>> stringSets = new HashMap<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1492
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1493
            // map Set<Requires.Modifier> to a specialized builder to allow them to be
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1494
            // deduplicated as they are requested
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1495
            final Map<Set<Requires.Modifier>, EnumSetBuilder<Requires.Modifier>>
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1496
                requiresModifiersSets = new HashMap<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1497
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1498
            // map Set<Exports.Modifier> to a specialized builder to allow them to be
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1499
            // deduplicated as they are requested
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1500
            final Map<Set<Exports.Modifier>, EnumSetBuilder<Exports.Modifier>>
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1501
                exportsModifiersSets = new HashMap<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1502
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1503
            // map Set<Opens.Modifier> to a specialized builder to allow them to be
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1504
            // deduplicated as they are requested
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1505
            final Map<Set<Opens.Modifier>, EnumSetBuilder<Opens.Modifier>>
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1506
                opensModifiersSets = new HashMap<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1507
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1508
            private final int stringSetVar;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1509
            private final int enumSetVar;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1510
            private final IntSupplier localVarSupplier;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1511
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1512
            DedupSetBuilder(IntSupplier localVarSupplier) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1513
                this.stringSetVar = localVarSupplier.getAsInt();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1514
                this.enumSetVar = localVarSupplier.getAsInt();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1515
                this.localVarSupplier = localVarSupplier;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1516
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1517
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1518
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1519
             * Add the given set of strings to this builder.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1520
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1521
            void stringSet(Set<String> strings) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1522
                stringSets.computeIfAbsent(strings,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1523
                    s -> new SetBuilder<>(s, stringSetVar, localVarSupplier)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1524
                ).increment();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1525
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1526
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1527
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1528
             * Add the given set of Exports.Modifiers
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1529
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1530
            void exportsModifiers(Set<Exports.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1531
                exportsModifiersSets.computeIfAbsent(mods, s ->
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1532
                                new EnumSetBuilder<>(s, EXPORTS_MODIFIER_CLASSNAME,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1533
                                        enumSetVar, localVarSupplier)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1534
                ).increment();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1535
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1536
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1537
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1538
             * Add the given set of Opens.Modifiers
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1539
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1540
            void opensModifiers(Set<Opens.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1541
                opensModifiersSets.computeIfAbsent(mods, s ->
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1542
                                new EnumSetBuilder<>(s, OPENS_MODIFIER_CLASSNAME,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1543
                                        enumSetVar, localVarSupplier)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1544
                ).increment();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1545
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1546
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1547
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1548
             * Add the given set of Requires.Modifiers
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1549
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1550
            void requiresModifiers(Set<Requires.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1551
                requiresModifiersSets.computeIfAbsent(mods, s ->
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1552
                    new EnumSetBuilder<>(s, REQUIRES_MODIFIER_CLASSNAME,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1553
                                         enumSetVar, localVarSupplier)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1554
                ).increment();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1555
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1556
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1557
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1558
             * Retrieve the index to the given set of Strings. Emit code to
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1559
             * generate it when SetBuilder::build is called.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1560
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1561
            int indexOfStringSet(Set<String> names) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1562
                return stringSets.get(names).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1563
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1564
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1565
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1566
             * Retrieve the index to the given set of Exports.Modifier.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1567
             * Emit code to generate it when EnumSetBuilder::build is called.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1568
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1569
            int indexOfExportsModifiers(Set<Exports.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1570
                return exportsModifiersSets.get(mods).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1571
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1572
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1573
            /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1574
             * Retrieve the index to the given set of Opens.Modifier.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1575
             * Emit code to generate it when EnumSetBuilder::build is called.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1576
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1577
            int indexOfOpensModifiers(Set<Opens.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1578
                return opensModifiersSets.get(mods).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1579
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1580
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1581
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1582
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1583
             * Retrieve the index to the given set of Requires.Modifier.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1584
             * Emit code to generate it when EnumSetBuilder::build is called.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1585
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1586
            int indexOfRequiresModifiers(Set<Requires.Modifier> mods) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1587
                return requiresModifiersSets.get(mods).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1588
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1589
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1590
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1591
             * Build a new string set without any attempt to deduplicate it.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1592
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1593
            int newStringSet(Set<String> names) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1594
                int index = new SetBuilder<>(names, stringSetVar, localVarSupplier).build();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1595
                assert index == stringSetVar;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1596
                return index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1597
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1598
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1599
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1600
        /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1601
         * SetBuilder generates bytecode to create one single instance of Set
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1602
         * for a given set of elements and assign to a local variable slot.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1603
         * When there is only one single reference to a Set<T>,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1604
         * it will reuse defaultVarIndex.  For a Set with multiple references,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1605
         * it will use a new local variable retrieved from the nextLocalVar
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1606
         */
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1607
        class SetBuilder<T extends Comparable<T>> {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1608
            private final Set<T> elements;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1609
            private final int defaultVarIndex;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1610
            private final IntSupplier nextLocalVar;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1611
            private int refCount;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1612
            private int localVarIndex;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1613
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1614
            SetBuilder(Set<T> elements,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1615
                       int defaultVarIndex,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1616
                       IntSupplier nextLocalVar) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1617
                this.elements = elements;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1618
                this.defaultVarIndex = defaultVarIndex;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1619
                this.nextLocalVar = nextLocalVar;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1620
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1621
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1622
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1623
             * Increments the number of references to this particular set.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1624
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1625
            final void increment() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1626
                refCount++;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1627
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1628
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1629
            /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1630
             * Generate the appropriate instructions to load an object reference
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1631
             * to the element onto the stack.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1632
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1633
            void visitElement(T element, MethodVisitor mv) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1634
                mv.visitLdcInsn(element);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1635
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1636
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1637
            /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1638
             * Build bytecode for the Set represented by this builder,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1639
             * or get the local variable index of a previously generated set
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1640
             * (in the local scope).
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1641
             *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1642
             * @return local variable index of the generated set.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1643
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1644
            final int build() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1645
                int index = localVarIndex;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1646
                if (localVarIndex == 0) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1647
                    // if non-empty and more than one set reference this builder,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1648
                    // emit to a unique local
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1649
                    index = refCount <= 1 ? defaultVarIndex
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1650
                                          : nextLocalVar.getAsInt();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1651
                    if (index < MAX_LOCAL_VARS) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1652
                        localVarIndex = index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1653
                    } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1654
                        // overflow: disable optimization by using localVarIndex = 0
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1655
                        index = defaultVarIndex;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1656
                    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1657
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1658
                    generateSetOf(index);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1659
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1660
                return index;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1661
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1662
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1663
            private void generateSetOf(int index) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1664
                if (elements.size() <= 10) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1665
                    // call Set.of(e1, e2, ...)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1666
                    StringBuilder sb = new StringBuilder("(");
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1667
                    for (T t : sorted(elements)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1668
                        sb.append("Ljava/lang/Object;");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1669
                        visitElement(t, mv);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1670
                    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1671
                    sb.append(")Ljava/util/Set;");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1672
                    mv.visitMethodInsn(INVOKESTATIC, "java/util/Set",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1673
                            "of", sb.toString(), true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1674
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1675
                    // call Set.of(E... elements)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1676
                    pushInt(mv, elements.size());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1677
                    mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1678
                    int arrayIndex = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1679
                    for (T t : sorted(elements)) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1680
                        mv.visitInsn(DUP);    // arrayref
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1681
                        pushInt(mv, arrayIndex);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1682
                        visitElement(t, mv);  // value
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1683
                        mv.visitInsn(AASTORE);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1684
                        arrayIndex++;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1685
                    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1686
                    mv.visitMethodInsn(INVOKESTATIC, "java/util/Set",
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1687
                            "of", "([Ljava/lang/Object;)Ljava/util/Set;", true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1688
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1689
                mv.visitVarInsn(ASTORE, index);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1690
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1691
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1692
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1693
        /*
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1694
         * Generates bytecode to create one single instance of EnumSet
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1695
         * for a given set of modifiers and assign to a local variable slot.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1696
         */
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1697
        class EnumSetBuilder<T extends Comparable<T>> extends SetBuilder<T> {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1698
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1699
            private final String className;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1700
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1701
            EnumSetBuilder(Set<T> modifiers, String className,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1702
                           int defaultVarIndex,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1703
                           IntSupplier nextLocalVar) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1704
                super(modifiers, defaultVarIndex, nextLocalVar);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1705
                this.className = className;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1706
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1707
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1708
            /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1709
             * Loads an Enum field.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1710
             */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1711
            void visitElement(T t, MethodVisitor mv) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1712
                mv.visitFieldInsn(GETSTATIC, className, t.toString(),
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1713
                                  "L" + className + ";");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1714
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1715
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1716
    }
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1717
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1718
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1719
     * Generate SystemModulesMap and add it as a resource.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1720
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1721
     * @return the name of the class resource added to the pool
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1722
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1723
    private String genSystemModulesMapClass(String allSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1724
                                            String defaultSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1725
                                            Map<String, String> map,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1726
                                            ResourcePoolBuilder out) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1727
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1728
                                         + ClassWriter.COMPUTE_FRAMES);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1729
        cw.visit(Opcodes.V1_8,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1730
                 ACC_FINAL+ACC_SUPER,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1731
                 SYSTEM_MODULES_MAP_CLASS,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1732
                 null,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1733
                 "java/lang/Object",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1734
                 null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1735
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1736
        // <init>
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1737
        MethodVisitor mv = cw.visitMethod(0, "<init>", "()V", null, null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1738
        mv.visitVarInsn(ALOAD, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1739
        mv.visitMethodInsn(INVOKESPECIAL,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1740
                           "java/lang/Object",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1741
                           "<init>",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1742
                           "()V",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1743
                           false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1744
        mv.visitInsn(RETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1745
        mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1746
        mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1747
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1748
        // allSystemModules()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1749
        mv = cw.visitMethod(ACC_STATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1750
                            "allSystemModules",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1751
                            "()Ljdk/internal/module/SystemModules;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1752
                            "()Ljdk/internal/module/SystemModules;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1753
                            null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1754
        mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1755
        mv.visitTypeInsn(NEW, allSystemModulesClassName);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1756
        mv.visitInsn(DUP);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1757
        mv.visitMethodInsn(INVOKESPECIAL,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1758
                           allSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1759
                           "<init>",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1760
                           "()V",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1761
                           false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1762
        mv.visitInsn(ARETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1763
        mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1764
        mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1765
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1766
        // defaultSystemModules()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1767
        mv = cw.visitMethod(ACC_STATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1768
                            "defaultSystemModules",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1769
                            "()Ljdk/internal/module/SystemModules;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1770
                            "()Ljdk/internal/module/SystemModules;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1771
                            null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1772
        mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1773
        mv.visitTypeInsn(NEW, defaultSystemModulesClassName);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1774
        mv.visitInsn(DUP);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1775
        mv.visitMethodInsn(INVOKESPECIAL,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1776
                           defaultSystemModulesClassName,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1777
                           "<init>",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1778
                           "()V",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1779
                           false);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1780
        mv.visitInsn(ARETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1781
        mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1782
        mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1783
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1784
        // moduleNames()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1785
        mv = cw.visitMethod(ACC_STATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1786
                            "moduleNames",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1787
                            "()[Ljava/lang/String;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1788
                            "()[Ljava/lang/String;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1789
                            null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1790
        mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1791
        pushInt(mv, map.size());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1792
        mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1793
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1794
        int index = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1795
        for (String moduleName : sorted(map.keySet())) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1796
            mv.visitInsn(DUP);                  // arrayref
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1797
            pushInt(mv, index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1798
            mv.visitLdcInsn(moduleName);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1799
            mv.visitInsn(AASTORE);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1800
            index++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1801
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1802
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1803
        mv.visitInsn(ARETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1804
        mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1805
        mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1806
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1807
        // classNames()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1808
        mv = cw.visitMethod(ACC_STATIC,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1809
                            "classNames",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1810
                            "()[Ljava/lang/String;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1811
                            "()[Ljava/lang/String;",
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1812
                            null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1813
        mv.visitCode();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1814
        pushInt(mv, map.size());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1815
        mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1816
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1817
        index = 0;
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1818
        for (String className : sorted(map.values())) {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1819
            mv.visitInsn(DUP);                  // arrayref
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1820
            pushInt(mv, index);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1821
            mv.visitLdcInsn(className.replace('/', '.'));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1822
            mv.visitInsn(AASTORE);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1823
            index++;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1824
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1825
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1826
        mv.visitInsn(ARETURN);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1827
        mv.visitMaxs(0, 0);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1828
        mv.visitEnd();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1829
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1830
        // write the class file to the pool as a resource
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1831
        String rn = "/java.base/" + SYSTEM_MODULES_MAP_CLASS + ".class";
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1832
        ResourcePoolEntry e = ResourcePoolEntry.create(rn, cw.toByteArray());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1833
        out.add(e);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1834
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1835
        return rn;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1836
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1837
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1838
    /**
52814
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1839
     * Returns a sorted copy of a collection.
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1840
     *
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1841
     * This is useful to ensure a deterministic iteration order.
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1842
     *
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1843
     * @return a sorted copy of the given collection.
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1844
     */
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1845
    private static <T extends Comparable<T>> List<T> sorted(Collection<T> c) {
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1846
        var l = new ArrayList<T>(c);
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1847
        Collections.sort(l);
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1848
        return l;
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1849
    }
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1850
abccada595dd 8214230: Classes generated by SystemModulesPlugin.java are not reproducable
ehelin
parents: 52515
diff changeset
  1851
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1852
     * Pushes an int constant
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1853
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1854
    private static void pushInt(MethodVisitor mv, int value) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1855
        if (value <= 5) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1856
            mv.visitInsn(ICONST_0 + value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1857
        } else if (value < Byte.MAX_VALUE) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1858
            mv.visitIntInsn(BIPUSH, value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1859
        } else if (value < Short.MAX_VALUE) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1860
            mv.visitIntInsn(SIPUSH, value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1861
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1862
            throw new IllegalArgumentException("exceed limit: " + value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1863
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1864
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1865
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1866
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1867
     * Returns a module finder that finds all modules in the given list
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1868
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1869
    private static ModuleFinder finderOf(Collection<ModuleInfo> moduleInfos) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1870
        Supplier<ModuleReader> readerSupplier = () -> null;
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1871
        Map<String, ModuleReference> namesToReference = new HashMap<>();
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1872
        for (ModuleInfo mi : moduleInfos) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1873
            String name = mi.moduleName();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1874
            ModuleReference mref
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1875
                = new ModuleReferenceImpl(mi.descriptor(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1876
                                          URI.create("jrt:/" + name),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1877
                                          readerSupplier,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1878
                                          null,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1879
                                          mi.target(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1880
                                          null,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1881
                                          null,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1882
                                          mi.moduleResolution());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45652
diff changeset
  1883
            namesToReference.put(name, mref);
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1884
        }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1885
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1886
        return new ModuleFinder() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1887
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1888
            public Optional<ModuleReference> find(String name) {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1889
                Objects.requireNonNull(name);
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1890
                return Optional.ofNullable(namesToReference.get(name));
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1891
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1892
            @Override
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1893
            public Set<ModuleReference> findAll() {
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1894
                return new HashSet<>(namesToReference.values());
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1895
            }
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1896
        };
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45004
diff changeset
  1897
    }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
  1898
}