src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
author alanb
Tue, 19 Nov 2019 11:35:29 +0000
changeset 59133 580fb715b29d
parent 48203 4fd79561f38f
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:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.internal.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.OutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.ByteBuffer;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    31
import java.util.Map;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    32
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import jdk.internal.org.objectweb.asm.ClassWriter;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    35
import jdk.internal.org.objectweb.asm.ModuleVisitor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.internal.org.objectweb.asm.Opcodes;
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
    37
import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    38
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    39
import static jdk.internal.org.objectweb.asm.Opcodes.*;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * Utility class to write a ModuleDescriptor as a module-info.class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public final class ModuleInfoWriter {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    47
    private static final Map<ModuleDescriptor.Modifier, Integer>
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    48
        MODULE_MODS_TO_FLAGS = Map.of(
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    49
            ModuleDescriptor.Modifier.OPEN, ACC_OPEN,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    50
            ModuleDescriptor.Modifier.SYNTHETIC, ACC_SYNTHETIC,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    51
            ModuleDescriptor.Modifier.MANDATED, ACC_MANDATED
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    52
        );
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    53
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    54
    private static final Map<ModuleDescriptor.Requires.Modifier, Integer>
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    55
        REQUIRES_MODS_TO_FLAGS = Map.of(
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    56
            ModuleDescriptor.Requires.Modifier.TRANSITIVE, ACC_TRANSITIVE,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    57
            ModuleDescriptor.Requires.Modifier.STATIC, ACC_STATIC_PHASE,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    58
            ModuleDescriptor.Requires.Modifier.SYNTHETIC, ACC_SYNTHETIC,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    59
            ModuleDescriptor.Requires.Modifier.MANDATED, ACC_MANDATED
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    60
        );
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    61
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    62
    private static final Map<ModuleDescriptor.Exports.Modifier, Integer>
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    63
        EXPORTS_MODS_TO_FLAGS = Map.of(
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    64
            ModuleDescriptor.Exports.Modifier.SYNTHETIC, ACC_SYNTHETIC,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    65
            ModuleDescriptor.Exports.Modifier.MANDATED, ACC_MANDATED
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    66
        );
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    67
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    68
    private static final Map<ModuleDescriptor.Opens.Modifier, Integer>
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    69
        OPENS_MODS_TO_FLAGS = Map.of(
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    70
            ModuleDescriptor.Opens.Modifier.SYNTHETIC, ACC_SYNTHETIC,
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    71
            ModuleDescriptor.Opens.Modifier.MANDATED, ACC_MANDATED
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    72
        );
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    73
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    74
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    75
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    private ModuleInfoWriter() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     * Writes the given module descriptor to a module-info.class file,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     * returning it in a byte array.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     */
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
    82
    private static byte[] toModuleInfo(ModuleDescriptor md,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
    83
                                       ModuleResolution mres,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
    84
                                       ModuleTarget target) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        ClassWriter cw = new ClassWriter(0);
48203
4fd79561f38f 8191867: Module attribute in 54.0+ class file cannot contains a requires java.base with ACC_TRANSITIVE or ACC_STATIC_PHASE
alanb
parents: 47489
diff changeset
    86
        cw.visit(Opcodes.V10, ACC_MODULE, "module-info", null, null, null);
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    87
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    88
        int moduleFlags = md.modifiers().stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    89
                .map(MODULE_MODS_TO_FLAGS::get)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    90
                .reduce(0, (x, y) -> (x | y));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    91
        String vs = md.rawVersion().orElse(null);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    92
        ModuleVisitor mv = cw.visitModule(md.name(), moduleFlags, vs);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    93
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    94
        // requires
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    95
        for (ModuleDescriptor.Requires r : md.requires()) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    96
            int flags = r.modifiers().stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    97
                    .map(REQUIRES_MODS_TO_FLAGS::get)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    98
                    .reduce(0, (x, y) -> (x | y));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    99
            vs = r.rawCompiledVersion().orElse(null);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   100
            mv.visitRequire(r.name(), flags, vs);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   101
        }
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
   102
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   103
        // exports
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   104
        for (ModuleDescriptor.Exports e : md.exports()) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   105
            int flags = e.modifiers().stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   106
                    .map(EXPORTS_MODS_TO_FLAGS::get)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   107
                    .reduce(0, (x, y) -> (x | y));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   108
            String[] targets = e.targets().toArray(EMPTY_STRING_ARRAY);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   109
            mv.visitExport(e.source().replace('.', '/'), flags, targets);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   110
        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   111
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   112
        // opens
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   113
        for (ModuleDescriptor.Opens opens : md.opens()) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   114
            int flags = opens.modifiers().stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   115
                    .map(OPENS_MODS_TO_FLAGS::get)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   116
                    .reduce(0, (x, y) -> (x | y));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   117
            String[] targets = opens.targets().toArray(EMPTY_STRING_ARRAY);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   118
            mv.visitOpen(opens.source().replace('.', '/'), flags, targets);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   119
        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   120
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   121
        // uses
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   122
        md.uses().stream().map(sn -> sn.replace('.', '/')).forEach(mv::visitUse);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   123
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   124
        // provides
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   125
        for (ModuleDescriptor.Provides p : md.provides()) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   126
            mv.visitProvide(p.service().replace('.', '/'),
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   127
                            p.providers()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   128
                                .stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   129
                                .map(pn -> pn.replace('.', '/'))
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   130
                                .toArray(String[]::new));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   131
        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   132
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   133
        // add the ModulePackages attribute when there are packages that aren't
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   134
        // exported or open
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   135
        Stream<String> exported = md.exports().stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   136
                .map(ModuleDescriptor.Exports::source);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   137
        Stream<String> open = md.opens().stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   138
                .map(ModuleDescriptor.Opens::source);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   139
        long exportedOrOpen = Stream.concat(exported, open).distinct().count();
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   140
        if (md.packages().size() > exportedOrOpen) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   141
            md.packages().stream()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   142
                    .map(pn -> pn.replace('.', '/'))
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   143
                    .forEach(mv::visitPackage);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   144
        }
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
   145
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   146
        // ModuleMainClass attribute
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   147
        md.mainClass()
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   148
            .map(mc -> mc.replace('.', '/'))
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   149
            .ifPresent(mv::visitMainClass);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   151
        mv.visitEnd();
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   152
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   153
        // write ModuleResolution attribute if specified
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   154
        if (mres != null) {
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   155
            cw.visitAttribute(new ModuleResolutionAttribute(mres.value()));
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   156
        }
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   157
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   158
        // write ModuleTarget attribute if there is a target platform
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   159
        if (target != null && target.targetPlatform().length() > 0) {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   160
            cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        cw.visitEnd();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        return cw.toByteArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
     * Writes a module descriptor to the given output stream as a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
     * module-info.class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     */
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   171
    public static void write(ModuleDescriptor descriptor,
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   172
                             ModuleResolution mres,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   173
                             ModuleTarget target,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   174
                             OutputStream out)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   175
        throws IOException
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   176
    {
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   177
        byte[] bytes = toModuleInfo(descriptor, mres, target);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   178
        out.write(bytes);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   179
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   180
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   181
    /**
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   182
     * Writes a module descriptor to the given output stream as a
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   183
     * module-info.class.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42703
diff changeset
   184
     */
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   185
    public static void write(ModuleDescriptor descriptor,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   186
                             ModuleResolution mres,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   187
                             OutputStream out)
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   188
        throws IOException
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   189
    {
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   190
        write(descriptor, mres, null, out);
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   191
    }
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   192
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   193
    /**
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   194
     * Writes a module descriptor to the given output stream as a
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   195
     * module-info.class.
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   196
     */
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   197
    public static void write(ModuleDescriptor descriptor,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   198
                             ModuleTarget target,
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   199
                             OutputStream out)
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   200
        throws IOException
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   201
    {
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   202
        write(descriptor, null, target, out);
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   203
    }
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   204
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   205
    /**
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   206
     * Writes a module descriptor to the given output stream as a
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   207
     * module-info.class.
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   208
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
    public static void write(ModuleDescriptor descriptor, OutputStream out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    {
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   212
        write(descriptor, null, null, out);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     * Returns a {@code ByteBuffer} containing the given module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     * in module-info.class format.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
59133
580fb715b29d 8233922: Service binding augments module graph with observable incubator modules
alanb
parents: 48203
diff changeset
   220
        byte[] bytes = toModuleInfo(descriptor, null, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        return ByteBuffer.wrap(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
}