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