src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java
author redestad
Tue, 07 Aug 2018 23:08:52 +0200
changeset 51327 a19fda433921
parent 47489 6d0e943bcd24
child 52515 746df0ae4fe1
permissions -rw-r--r--
8209003: Consolidate use of empty collections in java.lang.module Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
     2
 * Copyright (c) 2014, 2016, 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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package jdk.internal.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.OutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.internal.org.objectweb.asm.Attribute;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import jdk.internal.org.objectweb.asm.ClassReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import jdk.internal.org.objectweb.asm.ClassVisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import jdk.internal.org.objectweb.asm.ClassWriter;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    41
import jdk.internal.org.objectweb.asm.ModuleVisitor;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.internal.org.objectweb.asm.Opcodes;
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    43
import jdk.internal.org.objectweb.asm.commons.ModuleHashesAttribute;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    44
import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
    45
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * Utility class to extend a module-info.class with additional attributes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
public final class ModuleInfoExtender {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    // the input stream to read the original module-info.class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private final InputStream in;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42703
diff changeset
    56
    // the packages in the ModulePackages attribute
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    57
    private Set<String> packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    59
    // the value for the module version in the Module attribute
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    private Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42703
diff changeset
    62
    // the value of the ModuleMainClass attribute
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
    65
    // the value for the ModuleTarget attribute
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
    66
    private String targetPlatform;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42703
diff changeset
    68
    // the hashes for the ModuleHashes attribute
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    69
    private ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    71
    // the value of the ModuleResolution attribute
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    72
    private ModuleResolution moduleResolution;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    73
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    private ModuleInfoExtender(InputStream in) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        this.in = in;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    79
     * Sets the packages for the ModulePackages attribute
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    80
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    81
     * @apiNote This method does not check that the package names are legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    82
     * package names or that the set of packages is a super set of the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    83
     * packages in the module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    85
    public ModuleInfoExtender packages(Set<String> packages) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
    86
        this.packages = Collections.unmodifiableSet(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
    91
     * Sets the value for the module version in the Module attribute
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public ModuleInfoExtender version(Version version) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    /**
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42703
diff changeset
    99
     * Sets the value of the ModuleMainClass attribute.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
   100
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
   101
     * @apiNote This method does not check that the main class is a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
   102
     * class name in a named package.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    public ModuleInfoExtender mainClass(String mainClass) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    /**
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   110
     * Sets the value for the ModuleTarget attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     */
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   112
    public ModuleInfoExtender targetPlatform(String targetPlatform) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   113
        this.targetPlatform = targetPlatform;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    /**
43336
be9fca030f91 8173381: osName/osArch/osVersion is missing in ModuleDescriptor created by SystemModules
mchung
parents: 42703
diff changeset
   118
     * The ModuleHashes attribute will be emitted to the module-info with
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   119
     * the hashes encapsulated in the given {@code ModuleHashes}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     * object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   122
    public ModuleInfoExtender hashes(ModuleHashes hashes) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        this.hashes = hashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    /**
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   128
     * Sets the value for the ModuleResolution attribute.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   129
     */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   130
    public ModuleInfoExtender moduleResolution(ModuleResolution mres) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   131
        this.moduleResolution = mres;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   132
        return this;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   133
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   134
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   135
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * Outputs the modified module-info.class to the given output stream.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * Once this method has been called then the Extender object should
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * be discarded.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    public void write(OutputStream out) throws IOException {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   141
        // emit to the output stream
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   142
        out.write(toByteArray());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   143
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   144
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   145
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   146
     * Returns the bytes of the modified module-info.class.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   147
     * Once this method has been called then the Extender object should
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   148
     * be discarded.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   149
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   150
    public byte[] toByteArray() throws IOException {
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   151
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   152
                                         + ClassWriter.COMPUTE_FRAMES);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        ClassReader cr = new ClassReader(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   156
        ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   157
            @Override
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   158
            public ModuleVisitor visitModule(String name, int flags, String version) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   159
                Version v = ModuleInfoExtender.this.version;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   160
                String vs = (v != null) ? v.toString() : version;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   161
                ModuleVisitor mv = super.visitModule(name, flags, vs);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   162
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   163
                // ModuleMainClass attribute
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   164
                if (mainClass != null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   165
                    mv.visitMainClass(mainClass.replace('.', '/'));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   166
                }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   167
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   168
                // ModulePackages attribute
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   169
                if (packages != null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   170
                    packages.forEach(pn -> mv.visitPackage(pn.replace('.', '/')));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   171
                }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   172
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   173
                return new ModuleVisitor(Opcodes.ASM6, mv) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   174
                    public void visitMainClass(String existingMainClass) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   175
                        // skip main class if there is a new value
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   176
                        if (mainClass == null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   177
                            super.visitMainClass(existingMainClass);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   178
                        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   179
                    }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   180
                    public void visitPackage(String existingPackage) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   181
                        // skip packages if there is a new set of packages
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   182
                        if (packages == null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   183
                            super.visitPackage(existingPackage);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   184
                        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   185
                    }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   186
                };
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   187
            }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   188
            @Override
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   189
            public void visitAttribute(Attribute attr) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   190
                String name = attr.type;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   191
                // drop existing attributes if there are replacements
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   192
                if (name.equals(ClassFileConstants.MODULE_TARGET)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   193
                    && targetPlatform != null)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   194
                    return;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   195
                if (name.equals(ClassFileConstants.MODULE_RESOLUTION)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   196
                    && moduleResolution != null)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   197
                    return;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   198
                if (name.equals(ClassFileConstants.MODULE_HASHES)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   199
                    && hashes != null)
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   200
                    return;
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   201
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   202
                super.visitAttribute(attr);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   203
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   204
            }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   205
        };
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        List<Attribute> attrs = new ArrayList<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   208
        attrs.add(new ModuleTargetAttribute());
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   209
        attrs.add(new ModuleResolutionAttribute());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   210
        attrs.add(new ModuleHashesAttribute());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
        cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
47489
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   213
        // add ModuleTarget, ModuleResolution and ModuleHashes attributes
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   214
        if (targetPlatform != null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   215
            cw.visitAttribute(new ModuleTargetAttribute(targetPlatform));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   216
        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   217
        if (moduleResolution != null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   218
            int flags = moduleResolution.value();
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   219
            cw.visitAttribute(new ModuleResolutionAttribute(flags));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   220
        }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   221
        if (hashes != null) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   222
            String algorithm = hashes.algorithm();
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   223
            List<String> names = new ArrayList<>();
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   224
            List<byte[]> values = new ArrayList<>();
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   225
            for (String name : hashes.names()) {
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   226
                names.add(name);
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   227
                values.add(hashes.hashFor(name));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   228
            }
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   229
            cw.visitAttribute(new ModuleHashesAttribute(algorithm, names, values));
6d0e943bcd24 8186236: ModuleInfoExtender should be ASM6 aware
alanb
parents: 47216
diff changeset
   230
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   232
        return cw.toByteArray();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     * Returns an {@code Extender} that may be used to add additional
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     * attributes to the module-info.class read from the given input
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
     * stream.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
    public static ModuleInfoExtender newExtender(InputStream in) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        return new ModuleInfoExtender(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
}