View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.classfile;
    18
    19  import java.io.DataInputStream;
    20  import java.io.DataOutputStream;
    21  import java.io.IOException;
    22  import org.apache.bcel.Constants;
    23
    24  /***
    25   * This class is derived from <em>Attribute</em> and represents a constant
    26   * value, i.e., a default value for initializing a class field.
    27   * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
    28   *
    29   * @version $Id: ConstantValue.java 386056 2006-03-15 11:31:56Z tcurdt $
    30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    31   * @see     Attribute
    32   */
    33  public final class ConstantValue extends Attribute {
    34
    35      private int constantvalue_index;
    36
    37
    38      /***
    39       * Initialize from another object. Note that both objects use the same
    40       * references (shallow copy). Use clone() for a physical copy.
    41       */
    42      public ConstantValue(ConstantValue c) {
    43          this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
    44      }
    45
    46
    47      /***
    48       * Construct object from file stream.
    49       * @param name_index Name index in constant pool
    50       * @param length Content length in bytes
    51       * @param file Input stream
    52       * @param constant_pool Array of constants
    53       * @throws IOException
    54       */
    55      ConstantValue(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
    56              throws IOException {
    57          this(name_index, length, file.readUnsignedShort(), constant_pool);
    58      }
    59
    60
    61      /***
    62       * @param name_index Name index in constant pool
    63       * @param length Content length in bytes
    64       * @param constantvalue_index Index in constant pool
    65       * @param constant_pool Array of constants
    66       */
    67      public ConstantValue(int name_index, int length, int constantvalue_index,
    68              ConstantPool constant_pool) {
    69          super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
    70          this.constantvalue_index = constantvalue_index;
    71      }
    72
    73
    74      /***
    75       * Called by objects that are traversing the nodes of the tree implicitely
    76       * defined by the contents of a Java class. I.e., the hierarchy of methods,
    77       * fields, attributes, etc. spawns a tree of objects.
    78       *
    79       * @param v Visitor object
    80       */
    81      public void accept( Visitor v ) {
    82          v.visitConstantValue(this);
    83      }
    84
    85
    86      /***
    87       * Dump constant value attribute to file stream on binary format.
    88       *
    89       * @param file Output file stream
    90       * @throws IOException
    91       */
    92      public final void dump( DataOutputStream file ) throws IOException {
    93          super.dump(file);
    94          file.writeShort(constantvalue_index);
    95      }
    96
    97
    98      /***
    99       * @return Index in constant pool of constant value.
    100      */
    101     public final int getConstantValueIndex() {
    102         return constantvalue_index;
    103     }
    104
    105
    106     /***
    107      * @param constantvalue_index the index info the constant pool of this constant value
    108      */
    109     public final void setConstantValueIndex( int constantvalue_index ) {
    110         this.constantvalue_index = constantvalue_index;
    111     }
    112
    113
    114     /***
    115      * @return String representation of constant value.
    116      */
    117     public final String toString() {
    118         Constant c = constant_pool.getConstant(constantvalue_index);
    119         String buf;
    120         int i;
    121         // Print constant to string depending on its type
    122         switch (c.getTag()) {
    123             case Constants.CONSTANT_Long:
    124                 buf = "" + ((ConstantLong) c).getBytes();
    125                 break;
    126             case Constants.CONSTANT_Float:
    127                 buf = "" + ((ConstantFloat) c).getBytes();
    128                 break;
    129             case Constants.CONSTANT_Double:
    130                 buf = "" + ((ConstantDouble) c).getBytes();
    131                 break;
    132             case Constants.CONSTANT_Integer:
    133                 buf = "" + ((ConstantInteger) c).getBytes();
    134                 break;
    135             case Constants.CONSTANT_String:
    136                 i = ((ConstantString) c).getStringIndex();
    137                 c = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
    138                 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
    139                 break;
    140             default:
    141                 throw new IllegalStateException("Type of ConstValue invalid: " + c);
    142         }
    143         return buf;
    144     }
    145
    146
    147     /***
    148      * @return deep copy of this attribute
    149      */
    150     public Attribute copy( ConstantPool _constant_pool ) {
    151         ConstantValue c = (ConstantValue) clone();
    152         c.constant_pool = _constant_pool;
    153         return c;
    154     }
    155 }