View Javadoc
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   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and represents a constant
28   * value, i.e., a default value for initializing a class field.
29   * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
30   *
31   * @see     Attribute
32   */
33  public final class ConstantValue extends Attribute {
34  
35      private int constantValueIndex;
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 ConstantValuenstantValue.html#ConstantValue">ConstantValue(final ConstantValue c) {
43          this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
44      }
45  
46  
47      /**
48       * Construct object from input stream.
49       * @param name_index Name index in constant pool
50       * @param length Content length in bytes
51       * @param input Input stream
52       * @param constant_pool Array of constants
53       * @throws IOException
54       */
55      ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
56              throws IOException {
57          this(name_index, length, input.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 constantValueIndex Index in constant pool
65       * @param constant_pool Array of constants
66       */
67      public ConstantValue(final int name_index, final int length, final int constantValueIndex,
68              final ConstantPool constant_pool) {
69          super(Const.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
70          this.constantValueIndex = constantValueIndex;
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      @Override
82      public void accept( final Visitor v ) {
83          v.visitConstantValue(this);
84      }
85  
86  
87      /**
88       * Dump constant value attribute to file stream on binary format.
89       *
90       * @param file Output file stream
91       * @throws IOException
92       */
93      @Override
94      public void dump( final DataOutputStream file ) throws IOException {
95          super.dump(file);
96          file.writeShort(constantValueIndex);
97      }
98  
99  
100     /**
101      * @return Index in constant pool of constant value.
102      */
103     public int getConstantValueIndex() {
104         return constantValueIndex;
105     }
106 
107 
108     /**
109      * @param constantValueIndex the index info the constant pool of this constant value
110      */
111     public void setConstantValueIndex( final int constantValueIndex ) {
112         this.constantValueIndex = constantValueIndex;
113     }
114 
115 
116     /**
117      * @return String representation of constant value.
118      */
119     @Override
120     public String toString() {
121         Constant c = super.getConstantPool().getConstant(constantValueIndex);
122         String buf;
123         int i;
124         // Print constant to string depending on its type
125         switch (c.getTag()) {
126             case Const.CONSTANT_Long:
127                 buf = String.valueOf(((ConstantLong) c).getBytes());
128                 break;
129             case Const.CONSTANT_Float:
130                 buf = String.valueOf(((ConstantFloat) c).getBytes());
131                 break;
132             case Const.CONSTANT_Double:
133                 buf = String.valueOf(((ConstantDouble) c).getBytes());
134                 break;
135             case Const.CONSTANT_Integer:
136                 buf = String.valueOf(((ConstantInteger) c).getBytes());
137                 break;
138             case Const.CONSTANT_String:
139                 i = ((ConstantString) c).getStringIndex();
140                 c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
141                 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
142                 break;
143             default:
144                 throw new IllegalStateException("Type of ConstValue invalid: " + c);
145         }
146         return buf;
147     }
148 
149 
150     /**
151      * @return deep copy of this attribute
152      */
153     @Override
154     public Attribute copy( final ConstantPool _constant_pool ) {
155         final ConstantValue/../../../org/apache/bcel/classfile/ConstantValue.html#ConstantValue">ConstantValue c = (ConstantValue) clone();
156         c.setConstantPool(_constant_pool);
157         return c;
158     }
159 }