jdk/src/java.base/share/classes/java/lang/module/ModuleInfo.java
author alanb
Thu, 19 May 2016 13:10:08 +0100
changeset 38425 3191fb2435f8
parent 37779 7c84df693837
child 39050 9de41b79ec7e
permissions -rw-r--r--
8148834: Update module-info reader/writer to 53.0 Reviewed-by: sundar, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
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 java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.DataInput;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.DataInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.EOFException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleDescriptor.Requires.Modifier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.BufferUnderflowException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashSet;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    40
import java.util.LinkedHashSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.function.Supplier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    45
import jdk.internal.module.ModuleHashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import static jdk.internal.module.ClassFileConstants.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * Read module information from a {@code module-info} class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * @implNote The rationale for the hand-coded reader is startup performance
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * and fine control over the throwing of InvalidModuleDescriptorException.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
final class ModuleInfo {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    // supplies the set of packages when ConcealedPackages not present
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    private final Supplier<Set<String>> packageFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    // indicates if the Hashes attribute should be parsed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private final boolean parseHashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    // the builder, created when parsing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    private ModuleDescriptor.Builder builder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private ModuleInfo(Supplier<Set<String>> pf, boolean ph) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        packageFinder = pf;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        parseHashes = ph;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    private ModuleInfo(Supplier<Set<String>> pf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        this(pf, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     * Reads a {@code module-info.class} from the given input stream.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    public static ModuleDescriptor read(InputStream in,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                                        Supplier<Set<String>> pf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            return new ModuleInfo(pf).doRead(new DataInputStream(in));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            // IllegalArgumentException means a malformed class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            throw invalidModuleDescriptor(iae.getMessage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        } catch (EOFException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            throw truncatedModuleDescriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * Reads a {@code module-info.class} from the given byte buffer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * @throws UncheckedIOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    public static ModuleDescriptor read(ByteBuffer bb,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                                        Supplier<Set<String>> pf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            return new ModuleInfo(pf).doRead(new DataInputWrapper(bb));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            // IllegalArgumentException means a malformed class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            throw invalidModuleDescriptor(iae.getMessage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        } catch (EOFException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            throw truncatedModuleDescriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * Reads a {@code module-info.class} from the given byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     * but ignore the {@code Hashes} attribute.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * @throws UncheckedIOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    static ModuleDescriptor readIgnoringHashes(ByteBuffer bb,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                                               Supplier<Set<String>> pf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            return new ModuleInfo(pf, false).doRead(new DataInputWrapper(bb));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            throw invalidModuleDescriptor(iae.getMessage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        } catch (EOFException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            throw truncatedModuleDescriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     * Reads the input as a module-info class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     * @throws IllegalArgumentException if thrown by the ModuleDescriptor.Builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     *         because an identifier is not a legal Java identifier, duplicate
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     *         exports, and many other reasons
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    private ModuleDescriptor doRead(DataInput in) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        int magic = in.readInt();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        if (magic != 0xCAFEBABE)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            throw invalidModuleDescriptor("Bad magic number");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        int minor_version = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        int major_version = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        if (major_version < 53) {
38425
3191fb2435f8 8148834: Update module-info reader/writer to 53.0
alanb
parents: 37779
diff changeset
   157
            throw invalidModuleDescriptor("Must be >= 53.0");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        ConstantPool cpool = new ConstantPool(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        int access_flags = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        if (access_flags != ACC_MODULE)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            throw invalidModuleDescriptor("access_flags should be ACC_MODULE");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        int this_class = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        String mn = cpool.getClassName(this_class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        int suffix = mn.indexOf("/module-info");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        if (suffix < 1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            throw invalidModuleDescriptor("this_class not of form name/module-info");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        mn = mn.substring(0, suffix).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        builder = new ModuleDescriptor.Builder(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        int super_class = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        if (super_class > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            throw invalidModuleDescriptor("bad #super_class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        int interfaces_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        if (interfaces_count > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            throw invalidModuleDescriptor("Bad #interfaces");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        int fields_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        if (fields_count > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            throw invalidModuleDescriptor("Bad #fields");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        int methods_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        if (methods_count > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            throw invalidModuleDescriptor("Bad #methods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        int attributes_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        // the names of the attributes found in the class file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        Set<String> attributes = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        for (int i = 0; i < attributes_count ; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            int name_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
            String attribute_name = cpool.getUtf8(name_index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            int length = in.readInt();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            boolean added = attributes.add(attribute_name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            if (!added && isAttributeAtMostOnce(attribute_name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                throw invalidModuleDescriptor("More than one "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                                              + attribute_name + " attribute");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            switch (attribute_name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                case MODULE :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                    readModuleAttribute(mn, in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                case CONCEALED_PACKAGES :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                    readConcealedPackagesAttribute(in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                case VERSION :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                    readVersionAttribute(in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                case MAIN_CLASS :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
                    readMainClassAttribute(in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                case TARGET_PLATFORM :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                    readTargetPlatformAttribute(in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                case HASHES :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                    if (parseHashes) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                        readHashesAttribute(in, cpool);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                        in.skipBytes(length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                default:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                    if (isAttributeDisallowed(attribute_name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                        throw invalidModuleDescriptor(attribute_name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                                                      + " attribute not allowed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                        in.skipBytes(length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        // the Module attribute is required
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        if (!attributes.contains(MODULE)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            throw invalidModuleDescriptor(MODULE + " attribute not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        // If the ConcealedPackages attribute is not present then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
        // packageFinder is used to to find any non-exported packages.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
        if (!attributes.contains(CONCEALED_PACKAGES) && packageFinder != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
            Set<String> pkgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                pkgs = new HashSet<>(packageFinder.get());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
            } catch (UncheckedIOException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                throw x.getCause();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            pkgs.removeAll(builder.exportedPackages());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            builder.conceals(pkgs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
        // Was the Synthetic attribute present?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        if (attributes.contains(SYNTHETIC))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            builder.synthetic(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        return builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
     * Reads the Module attribute.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
    private void readModuleAttribute(String mn, DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
        int requires_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        if (requires_count == 0 && !mn.equals("java.base")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            throw invalidModuleDescriptor("The requires table must have"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
                                          + " at least one entry");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        for (int i=0; i<requires_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            int flags = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            String dn = cpool.getUtf8(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            Set<Modifier> mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            if (flags == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                mods = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
                mods = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
                if ((flags & ACC_PUBLIC) != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                    mods.add(Modifier.PUBLIC);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                if ((flags & ACC_SYNTHETIC) != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                    mods.add(Modifier.SYNTHETIC);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
                if ((flags & ACC_MANDATED) != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
                    mods.add(Modifier.MANDATED);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            builder.requires(mods, dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        int exports_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        if (exports_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
            for (int i=0; i<exports_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                String pkg = cpool.getUtf8(index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                int exports_to_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                if (exports_to_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                    Set<String> targets = new HashSet<>(exports_to_count);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                    for (int j=0; j<exports_to_count; j++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                        int exports_to_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                        targets.add(cpool.getUtf8(exports_to_index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
                    builder.exports(pkg, targets);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
                    builder.exports(pkg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        int uses_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        if (uses_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            for (int i=0; i<uses_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                String sn = cpool.getClassName(index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                builder.uses(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
        int provides_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        if (provides_count > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            Map<String, Set<String>> pm = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
            for (int i=0; i<provides_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                int with_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                String sn = cpool.getClassName(index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
                String cn = cpool.getClassName(with_index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                // computeIfAbsent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                Set<String> providers = pm.get(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                if (providers == null) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   341
                    providers = new LinkedHashSet<>(); // preserve order
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                    pm.put(sn, providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                providers.add(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            for (Map.Entry<String, Set<String>> e : pm.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
                builder.provides(e.getKey(), e.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
    }
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
     * Reads the ConcealedPackages attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
    private void readConcealedPackagesAttribute(DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
        int package_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        for (int i=0; i<package_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            String pn = cpool.getUtf8(index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
            builder.conceals(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
     * Reads the Version attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
    private void readVersionAttribute(DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
        int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
        builder.version(cpool.getUtf8(index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     * Reads the MainClass attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
    private void readMainClassAttribute(DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        builder.mainClass(cpool.getClassName(index).replace('/', '.'));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
     * Reads the TargetPlatform attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
    private void readTargetPlatformAttribute(DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        int name_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
        if (name_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
            builder.osName(cpool.getUtf8(name_index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
        int arch_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
        if (arch_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
            builder.osArch(cpool.getUtf8(arch_index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
        int version_index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
        if (version_index != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
            builder.osVersion(cpool.getUtf8(version_index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
     * Reads the Hashes attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
     * @apiNote For now the hash is stored in base64 as a UTF-8 string, this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
     * should be changed to be an array of u1.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
    private void readHashesAttribute(DataInput in, ConstantPool cpool)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
        int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
        String algorithm = cpool.getUtf8(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
        int hash_count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        Map<String, String> map = new HashMap<>(hash_count);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        for (int i=0; i<hash_count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
            index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
            String dn = cpool.getUtf8(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
            index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            String hash = cpool.getUtf8(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
            map.put(dn, hash);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   429
        builder.hashes(new ModuleHashes(algorithm, map));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
     * Returns true if the given attribute can be present at most once
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
     * in the class file. Returns false otherwise.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
    private static boolean isAttributeAtMostOnce(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        if (name.equals(MODULE) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                name.equals(SOURCE_FILE) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
                name.equals(SDE) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                name.equals(CONCEALED_PACKAGES) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
                name.equals(VERSION) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
                name.equals(MAIN_CLASS) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
                name.equals(TARGET_PLATFORM) ||
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
                name.equals(HASHES))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
        return false;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
     * Return true if the given attribute name is the name of a pre-defined
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
     * attribute that is not allowed in the class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
     * Except for Module, InnerClasses, Synthetic, SourceFile, SourceDebugExtension,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
     * and Deprecated, none of the pre-defined attributes in JVMS 4.7 may appear.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
    private static boolean isAttributeDisallowed(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
        Set<String> notAllowed = predefinedNotAllowed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        if (notAllowed == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
            notAllowed = Set.of(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                    "ConstantValue",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                    "Code",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                    "StackMapTable",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                    "Exceptions",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                    "EnclosingMethod",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                    "Signature",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                    "LineNumberTable",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                    "LocalVariableTable",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                    "LocalVariableTypeTable",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                    "RuntimeVisibleAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                    "RuntimeInvisibleAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
                    "RuntimeVisibleParameterAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
                    "RuntimeInvisibleParameterAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
                    "RuntimeVisibleTypeAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                    "RuntimeInvisibleTypeAnnotations",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                    "AnnotationDefault",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                    "BootstrapMethods",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
                    "MethodParameters");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
            predefinedNotAllowed = notAllowed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
        return notAllowed.contains(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
    // lazily created set the pre-defined attributes that are not allowed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
    private static volatile Set<String> predefinedNotAllowed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
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
     * The constant pool in a class file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
    private static class ConstantPool {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
        static final int CONSTANT_Utf8 = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
        static final int CONSTANT_Integer = 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
        static final int CONSTANT_Float = 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
        static final int CONSTANT_Long = 5;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
        static final int CONSTANT_Double = 6;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
        static final int CONSTANT_Class = 7;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
        static final int CONSTANT_String = 8;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
        static final int CONSTANT_Fieldref = 9;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
        static final int CONSTANT_Methodref = 10;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
        static final int CONSTANT_InterfaceMethodref = 11;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
        static final int CONSTANT_NameAndType = 12;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
        static final int CONSTANT_MethodHandle = 15;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        static final int CONSTANT_MethodType = 16;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        static final int CONSTANT_InvokeDynamic = 18;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        private static class Entry {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            protected Entry(int tag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
                this.tag = tag;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
            final int tag;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
        private static class IndexEntry extends Entry {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            IndexEntry(int tag, int index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
                super(tag);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
                this.index = index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
            final int index;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
        private static class Index2Entry extends Entry {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            Index2Entry(int tag, int index1, int index2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                super(tag);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                this.index1 = index1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
                this.index2 = index2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            final int index1,  index2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
        private static class ValueEntry extends Entry {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
            ValueEntry(int tag, Object value) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
                super(tag);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
                this.value = value;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            final Object value;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        final Entry[] pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
        ConstantPool(DataInput in) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            int count = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            pool = new Entry[count];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            for (int i = 1; i < count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                int tag = in.readUnsignedByte();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                switch (tag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
                    case CONSTANT_Utf8:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
                        String svalue = in.readUTF();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                        pool[i] = new ValueEntry(tag, svalue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                    case CONSTANT_Class:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
                    case CONSTANT_String:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
                        int index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
                        pool[i] = new IndexEntry(tag, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
                    case CONSTANT_Double:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
                        double dvalue = in.readDouble();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
                        pool[i] = new ValueEntry(tag, dvalue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
                        i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
                    case CONSTANT_Fieldref:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
                    case CONSTANT_InterfaceMethodref:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
                    case CONSTANT_Methodref:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
                    case CONSTANT_InvokeDynamic:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
                    case CONSTANT_NameAndType:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
                        int index1 = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
                        int index2 = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
                        pool[i] = new Index2Entry(tag, index1, index2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
                    case CONSTANT_MethodHandle:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
                        int refKind = in.readUnsignedByte();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                        index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
                        pool[i] = new Index2Entry(tag, refKind, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
                    case CONSTANT_MethodType:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                        index = in.readUnsignedShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                        pool[i] = new IndexEntry(tag, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                    case CONSTANT_Float:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                        float fvalue = in.readFloat();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
                        pool[i] = new ValueEntry(tag, fvalue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
                    case CONSTANT_Integer:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
                        int ivalue = in.readInt();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                        pool[i] = new ValueEntry(tag, ivalue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                    case CONSTANT_Long:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
                        long lvalue = in.readLong();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                        pool[i] = new ValueEntry(tag, lvalue);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
                        i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                    default:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                        throw invalidModuleDescriptor("Bad constant pool entry: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
                                                      + i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        String getClassName(int index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
            checkIndex(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
            Entry e = pool[index];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            if (e.tag != CONSTANT_Class) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
                throw invalidModuleDescriptor("CONSTANT_Class expected at entry: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
                                              + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
            return getUtf8(((IndexEntry) e).index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        String getUtf8(int index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
            checkIndex(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
            Entry e = pool[index];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
            if (e.tag != CONSTANT_Utf8) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                throw invalidModuleDescriptor("CONSTANT_Utf8 expected at entry: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                                              + index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
            return (String) (((ValueEntry) e).value);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
        void checkIndex(int index) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
            if (index < 1 || index >= pool.length)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                throw invalidModuleDescriptor("Index into constant pool out of range");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
     * A DataInput implementation that reads from a ByteBuffer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
    private static class DataInputWrapper implements DataInput {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
        private final ByteBuffer bb;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
        DataInputWrapper(ByteBuffer bb) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            this.bb = bb;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
        public void readFully(byte b[]) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
            readFully(b, 0, b.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
        public void readFully(byte b[], int off, int len) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
                bb.get(b, off, len);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
        public int skipBytes(int n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
            int skip = Math.min(n, bb.remaining());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
            bb.position(bb.position() + skip);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
            return skip;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
        public boolean readBoolean() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
                int ch = bb.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
                return (ch != 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
        public byte readByte() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
                return bb.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
        public int readUnsignedByte() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
                return ((int) bb.get()) & 0xff;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
        public short readShort() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
                return bb.getShort();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
        public int readUnsignedShort() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                return ((int) bb.getShort()) & 0xffff;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
        public char readChar() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
                return bb.getChar();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
        public int readInt() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
                return bb.getInt();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
        public long readLong() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
                return bb.getLong();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
                throw new EOFException();
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
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
        public float readFloat() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
                return bb.getFloat();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        public double readDouble() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
                return bb.getDouble();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
            } catch (BufferUnderflowException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
                throw new EOFException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
        public String readLine() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
            throw new RuntimeException("not implemented");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
        public String readUTF() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
            // ### Need to measure the performance and feasibility of using
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
            // the UTF-8 decoder instead.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
            return DataInputStream.readUTF(this);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
     * Returns an InvalidModuleDescriptorException with the given detail
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
     * message
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
    private static InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
    invalidModuleDescriptor(String msg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
        return new InvalidModuleDescriptorException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
     * Returns an InvalidModuleDescriptorException with a detail message to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
     * indicate that the class file is truncated.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
    private static InvalidModuleDescriptorException truncatedModuleDescriptor() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
        return invalidModuleDescriptor("Truncated module-info.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
}