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   * @version $Id: ConstantValue.html 1018313 2017-09-18 09:03:04Z britter $
32   * @see     Attribute
33   */
34  public final class ConstantValue extends Attribute {
35  
36      private int constantvalue_index;
37  
38  
39      /**
40       * Initialize from another object. Note that both objects use the same
41       * references (shallow copy). Use clone() for a physical copy.
42       */
43      public ConstantValue(final ConstantValue c) {
44          this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
45      }
46  
47  
48      /**
49       * Construct object from input stream.
50       * @param name_index Name index in constant pool
51       * @param length Content length in bytes
52       * @param input Input stream
53       * @param constant_pool Array of constants
54       * @throws IOException
55       */
56      ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
57              throws IOException {
58          this(name_index, length, input.readUnsignedShort(), constant_pool);
59      }
60  
61  
62      /**
63       * @param name_index Name index in constant pool
64       * @param length Content length in bytes
65       * @param constantvalue_index Index in constant pool
66       * @param constant_pool Array of constants
67       */
68      public ConstantValue(final int name_index, final int length, final int constantvalue_index,
69              final ConstantPool constant_pool) {
70          super(Const.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
71          this.constantvalue_index = constantvalue_index;
72      }
73  
74  
75      /**
76       * Called by objects that are traversing the nodes of the tree implicitely
77       * defined by the contents of a Java class. I.e., the hierarchy of methods,
78       * fields, attributes, etc. spawns a tree of objects.
79       *
80       * @param v Visitor object
81       */
82      @Override
83      public void accept( final Visitor v ) {
84          v.visitConstantValue(this);
85      }
86  
87  
88      /**
89       * Dump constant value attribute to file stream on binary format.
90       *
91       * @param file Output file stream
92       * @throws IOException
93       */
94      @Override
95      public final void dump( final DataOutputStream file ) throws IOException {
96          super.dump(file);
97          file.writeShort(constantvalue_index);
98      }
99  
100 
101     /**
102      * @return Index in constant pool of constant value.
103      */
104     public final int getConstantValueIndex() {
105         return constantvalue_index;
106     }
107 
108 
109     /**
110      * @param constantvalue_index the index info the constant pool of this constant value
111      */
112     public final void setConstantValueIndex( final int constantvalue_index ) {
113         this.constantvalue_index = constantvalue_index;
114     }
115 
116 
117     /**
118      * @return String representation of constant value.
119      */
120     @Override
121     public final String toString() {
122         Constant c = super.getConstantPool().getConstant(constantvalue_index);
123         String buf;
124         int i;
125         // Print constant to string depending on its type
126         switch (c.getTag()) {
127             case Const.CONSTANT_Long:
128                 buf = String.valueOf(((ConstantLong) c).getBytes());
129                 break;
130             case Const.CONSTANT_Float:
131                 buf = String.valueOf(((ConstantFloat) c).getBytes());
132                 break;
133             case Const.CONSTANT_Double:
134                 buf = String.valueOf(((ConstantDouble) c).getBytes());
135                 break;
136             case Const.CONSTANT_Integer:
137                 buf = String.valueOf(((ConstantInteger) c).getBytes());
138                 break;
139             case Const.CONSTANT_String:
140                 i = ((ConstantString) c).getStringIndex();
141                 c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
142                 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
143                 break;
144             default:
145                 throw new IllegalStateException("Type of ConstValue invalid: " + c);
146         }
147         return buf;
148     }
149 
150 
151     /**
152      * @return deep copy of this attribute
153      */
154     @Override
155     public Attribute copy( final ConstantPool _constant_pool ) {
156         final ConstantValue c = (ConstantValue) clone();
157         c.setConstantPool(_constant_pool);
158         return c;
159     }
160 }