jdk/src/jdk.rmic/share/classes/sun/tools/java/BinaryClass.java
author amlu
Tue, 10 Feb 2015 12:28:02 -0500
changeset 28856 1c525c8d1cf7
parent 25859 3317bb8137f4
permissions -rw-r--r--
8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo) Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28856
1c525c8d1cf7 8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo)
amlu
parents: 25859
diff changeset
     2
 * Copyright (c) 1994, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
28856
1c525c8d1cf7 8069255: Suppress deprecation warnings in jdk.rmic module (jdk repo)
amlu
parents: 25859
diff changeset
    42
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class BinaryClass extends ClassDefinition implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    BinaryConstantPool cpool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    BinaryAttribute atts;
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
    47
    Vector<ClassDeclaration> dependencies;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private boolean haveLoadedNested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public BinaryClass(Object source, ClassDeclaration declaration, int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                           ClassDeclaration superClass, ClassDeclaration interfaces[],
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
    55
                           Vector<ClassDeclaration> dependencies) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(source, 0, declaration, modifiers, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.dependencies = dependencies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.superClass = superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.interfaces = interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Flags used by basicCheck() to avoid duplicate calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * (Part of fix for 4105911)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean basicCheckDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean basicChecking = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Ready a BinaryClass for further checking.  Note that, until recently,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * BinaryClass relied on the default basicCheck() provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * ClassDefinition.  The definition here has been added to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * the information generated by collectInheritedMethods is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * for BinaryClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected void basicCheck(Environment env) throws ClassNotFound {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (tracing) env.dtEnter("BinaryClass.basicCheck: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // We need to guard against duplicate calls to basicCheck().  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // can lead to calling collectInheritedMethods() for this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // from within a previous call to collectInheritedMethods() for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // this class.  That is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // (Part of fix for 4105911)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (basicChecking || basicCheckDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (tracing) env.dtExit("BinaryClass.basicCheck: OK " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (tracing) env.dtEvent("BinaryClass.basicCheck: CHECKING " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        basicChecking = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super.basicCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Collect inheritance information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (doInheritanceChecks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            collectInheritedMethods(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        basicCheckDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        basicChecking = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (tracing) env.dtExit("BinaryClass.basicCheck: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Load a binary class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static BinaryClass load(Environment env, DataInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return load(env, in, ~(ATT_CODE|ATT_ALLCLASSES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static BinaryClass load(Environment env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                   DataInputStream in, int mask) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Read the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int magic = in.readInt();                    // JVM 4.1 ClassFile.magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (magic != JAVA_MAGIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new ClassFormatError("wrong magic: " + magic + ", expected " + JAVA_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int minor_version = in.readUnsignedShort();  // JVM 4.1 ClassFile.minor_version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int version = in.readUnsignedShort();        // JVM 4.1 ClassFile.major_version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (version < JAVA_MIN_SUPPORTED_VERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new ClassFormatError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                           sun.tools.javac.Main.getText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                               "javac.err.version.too.old",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                               String.valueOf(version)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else if ((version > JAVA_MAX_SUPPORTED_VERSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                     || (version == JAVA_MAX_SUPPORTED_VERSION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                  && minor_version > JAVA_MAX_SUPPORTED_MINOR_VERSION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new ClassFormatError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           sun.tools.javac.Main.getText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                               "javac.err.version.too.recent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                               version+"."+minor_version));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // Read the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        BinaryConstantPool cpool = new BinaryConstantPool(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // The dependencies of this class
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   138
        Vector<ClassDeclaration> dependencies = cpool.getDependencies(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Read modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int classMod = in.readUnsignedShort() & ACCM_CLASS;  // JVM 4.1 ClassFile.access_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Read the class name - from JVM 4.1 ClassFile.this_class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ClassDeclaration classDecl = cpool.getDeclaration(env, in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Read the super class name (may be null) - from JVM 4.1 ClassFile.super_class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        ClassDeclaration superClassDecl = cpool.getDeclaration(env, in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Read the interface names - from JVM 4.1 ClassFile.interfaces_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ClassDeclaration interfaces[] = new ClassDeclaration[in.readUnsignedShort()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // JVM 4.1 ClassFile.interfaces[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            interfaces[i] = cpool.getDeclaration(env, in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Allocate the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        BinaryClass c = new BinaryClass(null, classDecl, classMod, superClassDecl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                        interfaces, dependencies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        c.cpool = cpool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Add any additional dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        c.addDependency(superClassDecl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Read the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int nfields = in.readUnsignedShort();  // JVM 4.1 ClassFile.fields_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        for (int i = 0 ; i < nfields ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // JVM 4.5 field_info.access_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int fieldMod = in.readUnsignedShort() & ACCM_FIELD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // JVM 4.5 field_info.name_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Identifier fieldName = cpool.getIdentifier(in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // JVM 4.5 field_info.descriptor_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Type fieldType = cpool.getType(in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            BinaryAttribute atts = BinaryAttribute.load(in, cpool, mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            c.addMember(new BinaryMember(c, fieldMod, fieldType, fieldName, atts));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // Read the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int nmethods = in.readUnsignedShort();  // JVM 4.1 ClassFile.methods_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (int i = 0 ; i < nmethods ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // JVM 4.6 method_info.access_flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int methMod = in.readUnsignedShort() & ACCM_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // JVM 4.6 method_info.name_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            Identifier methName = cpool.getIdentifier(in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // JVM 4.6 method_info.descriptor_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            Type methType = cpool.getType(in.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            BinaryAttribute atts = BinaryAttribute.load(in, cpool, mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            c.addMember(new BinaryMember(c, methMod, methType, methName, atts));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // Read the class attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        c.atts = BinaryAttribute.load(in, cpool, mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // See if the SourceFile is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        byte data[] = c.getAttribute(idSourceFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            DataInputStream dataStream = new DataInputStream(new ByteArrayInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // JVM 4.7.2 SourceFile_attribute.sourcefile_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            c.source = cpool.getString(dataStream.readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // See if the Documentation is know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        data = c.getAttribute(idDocumentation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            c.documentation = new DataInputStream(new ByteArrayInputStream(data)).readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // Was it compiled as deprecated?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (c.getAttribute(idDeprecated) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            c.modifiers |= M_DEPRECATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // Was it synthesized by the compiler?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (c.getAttribute(idSynthetic) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            c.modifiers |= M_SYNTHETIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Called when an environment ties a binary definition to a declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * At this point, auxiliary definitions may be loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void loadNested(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        loadNested(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void loadNested(Environment env, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // Sanity check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (haveLoadedNested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // Duplicate calls most likely should not occur, but they do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // in javap.  Be tolerant of them for the time being.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // throw new CompilerError("multiple loadNested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (tracing) env.dtEvent("loadNested: DUPLICATE CALL SKIPPED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        haveLoadedNested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Read class-nesting information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            byte data[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            data = getAttribute(idInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                initInnerClasses(env, data, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // The inner classes attribute is not well-formed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // It may, for example, contain no data.  Report this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // We used to throw a CompilerError here (bug 4095108).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            env.error(0, "malformed.attribute", getClassDeclaration(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                      idInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                env.dtEvent("loadNested: MALFORMED ATTRIBUTE (InnerClasses)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void initInnerClasses(Environment env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                  byte data[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                  int flags) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        DataInputStream ds = new DataInputStream(new ByteArrayInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int nrec = ds.readUnsignedShort();  // InnerClasses_attribute.number_of_classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = 0; i < nrec; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // For each inner class name transformation, we have a record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // with the following fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            //    u2 inner_class_info_index;   // CONSTANT_Class_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            //    u2 outer_class_info_index;   // CONSTANT_Class_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            //    u2 inner_name_index;         // CONSTANT_Utf8_info index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            //    u2 inner_class_access_flags; // access_flags bitmask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // The spec states that outer_class_info_index is 0 iff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // the inner class is not a member of its enclosing class (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // it is a local or anonymous class).  The spec also states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // that if a class is anonymous then inner_name_index should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // Prior to jdk1.2, javac did not implement the spec.  Instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // it <em>always</em> set outer_class_info_index to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // enclosing outer class and if the class was anonymous,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // it set inner_name_index to be the index of a CONSTANT_Utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // entry containing the null string "" (idNull).  This code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // designed to handle either kind of class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // See also the compileClass() method in SourceClass.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // Read in the inner_class_info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // InnerClasses_attribute.classes.inner_class_info_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            int inner_index = ds.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // could check for zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            ClassDeclaration inner = cpool.getDeclaration(env, inner_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // Read in the outer_class_info.  Note that the index will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // zero if the class is "not a member".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            ClassDeclaration outer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // InnerClasses_attribute.classes.outer_class_info_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int outer_index = ds.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (outer_index != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                outer = cpool.getDeclaration(env, outer_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // Read in the inner_name_index.  This may be zero.  An anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // class will either have an inner_nm_index of zero (as the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // dictates) or it will have an inner_nm of idNull (for classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // generated by pre-1.2 compilers).  Handle both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            Identifier inner_nm = idNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // InnerClasses_attribute.classes.inner_name_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int inner_nm_index = ds.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (inner_nm_index != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                inner_nm = Identifier.lookup(cpool.getString(inner_nm_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // Read in the modifiers for the inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // InnerClasses_attribute.classes.inner_name_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            int mods = ds.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // Is the class accessible?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // The old code checked for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            //    (!inner_nm.equals(idNull) && (mods & M_PRIVATE) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // which we will preserve to keep it working for class files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // generated by 1.1 compilers.  In addition we check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            //    (outer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // as an additional check that only makes sense with 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // generated files.  Note that it is entirely possible that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // the M_PRIVATE bit is always enough.  We are being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // conservative here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // The ATT_ALLCLASSES flag causes the M_PRIVATE modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // to be ignored, and is used by tools such as 'javap' that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // wish to examine all classes regardless of the normal access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // controls that apply during compilation.  Note that anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // and local classes are still not considered accessible, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // named local classes in jdk1.1 may slip through.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // this accessibility test is an optimization, and it is safe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // err on the side of greater accessibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            boolean accessible =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                (outer != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                (!inner_nm.equals(idNull)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                ((mods & M_PRIVATE) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                 (flags & ATT_ALLCLASSES) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // The reader should note that there has been a significant change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // in the way that the InnerClasses attribute is being handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // In particular, previously the compiler called initInner() for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // <em>every</em> inner class.  Now the compiler does not call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // initInner() if the inner class is inaccessible.  This means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // that inaccessible inner classes don't have any of the processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            // from initInner() done for them: fixing the access flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // setting outerClass, setting outerMember in their outerClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // etc.  We believe this is fine: if the class is inaccessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // and binary, then everyone who needs to see its internals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // has already been compiled.  Hopefully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                Identifier nm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    Identifier.lookupInner(outer.getName(), inner_nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                // Tell the type module about the nesting relation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                Type.tClass(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                if (inner.equals(getClassDeclaration())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    // The inner class in the record is this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        ClassDefinition outerClass = outer.getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        initInner(outerClass, mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        // report the error elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                } else if (outer.equals(getClassDeclaration())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    // The outer class in the record is this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        ClassDefinition innerClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            inner.getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        initOuter(innerClass, mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        // report the error elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private void initInner(ClassDefinition outerClass, int mods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (getOuterClass() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return;             // already done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /******
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Maybe set static, protected, or private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if ((modifiers & M_PUBLIC) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            mods &= M_STATIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            mods &= M_PRIVATE | M_PROTECTED | M_STATIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        modifiers |= mods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        ******/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // For an inner class, the class access may have been weakened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // from that originally declared the source.  We must take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // actual access permissions against which we check any source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // we are currently compiling from the InnerClasses attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // We attempt to guard here against bogus combinations of modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if ((mods & M_PRIVATE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // Private cannot be combined with public or protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            mods &= ~(M_PUBLIC | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else if ((mods & M_PROTECTED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // Protected cannot be combined with public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            mods &= ~M_PUBLIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if ((mods & M_INTERFACE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // All interfaces are implicitly abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            // All interfaces that are members of a type are implicitly static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            mods |= (M_ABSTRACT | M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (outerClass.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // All types that are members of interfaces are implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // public and static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            mods |= (M_PUBLIC | M_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            mods &= ~(M_PRIVATE | M_PROTECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        modifiers = mods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        setOuterClass(outerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        for (MemberDefinition field = getFirstMember();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
             field != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
             field = field.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (field.isUplevelValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    && outerClass.getType().equals(field.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    && field.getName().toString().startsWith(prefixThis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                setOuterMember(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private void initOuter(ClassDefinition innerClass, int mods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (innerClass instanceof BinaryClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            ((BinaryClass)innerClass).initInner(this, mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        addMember(new BinaryMember(innerClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Write the class out to a given stream.  This function mirrors the loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public void write(Environment env, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        DataOutputStream data = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // write out the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        data.writeInt(JAVA_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        data.writeShort(env.getMinorVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        data.writeShort(env.getMajorVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // Write out the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        cpool.write(data, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Write class information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        data.writeShort(getModifiers() & ACCM_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        data.writeShort(cpool.indexObject(getClassDeclaration(), env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        data.writeShort((getSuperClass() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        ? cpool.indexObject(getSuperClass(), env) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        data.writeShort(interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        for (int i = 0 ; i < interfaces.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            data.writeShort(cpool.indexObject(interfaces[i], env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // count the fields and the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int fieldCount = 0, methodCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        for (MemberDefinition f = firstMember; f != null; f = f.getNextMember())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (f.isMethod()) methodCount++; else fieldCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // write out each the field count, and then each field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        data.writeShort(fieldCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (MemberDefinition f = firstMember; f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (!f.isMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                data.writeShort(f.getModifiers() & ACCM_FIELD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                String name = f.getName().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                String signature = f.getType().getTypeSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                data.writeShort(cpool.indexString(name, env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                data.writeShort(cpool.indexString(signature, env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                BinaryAttribute.write(((BinaryMember)f).atts, data, cpool, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // write out each method count, and then each method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        data.writeShort(methodCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        for (MemberDefinition f = firstMember; f != null; f = f.getNextMember()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (f.isMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                data.writeShort(f.getModifiers() & ACCM_METHOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                String name = f.getName().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                String signature = f.getType().getTypeSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                data.writeShort(cpool.indexString(name, env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                data.writeShort(cpool.indexString(signature, env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                BinaryAttribute.write(((BinaryMember)f).atts, data, cpool, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // write out the class attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        BinaryAttribute.write(atts, data, cpool, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        data.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Get the dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   504
    public Enumeration<ClassDeclaration> getDependencies() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return dependencies.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Add a dependency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public void addDependency(ClassDeclaration c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if ((c != null) && !dependencies.contains(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            dependencies.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Get the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public BinaryConstantPool getConstants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return cpool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Get a class attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public byte getAttribute(Identifier name)[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        for (BinaryAttribute att = atts ; att != null ; att = att.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (att.name.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                return att.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}