ConstantValue.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * This class is derived from <em>Attribute</em> and represents a constant value, i.e., a default value for initializing
  25.  * a class field. This class is instantiated by the <em>Attribute.readAttribute()</em> method.
  26.  *
  27.  * <pre>
  28.  * ConstantValue_attribute {
  29.  *   u2 attribute_name_index;
  30.  *   u4 attribute_length;
  31.  *   u2 constantvalue_index;
  32.  * }
  33.  * </pre>
  34.  * @see Attribute
  35.  */
  36. public final class ConstantValue extends Attribute {

  37.     private int constantValueIndex;

  38.     /**
  39.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
  40.      * physical copy.
  41.      *
  42.      * @param c Source to copy.
  43.      */
  44.     public ConstantValue(final ConstantValue c) {
  45.         this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
  46.     }

  47.     /**
  48.      * Constructs object from input stream.
  49.      *
  50.      * @param nameIndex Name index in constant pool
  51.      * @param length Content length in bytes
  52.      * @param input Input stream
  53.      * @param constantPool Array of constants
  54.      * @throws IOException if an I/O error occurs.
  55.      */
  56.     ConstantValue(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  57.         this(nameIndex, length, input.readUnsignedShort(), constantPool);
  58.     }

  59.     /**
  60.      * @param nameIndex Name index in constant pool
  61.      * @param length Content length in bytes
  62.      * @param constantValueIndex Index in constant pool
  63.      * @param constantPool Array of constants
  64.      */
  65.     public ConstantValue(final int nameIndex, final int length, final int constantValueIndex, final ConstantPool constantPool) {
  66.         super(Const.ATTR_CONSTANT_VALUE, nameIndex, Args.require(length, 2, "ConstantValue attribute length"), constantPool);
  67.         this.constantValueIndex = constantValueIndex;
  68.     }

  69.     /**
  70.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  71.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  72.      *
  73.      * @param v Visitor object
  74.      */
  75.     @Override
  76.     public void accept(final Visitor v) {
  77.         v.visitConstantValue(this);
  78.     }

  79.     /**
  80.      * @return deep copy of this attribute
  81.      */
  82.     @Override
  83.     public Attribute copy(final ConstantPool constantPool) {
  84.         final ConstantValue c = (ConstantValue) clone();
  85.         c.setConstantPool(constantPool);
  86.         return c;
  87.     }

  88.     /**
  89.      * Dump constant value attribute to file stream on binary format.
  90.      *
  91.      * @param file Output file stream
  92.      * @throws IOException if an I/O error occurs.
  93.      */
  94.     @Override
  95.     public void dump(final DataOutputStream file) throws IOException {
  96.         super.dump(file);
  97.         file.writeShort(constantValueIndex);
  98.     }

  99.     /**
  100.      * @return Index in constant pool of constant value.
  101.      */
  102.     public int getConstantValueIndex() {
  103.         return constantValueIndex;
  104.     }

  105.     /**
  106.      * @param constantValueIndex the index info the constant pool of this constant value
  107.      */
  108.     public void setConstantValueIndex(final int constantValueIndex) {
  109.         this.constantValueIndex = constantValueIndex;
  110.     }

  111.     /**
  112.      * @return String representation of constant value.
  113.      */
  114.     @Override
  115.     public String toString() {
  116.         Constant c = super.getConstantPool().getConstant(constantValueIndex);
  117.         String buf;
  118.         int i;
  119.         // Print constant to string depending on its type
  120.         switch (c.getTag()) {
  121.         case Const.CONSTANT_Long:
  122.             buf = String.valueOf(((ConstantLong) c).getBytes());
  123.             break;
  124.         case Const.CONSTANT_Float:
  125.             buf = String.valueOf(((ConstantFloat) c).getBytes());
  126.             break;
  127.         case Const.CONSTANT_Double:
  128.             buf = String.valueOf(((ConstantDouble) c).getBytes());
  129.             break;
  130.         case Const.CONSTANT_Integer:
  131.             buf = String.valueOf(((ConstantInteger) c).getBytes());
  132.             break;
  133.         case Const.CONSTANT_String:
  134.             i = ((ConstantString) c).getStringIndex();
  135.             c = super.getConstantPool().getConstantUtf8(i);
  136.             buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
  137.             break;
  138.         default:
  139.             throw new IllegalStateException("Type of ConstValue invalid: " + c);
  140.         }
  141.         return buf;
  142.     }
  143. }