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 * @version $Id$
36 * @see Attribute
37 */
38 public final class ConstantValue extends Attribute {
39
40 private int constantvalue_index;
41
42
43 /**
44 * Initialize from another object. Note that both objects use the same
45 * references (shallow copy). Use clone() for a physical copy.
46 */
47 public ConstantValue(final ConstantValue c) {
48 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
49 }
50
51
52 /**
53 * Construct object from input stream.
54 * @param name_index Name index in constant pool
55 * @param length Content length in bytes
79 /**
80 * Called by objects that are traversing the nodes of the tree implicitely
81 * defined by the contents of a Java class. I.e., the hierarchy of methods,
82 * fields, attributes, etc. spawns a tree of objects.
83 *
84 * @param v Visitor object
85 */
86 @Override
87 public void accept( final Visitor v ) {
88 v.visitConstantValue(this);
89 }
90
91
92 /**
93 * Dump constant value attribute to file stream on binary format.
94 *
95 * @param file Output file stream
96 * @throws IOException
97 */
98 @Override
99 public final void dump( final DataOutputStream file ) throws IOException {
100 super.dump(file);
101 file.writeShort(constantvalue_index);
102 }
103
104
105 /**
106 * @return Index in constant pool of constant value.
107 */
108 public final int getConstantValueIndex() {
109 return constantvalue_index;
110 }
111
112
113 /**
114 * @param constantvalue_index the index info the constant pool of this constant value
115 */
116 public final void setConstantValueIndex( final int constantvalue_index ) {
117 this.constantvalue_index = constantvalue_index;
118 }
119
120
121 /**
122 * @return String representation of constant value.
123 */
124 @Override
125 public final String toString() {
126 Constant c = super.getConstantPool().getConstant(constantvalue_index);
127 String buf;
128 int i;
129 // Print constant to string depending on its type
130 switch (c.getTag()) {
131 case Const.CONSTANT_Long:
132 buf = String.valueOf(((ConstantLong) c).getBytes());
133 break;
134 case Const.CONSTANT_Float:
135 buf = String.valueOf(((ConstantFloat) c).getBytes());
136 break;
137 case Const.CONSTANT_Double:
138 buf = String.valueOf(((ConstantDouble) c).getBytes());
139 break;
140 case Const.CONSTANT_Integer:
141 buf = String.valueOf(((ConstantInteger) c).getBytes());
142 break;
143 case Const.CONSTANT_String:
144 i = ((ConstantString) c).getStringIndex();
145 c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
|
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 */
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
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
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
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 */
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 */
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
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);
|