jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ClassParser.java
author joehw
Mon, 17 Apr 2017 16:24:10 -0700
changeset 44797 8b3b3b911b8a
parent 25868 686eef1e7a79
child 46174 5611d2529b49
permissions -rw-r--r--
8162572: Update License Header for all JAXP sources Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
/*
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    14
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
 */
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.classfile;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import  com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import  java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import  java.util.zip.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * Wrapper class that parses a given Java .class file. The method <A
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * href ="#parse">parse</A> returns a <A href ="JavaClass.html">
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * JavaClass</A> object on success. When an I/O error or an
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * inconsistency occurs an appropiate exception is propagated back to
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * the caller.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * The structure and the names comply, except for a few conveniences,
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * exactly with the <A href="ftp://java.sun.com/docs/specs/vmspec.ps">
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * JVM specification 1.0</a>. See this paper for
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * further details about the structure of a bytecode file.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
public final class ClassParser {
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
  private DataInputStream file;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  private ZipFile         zip;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
  private String          file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  private int             class_name_index, superclass_name_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  private int             major, minor; // Compiler version
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  private int             access_flags; // Access rights of parsed class
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  private int[]           interfaces; // Names of implemented interfaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  private ConstantPool    constant_pool; // collection of constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  private Field[]         fields; // class fields, i.e., its variables
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  private Method[]        methods; // methods defined in the class
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  private Attribute[]     attributes; // attributes defined in the class
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
  private boolean         is_zip; // Loaded from zip file
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  private static final int BUFSIZE = 8192;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * Parse class from the given stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   * @param file Input stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   * @param file_name File name
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  public ClassParser(InputStream file, String file_name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    this.file_name = file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    String clazz = file.getClass().getName(); // Not a very clean solution ...
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    is_zip = clazz.startsWith("java.util.zip.") || clazz.startsWith("java.util.jar.");
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    if(file instanceof DataInputStream) // Is already a data stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
      this.file = (DataInputStream)file;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
      this.file = new DataInputStream(new BufferedInputStream(file, BUFSIZE));
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /** Parse class from given .class file.
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * @param file_name file name
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  public ClassParser(String file_name) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    is_zip = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    this.file_name = file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    file = new DataInputStream(new BufferedInputStream
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                               (new FileInputStream(file_name), BUFSIZE));
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  /** Parse class from given .class file in a ZIP-archive
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * @param file_name file name
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  public ClassParser(String zip_file, String file_name) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    is_zip = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    zip = new ZipFile(zip_file);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    ZipEntry entry = zip.getEntry(file_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    this.file_name = file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    file = new DataInputStream(new BufferedInputStream(zip.getInputStream(entry),
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                                                       BUFSIZE));
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
   * Parse the given Java class file and return an object that represents
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
   * the contained data, i.e., constants, methods, fields and commands.
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
   * A <em>ClassFormatException</em> is raised, if the file is not a valid
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * .class file. (This does not include verification of the byte code as it
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * is performed by the java interpreter).
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * @return Class object representing the parsed class file
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  public JavaClass parse() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    /****************** Read headers ********************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    // Check magic tag of class file
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    readID();
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    // Get compiler version
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    readVersion();
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    /****************** Read constant pool and related **************/
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    // Read constant pool entries
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    readConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    // Get class information
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    readClassInfo();
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    // Get interface information, i.e., implemented interfaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    readInterfaces();
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    /****************** Read class fields and methods ***************/
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    // Read class fields, i.e., the variables of the class
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    readFields();
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    // Read class methods, i.e., the functions in the class
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    readMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    // Read class attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    readAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    // Check for unknown variables
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    //Unknown[] u = Unknown.getUnknownAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    //for(int i=0; i < u.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    //  System.err.println("WARNING: " + u[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    // Everything should have been read now
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    //      if(file.available() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    //        int bytes = file.available();
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    //        byte[] buf = new byte[bytes];
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    //        file.read(buf);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    //        if(!(is_zip && (buf.length == 1))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    //          System.err.println("WARNING: Trailing garbage at end of " + file_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    //          System.err.println(bytes + " extra bytes: " + Utility.toHexString(buf));
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    //        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    //      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    // Read everything of interest, so close the file
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    file.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    if(zip != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
      zip.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    // Return the information we have gathered in a new object
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    return new JavaClass(class_name_index, superclass_name_index,
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                         file_name, major, minor, access_flags,
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                         constant_pool, interfaces, fields,
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                         methods, attributes, is_zip? JavaClass.ZIP : JavaClass.FILE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   * Read information about the attributes of the class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
  private final void readAttributes() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    int attributes_count;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    attributes_count = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    attributes       = new Attribute[attributes_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
      attributes[i] = Attribute.readAttribute(file, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   * Read information about the class and its super class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
  private final void readClassInfo() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    access_flags = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    /* Interfaces are implicitely abstract, the flag should be set
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * according to the JVM specification.
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    if((access_flags & Constants.ACC_INTERFACE) != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
      access_flags |= Constants.ACC_ABSTRACT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    if(((access_flags & Constants.ACC_ABSTRACT) != 0) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
       ((access_flags & Constants.ACC_FINAL)    != 0 ))
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
      throw new ClassFormatException("Class can't be both final and abstract");
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    class_name_index      = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    superclass_name_index = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   * Read constant pool entries.
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
  private final void readConstantPool() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    constant_pool = new ConstantPool(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   * Read information about the fields of the class, i.e., its variables.
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
  private final void readFields() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    int fields_count;
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    fields_count = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    fields       = new Field[fields_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    for(int i=0; i < fields_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
      fields[i] = new Field(file, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
  /******************** Private utility methods **********************/
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
   * Check whether the header of the file is ok.
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
   * Of course, this has to be the first action on successive file reads.
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
  private final void readID() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
    int magic = 0xCAFEBABE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
    if(file.readInt() != magic)
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
      throw new ClassFormatException(file_name + " is not a Java .class file");
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   * Read information about the interfaces implemented by this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
  private final void readInterfaces() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    int interfaces_count;
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    interfaces_count = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    interfaces       = new int[interfaces_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    for(int i=0; i < interfaces_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
      interfaces[i] = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   * Read information about the methods of the class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
  private final void readMethods() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    int methods_count;
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    methods_count = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    methods       = new Method[methods_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    for(int i=0; i < methods_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
      methods[i] = new Method(file, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * Read major and minor version of compiler which created the file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * @throws  IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   * @throws  ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
  private final void readVersion() throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
    minor = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    major = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
}