src/java.base/share/classes/jdk/internal/module/ModuleInfoExtender.java
author rriggs
Sun, 29 Oct 2017 18:20:53 -0400
changeset 47471 304ef03403b1
parent 47216 71c04702a3d5
child 47489 6d0e943bcd24
permissions -rw-r--r--
8190323: "the the" typos Reviewed-by: rriggs Contributed-by: christoph.dreis@freenet.de
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.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import jdk.internal.org.objectweb.asm.Attribute;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import jdk.internal.org.objectweb.asm.ClassReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.internal.org.objectweb.asm.ClassVisitor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.internal.org.objectweb.asm.Opcodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import static jdk.internal.module.ClassFileAttributes.*;
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
     * A ClassVisitor that supports adding class file attributes. If an
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43336
diff changeset
   137
     * attribute already exists then the first occurrence of the attribute
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * is replaced.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    private static class AttributeAddingClassVisitor extends ClassVisitor {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        private Map<String, Attribute> attrs = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        AttributeAddingClassVisitor(int api, ClassVisitor cv) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            super(api, cv);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        void addAttribute(Attribute attr) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            attrs.put(attr.type, attr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        public void visitAttribute(Attribute attr) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            String name = attr.type;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            Attribute replacement = attrs.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            if (replacement != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                attr = replacement;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                attrs.remove(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            super.visitAttribute(attr);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        }
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
         * Adds any remaining attributes that weren't replaced to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
         * class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        void finish() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            attrs.values().forEach(a -> super.visitAttribute(a));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            attrs.clear();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
     * Outputs the modified module-info.class to the given output stream.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
     * Once this method has been called then the Extender object should
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
     * be discarded.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    public void write(OutputStream out) throws IOException {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   178
        // emit to the output stream
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   179
        out.write(toByteArray());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   180
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   181
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   182
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   183
     * Returns the bytes of the modified module-info.class.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   184
     * Once this method has been called then the Extender object should
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   185
     * be discarded.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   186
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   187
    public byte[] toByteArray() throws IOException {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        ClassWriter cw
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        AttributeAddingClassVisitor cv
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            = new AttributeAddingClassVisitor(Opcodes.ASM5, cw);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        ClassReader cr = new ClassReader(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   196
        if (packages != null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   197
            cv.addAttribute(new ModulePackagesAttribute(packages));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        if (mainClass != null)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   199
            cv.addAttribute(new ModuleMainClassAttribute(mainClass));
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   200
        if (targetPlatform != null)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44359
diff changeset
   201
            cv.addAttribute(new ModuleTargetAttribute(targetPlatform));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        if (hashes != null)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   203
            cv.addAttribute(new ModuleHashesAttribute(hashes));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   204
        if (moduleResolution != null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   205
            cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value()));
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<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        // prototypes of attributes that should be parsed
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   210
        attrs.add(new ModuleAttribute(version));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   211
        attrs.add(new ModulePackagesAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   212
        attrs.add(new ModuleMainClassAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   213
        attrs.add(new ModuleTargetAttribute());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41352
diff changeset
   214
        attrs.add(new ModuleHashesAttribute());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        // add any attributes that didn't replace previous attributes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        cv.finish();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents: 37779
diff changeset
   221
        return cw.toByteArray();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     * Returns an {@code Extender} that may be used to add additional
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     * attributes to the module-info.class read from the given input
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     * stream.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
    public static ModuleInfoExtender newExtender(InputStream in) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        return new ModuleInfoExtender(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
}