jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/Code.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
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * This class represents a chunk of Java byte code contained in a
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * method. It is instantiated by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * <em>Attribute.readAttribute()</em> method. A <em>Code</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * attribute contains informations about operand stack, local
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * variables, byte code and the exceptions handled within this
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * method.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * This attribute has attributes itself, namely <em>LineNumberTable</em> which
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * is used for debugging purposes and <em>LocalVariableTable</em> which
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * contains information about the local variables.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @see     Attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @see     CodeException
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * @see     LineNumberTable
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * @see LocalVariableTable
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
public final class Code extends Attribute {
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  private int             max_stack;   // Maximum size of stack used by this method
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  private int             max_locals;  // Number of local variables
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  private int             code_length; // Length of code in bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  private byte[]          code;        // Actual byte code
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  private int             exception_table_length;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  private CodeException[] exception_table;  // Table of handled exceptions
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  private int             attributes_count; // Attributes of code: LineNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
  private Attribute[]     attributes;       // or LocalVariable
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * Initialize from another object. Note that both objects use the same
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   * references (shallow copy). Use copy() for a physical copy.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  public Code(Code c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(),
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
         c.getCode(), c.getExceptionTable(), c.getAttributes(),
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
         c.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * @param name_index Index pointing to the name <em>Code</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * @param length Content length in bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * @param file Input stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * @param constant_pool Array of constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  Code(int name_index, int length, DataInputStream file,
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
       ConstantPool constant_pool) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    // Initialize with some default values which will be overwritten later
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    this(name_index, length,
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
         file.readUnsignedShort(), file.readUnsignedShort(),
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
         (byte[])null, (CodeException[])null, (Attribute[])null,
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
         constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    code_length = file.readInt();
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    code = new byte[code_length]; // Read byte code
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    file.readFully(code);
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /* Read exception table that contains all regions where an exception
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * handler is active, i.e., a try { ... } catch() block.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    exception_table_length = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    exception_table        = new CodeException[exception_table_length];
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    for(int i=0; i < exception_table_length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
      exception_table[i] = new CodeException(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    /* Read all attributes, currently `LineNumberTable' and
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     * `LocalVariableTable'
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    attributes_count = file.readUnsignedShort();
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    attributes = new Attribute[attributes_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
      attributes[i] = Attribute.readAttribute(file, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    /* Adjust length, because of setAttributes in this(), s.b.  length
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     * is incorrect, because it didn't take the internal attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     * into account yet! Very subtle bug, fixed in 3.1.1.
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    this.length = length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * @param name_index Index pointing to the name <em>Code</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * @param length Content length in bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * @param max_stack Maximum size of stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * @param max_locals Number of local variables
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * @param code Actual byte code
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * @param exception_table Table of handled exceptions
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * @param attributes Attributes of code: LineNumber or LocalVariable
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * @param constant_pool Array of constants
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  public Code(int name_index, int length,
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
              int max_stack,  int max_locals,
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
              byte[]          code,
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
              CodeException[] exception_table,
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
              Attribute[]     attributes,
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
              ConstantPool    constant_pool)
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    super(Constants.ATTR_CODE, name_index, length, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    this.max_stack         = max_stack;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    this.max_locals        = max_locals;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    setCode(code);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    setExceptionTable(exception_table);
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    setAttributes(attributes); // Overwrites length!
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
   * Called by objects that are traversing the nodes of the tree implicitely
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
   * defined by the contents of a Java class. I.e., the hierarchy of methods,
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
   * fields, attributes, etc. spawns a tree of objects.
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
   * @param v Visitor object
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
  public void accept(Visitor v) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    v.visitCode(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * Dump code attribute to file stream in binary format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   * @param file Output file stream
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
  public final void dump(DataOutputStream file) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    super.dump(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    file.writeShort(max_stack);
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    file.writeShort(max_locals);
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    file.writeInt(code_length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    file.write(code, 0, code_length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    file.writeShort(exception_table_length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    for(int i=0; i < exception_table_length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
      exception_table[i].dump(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    file.writeShort(attributes_count);
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
      attributes[i].dump(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   * @return Collection of code attributes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   * @see Attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
  public final Attribute[] getAttributes()         { return attributes; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   * @return LineNumberTable of Code, if it has one
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
  public LineNumberTable getLineNumberTable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
      if(attributes[i] instanceof LineNumberTable)
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        return (LineNumberTable)attributes[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   * @return LocalVariableTable of Code, if it has one
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
  public LocalVariableTable getLocalVariableTable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
      if(attributes[i] instanceof LocalVariableTable)
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
        return (LocalVariableTable)attributes[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
   * @return Actual byte code of the method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
  public final byte[] getCode()      { return code; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   * @return Table of handled exceptions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   * @see CodeException
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
  public final CodeException[] getExceptionTable() { return exception_table; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   * @return Number of local variables.
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
  public final int  getMaxLocals() { return max_locals; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   * @return Maximum size of stack used by this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  public final int  getMaxStack()  { return max_stack; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
   * @return the internal length of this code attribute (minus the first 6 bytes)
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   * and excluding all its attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
  private final int getInternalLength() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    return 2 /*max_stack*/ + 2 /*max_locals*/ + 4 /*code length*/
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
      + code_length /*byte-code*/
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
      + 2 /*exception-table length*/
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
      + 8 * exception_table_length /* exception table */
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
      + 2 /* attributes count */;
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   * @return the full size of this code attribute, minus its first 6 bytes,
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   * including the size of all its contained attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  private final int calculateLength() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    int len = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
      len += attributes[i].length + 6 /*attribute header size*/;
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    return len + getInternalLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
   * @param attributes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
  public final void setAttributes(Attribute[] attributes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    this.attributes  = attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
    attributes_count = (attributes == null)? 0 : attributes.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    length = calculateLength(); // Adjust length
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   * @param code byte code
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
  public final void setCode(byte[] code) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    this.code   = code;
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
    code_length = (code == null)? 0 : code.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   * @param exception_table exception table
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
  public final void setExceptionTable(CodeException[] exception_table) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    this.exception_table   = exception_table;
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    exception_table_length = (exception_table == null)? 0 :
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
      exception_table.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   * @param max_locals maximum number of local variables
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  public final void setMaxLocals(int max_locals) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    this.max_locals = max_locals;
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   * @param max_stack maximum stack size
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
  public final void setMaxStack(int max_stack) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
    this.max_stack = max_stack;
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   * @return String representation of code chunk.
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
  public final String toString(boolean verbose) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    StringBuffer buf;
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    buf = new StringBuffer("Code(max_stack = " + max_stack +
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
                           ", max_locals = " + max_locals +
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                           ", code_length = " + code_length + ")\n" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                           Utility.codeToString(code, constant_pool, 0, -1, verbose));
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
    if(exception_table_length > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
      buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
      for(int i=0; i < exception_table_length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
        buf.append(exception_table[i].toString(constant_pool, verbose) + "\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
    if(attributes_count > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
      buf.append("\nAttribute(s) = \n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
      for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
        buf.append(attributes[i].toString() + "\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
   * @return String representation of code chunk.
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
  public final String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    return toString(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
   * @return deep copy of this attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
  public Attribute copy(ConstantPool constant_pool) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    Code c = (Code)clone();
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    c.code          = (byte[])code.clone();
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    c.constant_pool = constant_pool;
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
    c.exception_table = new CodeException[exception_table_length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    for(int i=0; i < exception_table_length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
      c.exception_table[i] = exception_table[i].copy();
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
    c.attributes = new Attribute[attributes_count];
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
      c.attributes[i] = attributes[i].copy(constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
    return c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
}