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