001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.classfile;
019
020 import java.io.DataInput;
021 import java.io.DataOutputStream;
022 import java.io.IOException;
023
024 import org.apache.bcel.Constants;
025
026 /**
027 * This class is derived from <em>Attribute</em> and represents a constant
028 * value, i.e., a default value for initializing a class field.
029 * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
030 *
031 * @version $Id: ConstantValue.java 1152072 2011-07-29 01:54:05Z dbrosius $
032 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033 * @see Attribute
034 */
035 public final class ConstantValue extends Attribute {
036
037 private static final long serialVersionUID = -5668999920978520157L;
038 private int constantvalue_index;
039
040
041 /**
042 * Initialize from another object. Note that both objects use the same
043 * references (shallow copy). Use clone() for a physical copy.
044 */
045 public ConstantValue(ConstantValue c) {
046 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
047 }
048
049
050 /**
051 * Construct object from file stream.
052 * @param name_index Name index in constant pool
053 * @param length Content length in bytes
054 * @param file Input stream
055 * @param constant_pool Array of constants
056 * @throws IOException
057 */
058 ConstantValue(int name_index, int length, DataInput file, ConstantPool constant_pool)
059 throws IOException {
060 this(name_index, length, file.readUnsignedShort(), constant_pool);
061 }
062
063
064 /**
065 * @param name_index Name index in constant pool
066 * @param length Content length in bytes
067 * @param constantvalue_index Index in constant pool
068 * @param constant_pool Array of constants
069 */
070 public ConstantValue(int name_index, int length, int constantvalue_index,
071 ConstantPool constant_pool) {
072 super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
073 this.constantvalue_index = constantvalue_index;
074 }
075
076
077 /**
078 * Called by objects that are traversing the nodes of the tree implicitely
079 * defined by the contents of a Java class. I.e., the hierarchy of methods,
080 * fields, attributes, etc. spawns a tree of objects.
081 *
082 * @param v Visitor object
083 */
084 @Override
085 public void accept( Visitor v ) {
086 v.visitConstantValue(this);
087 }
088
089
090 /**
091 * Dump constant value attribute to file stream on binary format.
092 *
093 * @param file Output file stream
094 * @throws IOException
095 */
096 @Override
097 public final void dump( DataOutputStream file ) throws IOException {
098 super.dump(file);
099 file.writeShort(constantvalue_index);
100 }
101
102
103 /**
104 * @return Index in constant pool of constant value.
105 */
106 public final int getConstantValueIndex() {
107 return constantvalue_index;
108 }
109
110
111 /**
112 * @param constantvalue_index the index info the constant pool of this constant value
113 */
114 public final void setConstantValueIndex( int constantvalue_index ) {
115 this.constantvalue_index = constantvalue_index;
116 }
117
118
119 /**
120 * @return String representation of constant value.
121 */
122 @Override
123 public final String toString() {
124 Constant c = constant_pool.getConstant(constantvalue_index);
125 String buf;
126 int i;
127 // Print constant to string depending on its type
128 switch (c.getTag()) {
129 case Constants.CONSTANT_Long:
130 buf = String.valueOf(((ConstantLong) c).getBytes());
131 break;
132 case Constants.CONSTANT_Float:
133 buf = String.valueOf(((ConstantFloat) c).getBytes());
134 break;
135 case Constants.CONSTANT_Double:
136 buf = String.valueOf(((ConstantDouble) c).getBytes());
137 break;
138 case Constants.CONSTANT_Integer:
139 buf = String.valueOf(((ConstantInteger) c).getBytes());
140 break;
141 case Constants.CONSTANT_String:
142 i = ((ConstantString) c).getStringIndex();
143 c = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
144 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
145 break;
146 default:
147 throw new IllegalStateException("Type of ConstValue invalid: " + c);
148 }
149 return buf;
150 }
151
152
153 /**
154 * @return deep copy of this attribute
155 */
156 @Override
157 public Attribute copy( ConstantPool _constant_pool ) {
158 ConstantValue c = (ConstantValue) clone();
159 c.constant_pool = _constant_pool;
160 return c;
161 }
162 }