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 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.Const;
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 * @see     Attribute
032 */
033public final class ConstantValue extends Attribute {
034
035    private int constantValueIndex;
036
037
038    /**
039     * Initialize from another object. Note that both objects use the same
040     * references (shallow copy). Use clone() for a physical copy.
041     */
042    public ConstantValue(final ConstantValue c) {
043        this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
044    }
045
046
047    /**
048     * Construct object from input stream.
049     * @param name_index Name index in constant pool
050     * @param length Content length in bytes
051     * @param input Input stream
052     * @param constant_pool Array of constants
053     * @throws IOException
054     */
055    ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
056            throws IOException {
057        this(name_index, length, input.readUnsignedShort(), constant_pool);
058    }
059
060
061    /**
062     * @param name_index Name index in constant pool
063     * @param length Content length in bytes
064     * @param constantValueIndex Index in constant pool
065     * @param constant_pool Array of constants
066     */
067    public ConstantValue(final int name_index, final int length, final int constantValueIndex,
068            final ConstantPool constant_pool) {
069        super(Const.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
070        this.constantValueIndex = constantValueIndex;
071    }
072
073
074    /**
075     * Called by objects that are traversing the nodes of the tree implicitely
076     * defined by the contents of a Java class. I.e., the hierarchy of methods,
077     * fields, attributes, etc. spawns a tree of objects.
078     *
079     * @param v Visitor object
080     */
081    @Override
082    public void accept( final Visitor v ) {
083        v.visitConstantValue(this);
084    }
085
086
087    /**
088     * Dump constant value attribute to file stream on binary format.
089     *
090     * @param file Output file stream
091     * @throws IOException
092     */
093    @Override
094    public void dump( final DataOutputStream file ) throws IOException {
095        super.dump(file);
096        file.writeShort(constantValueIndex);
097    }
098
099
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 c = (ConstantValue) clone();
156        c.setConstantPool(_constant_pool);
157        return c;
158    }
159}