jdk/src/java.base/share/classes/jdk/internal/module/ClassFileAttributes.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 37779 7c84df693837
child 42703 20c39ea4a507
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
     2
 * Copyright (c) 2015, 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.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor.Requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleDescriptor.Exports;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    31
import java.lang.module.ModuleDescriptor.Opens;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor.Provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleDescriptor.Version;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    34
import java.util.ArrayList;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.HashSet;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    38
import java.util.List;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    42
import jdk.internal.misc.JavaLangModuleAccess;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    43
import jdk.internal.misc.SharedSecrets;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.internal.org.objectweb.asm.Attribute;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.internal.org.objectweb.asm.ByteVector;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.org.objectweb.asm.ClassReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.internal.org.objectweb.asm.Label;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import static jdk.internal.module.ClassFileConstants.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * Provides ASM implementations of {@code Attribute} to read and write the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * class file attributes in a module-info class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    57
public final class ClassFileAttributes {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    private ClassFileAttributes() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     * Module_attribute {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     *   // See lang-vm.html for details.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
     * }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    66
    public static class ModuleAttribute extends Attribute {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    67
        private static final JavaLangModuleAccess JLMA
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    68
            = SharedSecrets.getJavaLangModuleAccess();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        private ModuleDescriptor descriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    72
        public ModuleAttribute(ModuleDescriptor descriptor) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            super(MODULE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            this.descriptor = descriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    77
        public ModuleAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            super(MODULE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            ModuleAttribute attr = new ModuleAttribute();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    91
            // module_name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    92
            String mn = cr.readUTF8(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    93
            off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    94
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    95
            // module_flags
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    96
            int module_flags = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    97
            boolean open = ((module_flags & ACC_OPEN) != 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    98
            off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
    99
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   100
            ModuleDescriptor.Builder builder;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   101
            if (open) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   102
                builder = JLMA.newOpenModuleBuilder(mn, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   103
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   104
                builder = JLMA.newModuleBuilder(mn, false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   105
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   106
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            // requires_count and requires[requires_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            int requires_count = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            for (int i=0; i<requires_count; i++) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   111
                String dn = cr.readUTF8(off, buf).replace('/', '.');
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                int flags = cr.readUnsignedShort(off + 2);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   113
                Set<Requires.Modifier> mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                if (flags == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                    mods = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                    mods = new HashSet<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   118
                    if ((flags & ACC_TRANSITIVE) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   119
                        mods.add(Requires.Modifier.TRANSITIVE);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   120
                    if ((flags & ACC_STATIC_PHASE) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   121
                        mods.add(Requires.Modifier.STATIC);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    if ((flags & ACC_SYNTHETIC) != 0)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   123
                        mods.add(Requires.Modifier.SYNTHETIC);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                    if ((flags & ACC_MANDATED) != 0)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   125
                        mods.add(Requires.Modifier.MANDATED);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                builder.requires(mods, dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                off += 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            // exports_count and exports[exports_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            int exports_count = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            if (exports_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                for (int i=0; i<exports_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                    String pkg = cr.readUTF8(off, buf).replace('/', '.');
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   137
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   138
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   139
                    int flags = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   140
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   141
                    Set<Exports.Modifier> mods;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   142
                    if (flags == 0) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   143
                        mods = Collections.emptySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   144
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   145
                        mods = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   146
                        if ((flags & ACC_SYNTHETIC) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   147
                            mods.add(Exports.Modifier.SYNTHETIC);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   148
                        if ((flags & ACC_MANDATED) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   149
                            mods.add(Exports.Modifier.MANDATED);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   150
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   151
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   152
                    int exports_to_count = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   153
                    off += 2;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                    if (exports_to_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                        Set<String> targets = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                        for (int j=0; j<exports_to_count; j++) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   157
                            String t = cr.readUTF8(off, buf).replace('/', '.');
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                            targets.add(t);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   161
                        builder.exports(mods, pkg, targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   163
                        builder.exports(mods, pkg);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   164
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   165
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   166
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   167
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   168
            // opens_count and opens[opens_count]
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   169
            int open_count = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   170
            off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   171
            if (open_count > 0) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   172
                for (int i=0; i<open_count; i++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   173
                    String pkg = cr.readUTF8(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   174
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   175
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   176
                    int flags = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   177
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   178
                    Set<Opens.Modifier> mods;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   179
                    if (flags == 0) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   180
                        mods = Collections.emptySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   181
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   182
                        mods = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   183
                        if ((flags & ACC_SYNTHETIC) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   184
                            mods.add(Opens.Modifier.SYNTHETIC);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   185
                        if ((flags & ACC_MANDATED) != 0)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   186
                            mods.add(Opens.Modifier.MANDATED);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   187
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   188
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   189
                    int opens_to_count = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   190
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   191
                    if (opens_to_count > 0) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   192
                        Set<String> targets = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   193
                        for (int j=0; j<opens_to_count; j++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   194
                            String t = cr.readUTF8(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   195
                            off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   196
                            targets.add(t);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   197
                        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   198
                        builder.opens(mods, pkg, targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   199
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   200
                        builder.opens(mods, pkg);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
            // uses_count and uses_index[uses_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            int uses_count = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            if (uses_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                for (int i=0; i<uses_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                    String sn = cr.readClass(off, buf).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                    builder.uses(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                    off += 2;
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
            // provides_count and provides[provides_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            int provides_count = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
            if (provides_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                for (int i=0; i<provides_count; i++) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   221
                    String service = cr.readClass(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   222
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   223
                    int with_count = cr.readUnsignedShort(off);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   224
                    off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   225
                    List<String> providers = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   226
                    for (int j=0; j<with_count; j++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   227
                        String cn = cr.readClass(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   228
                        off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   229
                        providers.add(cn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   230
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   231
                    builder.provides(service, providers);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                }
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
            attr.descriptor = builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
            assert descriptor != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   249
            // module_name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   250
            String mn = descriptor.name();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   251
            int module_name_index = cw.newUTF8(mn.replace('.', '/'));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   252
            attr.putShort(module_name_index);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   253
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   254
            // module_flags
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   255
            int module_flags = 0;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   256
            if (descriptor.isOpen())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   257
                module_flags |= ACC_OPEN;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   258
            if (descriptor.isSynthetic())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   259
                module_flags |= ACC_SYNTHETIC;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   260
            attr.putShort(module_flags);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   261
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            // requires_count
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
            attr.putShort(descriptor.requires().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            // requires[requires_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            for (Requires md : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                String dn = md.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                int flags = 0;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   269
                if (md.modifiers().contains(Requires.Modifier.TRANSITIVE))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   270
                    flags |= ACC_TRANSITIVE;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   271
                if (md.modifiers().contains(Requires.Modifier.STATIC))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   272
                    flags |= ACC_STATIC_PHASE;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   273
                if (md.modifiers().contains(Requires.Modifier.SYNTHETIC))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                    flags |= ACC_SYNTHETIC;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   275
                if (md.modifiers().contains(Requires.Modifier.MANDATED))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                    flags |= ACC_MANDATED;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   277
                int index = cw.newUTF8(dn.replace('.', '/'));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                attr.putShort(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                attr.putShort(flags);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            // exports_count and exports[exports_count];
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   283
            attr.putShort(descriptor.exports().size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   284
            for (Exports e : descriptor.exports()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   285
                String pkg = e.source().replace('.', '/');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   286
                attr.putShort(cw.newUTF8(pkg));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   287
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   288
                int flags = 0;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   289
                if (e.modifiers().contains(Exports.Modifier.SYNTHETIC))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   290
                    flags |= ACC_SYNTHETIC;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   291
                if (e.modifiers().contains(Exports.Modifier.MANDATED))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   292
                    flags |= ACC_MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   293
                attr.putShort(flags);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   294
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   295
                if (e.isQualified()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   296
                    Set<String> ts = e.targets();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   297
                    attr.putShort(ts.size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   298
                    ts.forEach(t -> attr.putShort(cw.newUTF8(t.replace('.', '/'))));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   299
                } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   300
                    attr.putShort(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   301
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   302
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   303
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   304
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   305
            // opens_counts and opens[opens_counts]
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   306
            attr.putShort(descriptor.opens().size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   307
            for (Opens obj : descriptor.opens()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   308
                String pkg = obj.source().replace('.', '/');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   309
                attr.putShort(cw.newUTF8(pkg));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   310
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   311
                int flags = 0;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   312
                if (obj.modifiers().contains(Opens.Modifier.SYNTHETIC))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   313
                    flags |= ACC_SYNTHETIC;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   314
                if (obj.modifiers().contains(Opens.Modifier.MANDATED))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   315
                    flags |= ACC_MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   316
                attr.putShort(flags);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   317
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   318
                if (obj.isQualified()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   319
                    Set<String> ts = obj.targets();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   320
                    attr.putShort(ts.size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   321
                    ts.forEach(t -> attr.putShort(cw.newUTF8(t.replace('.', '/'))));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   322
                } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   323
                    attr.putShort(0);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            // uses_count and uses_index[uses_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
            if (descriptor.uses().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                attr.putShort(0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                attr.putShort(descriptor.uses().size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                for (String s : descriptor.uses()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                    String service = s.replace('.', '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                    int index = cw.newClass(service);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                    attr.putShort(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
            // provides_count and provides[provides_count]
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            if (descriptor.provides().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
                attr.putShort(0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            } else {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   343
                attr.putShort(descriptor.provides().size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   344
                for (Provides p : descriptor.provides()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                    String service = p.service().replace('.', '/');
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   346
                    attr.putShort(cw.newClass(service));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   347
                    int with_count = p.providers().size();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   348
                    attr.putShort(with_count);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                    for (String provider : p.providers()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                        attr.putShort(cw.newClass(provider.replace('.', '/')));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   360
     * ModulePackages attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
     * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   364
     * ModulePackages_attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
     *   // index to CONSTANT_utf8_info structure in constant pool representing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   366
     *   // the string "ModulePackages"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
     *   u2 attribute_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
     *   u4 attribute_length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
     *   // the number of entries in the packages table
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   371
     *   u2 packages_count;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
     *   { // index to CONSTANT_CONSTANT_utf8_info structure with the package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     *     u2 package_index
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   374
     *   } packages[package_count];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   378
    public static class ModulePackagesAttribute extends Attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
        private final Set<String> packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   381
        public ModulePackagesAttribute(Set<String> packages) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   382
            super(MODULE_PACKAGES);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
            this.packages = packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   386
        public ModulePackagesAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
            this(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
            // package count
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            int package_count = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
            // packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
            Set<String> packages = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            for (int i=0; i<package_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                String pkg = cr.readUTF8(off, buf).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                packages.add(pkg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   410
            return new ModulePackagesAttribute(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
            assert packages != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
            // package_count
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            attr.putShort(packages.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
            // packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
            packages.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
                .map(p -> p.replace('.', '/'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                .forEach(p -> attr.putShort(cw.newUTF8(p)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   438
     * ModuleVersion attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
     * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   442
     * ModuleVersion_attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
     *   // index to CONSTANT_utf8_info structure in constant pool representing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   444
     *   // the string "ModuleVersion"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
     *   u2 attribute_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
     *   u4 attribute_length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
     *   // index to CONSTANT_CONSTANT_utf8_info structure with the version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
     *   u2 version_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
     * }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
     * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   454
    public static class ModuleVersionAttribute extends Attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
        private final Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   457
        public ModuleVersionAttribute(Version version) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   458
            super(MODULE_VERSION);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
            this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   462
        public ModuleVersionAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
            this(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            String value = cr.readUTF8(off, buf);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   475
            return new ModuleVersionAttribute(Version.parse(value));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            int index = cw.newUTF8(version.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
            attr.putShort(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   493
     * ModuleMainClass attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
     * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
     * MainClass_attribute {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
     *   // index to CONSTANT_utf8_info structure in constant pool representing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   499
     *   // the string "ModuleMainClass"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
     *   u2 attribute_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
     *   u4 attribute_length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
     *   // index to CONSTANT_Class_info structure with the main class name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
     *   u2 main_class_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
     * }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
     * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   509
    public static class ModuleMainClassAttribute extends Attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        private final String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   512
        public ModuleMainClassAttribute(String mainClass) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   513
            super(MODULE_MAIN_CLASS);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
            this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   517
        public ModuleMainClassAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            this(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            String value = cr.readClass(off, buf);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   530
            return new ModuleMainClassAttribute(value);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            int index = cw.newClass(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            attr.putShort(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   548
     * ModuleTarget attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
     * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
     * TargetPlatform_attribute {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
     *   // index to CONSTANT_utf8_info structure in constant pool representing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   554
     *   // the string "ModuleTarget"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
     *   u2 attribute_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
     *   u4 attribute_length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
     *   // index to CONSTANT_CONSTANT_utf8_info structure with the OS name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
     *   u2 os_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
     *   // index to CONSTANT_CONSTANT_utf8_info structure with the OS arch
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
     *   u2 os_arch_index
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
     *   // index to CONSTANT_CONSTANT_utf8_info structure with the OS version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
     *   u2 os_version_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
     * }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
     * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   568
    public static class ModuleTargetAttribute extends Attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
        private final String osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        private final String osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
        private final String osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   573
        public ModuleTargetAttribute(String osName, String osArch, String osVersion) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   574
            super(MODULE_TARGET);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
            this.osName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
            this.osArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
            this.osVersion = osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   580
        public ModuleTargetAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
            this(null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
            String osName = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
            String osArch = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
            String osVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            int name_index = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
            if (name_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                osName = cr.readUTF8(off, buf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
            int arch_index = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
            if (arch_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
                osArch = cr.readUTF8(off, buf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
            int version_index = cr.readUnsignedShort(off);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
            if (version_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                osVersion = cr.readUTF8(off, buf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   612
            return new ModuleTargetAttribute(osName, osArch, osVersion);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
            int name_index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
            if (osName != null && osName.length() > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
                name_index = cw.newUTF8(osName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
            attr.putShort(name_index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            int arch_index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
            if (osArch != null && osArch.length() > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                arch_index = cw.newUTF8(osArch);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            attr.putShort(arch_index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
            int version_index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
            if (osVersion != null && osVersion.length() > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                version_index = cw.newUTF8(osVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
            attr.putShort(version_index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   644
     * ModuleHashes attribute.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
     * <pre> {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   648
     * ModuleHashes_attribute {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
     *   // index to CONSTANT_utf8_info structure in constant pool representing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   650
     *   // the string "ModuleHashes"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
     *   u2 attribute_name_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
     *   u4 attribute_length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   654
     *   // index to CONSTANT_utf8_info structure with algorithm name
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
     *   u2 algorithm_index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
     *   // the number of entries in the hashes table
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   658
     *   u2 hashes_count;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   659
     *   {   u2 module_name_index
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   660
     *       u2 hash_length;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   661
     *       u1 hash[hash_length];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   662
     *   } hashes[hashes_count];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
     * } </pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   666
    static class ModuleHashesAttribute extends Attribute {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   667
        private final ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   669
        ModuleHashesAttribute(ModuleHashes hashes) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   670
            super(MODULE_HASHES);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
            this.hashes = hashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   674
        ModuleHashesAttribute() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
            this(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
        protected Attribute read(ClassReader cr,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
                                 int off,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
                                 int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
                                 char[] buf,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
                                 int codeOff,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
                                 Label[] labels)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            String algorithm = cr.readUTF8(off, buf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   689
            int hashes_count = cr.readUnsignedShort(off);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
            off += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   692
            Map<String, byte[]> map = new HashMap<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   693
            for (int i=0; i<hashes_count; i++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   694
                String mn = cr.readUTF8(off, buf).replace('/', '.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   695
                off += 2;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   696
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   697
                int hash_length = cr.readUnsignedShort(off);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
                off += 2;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   699
                byte[] hash = new byte[hash_length];
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   700
                for (int j=0; j<hash_length; j++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   701
                    hash[j] = (byte) (0xff & cr.readByte(off+j));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   702
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   703
                off += hash_length;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   704
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   705
                map.put(mn, hash);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   708
            ModuleHashes hashes = new ModuleHashes(algorithm, map);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   710
            return new ModuleHashesAttribute(hashes);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
        protected ByteVector write(ClassWriter cw,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
                                   byte[] code,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
                                   int len,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
                                   int maxStack,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
                                   int maxLocals)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
            ByteVector attr = new ByteVector();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
            int index = cw.newUTF8(hashes.algorithm());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
            attr.putShort(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
            Set<String> names = hashes.names();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
            attr.putShort(names.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   728
            for (String mn : names) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   729
                byte[] hash = hashes.hashFor(mn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
                assert hash != null;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   731
                attr.putShort(cw.newUTF8(mn.replace('.', '/')));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   732
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   733
                attr.putShort(hash.length);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   734
                for (byte b: hash) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   735
                    attr.putByte(b);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37779
diff changeset
   736
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            return attr;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
}