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 */
017package org.apache.bcel.classfile;
018
019import java.io.DataInput;
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.Const;
024import org.apache.bcel.util.Args;
025
026/**
027 * This class is derived from <em>Attribute</em> and represents a constant value, i.e., a default value for initializing
028 * a class field. This class is instantiated by the <em>Attribute.readAttribute()</em> method.
029 *
030 * <pre>
031 * ConstantValue_attribute {
032 *   u2 attribute_name_index;
033 *   u4 attribute_length;
034 *   u2 constantvalue_index;
035 * }
036 * </pre>
037 * @see Attribute
038 */
039public final class ConstantValue extends Attribute {
040
041    private int constantValueIndex;
042
043    /**
044     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
045     * physical copy.
046     *
047     * @param c Source to copy.
048     */
049    public ConstantValue(final ConstantValue c) {
050        this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
051    }
052
053    /**
054     * Constructs object from input stream.
055     *
056     * @param nameIndex Name index in constant pool
057     * @param length Content length in bytes
058     * @param input Input stream
059     * @param constantPool Array of constants
060     * @throws IOException if an I/O error occurs.
061     */
062    ConstantValue(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
063        this(nameIndex, length, input.readUnsignedShort(), constantPool);
064    }
065
066    /**
067     * @param nameIndex Name index in constant pool
068     * @param length Content length in bytes
069     * @param constantValueIndex Index in constant pool
070     * @param constantPool Array of constants
071     */
072    public ConstantValue(final int nameIndex, final int length, final int constantValueIndex, final ConstantPool constantPool) {
073        super(Const.ATTR_CONSTANT_VALUE, nameIndex, Args.require(length, 2, "ConstantValue attribute length"), constantPool);
074        this.constantValueIndex = constantValueIndex;
075    }
076
077    /**
078     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
079     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
080     *
081     * @param v Visitor object
082     */
083    @Override
084    public void accept(final Visitor v) {
085        v.visitConstantValue(this);
086    }
087
088    /**
089     * @return deep copy of this attribute
090     */
091    @Override
092    public Attribute copy(final ConstantPool constantPool) {
093        final ConstantValue c = (ConstantValue) clone();
094        c.setConstantPool(constantPool);
095        return c;
096    }
097
098    /**
099     * Dump constant value attribute to file stream on binary format.
100     *
101     * @param file Output file stream
102     * @throws IOException if an I/O error occurs.
103     */
104    @Override
105    public void dump(final DataOutputStream file) throws IOException {
106        super.dump(file);
107        file.writeShort(constantValueIndex);
108    }
109
110    /**
111     * @return Index in constant pool of constant value.
112     */
113    public int getConstantValueIndex() {
114        return constantValueIndex;
115    }
116
117    /**
118     * @param constantValueIndex the index info the constant pool of this constant value
119     */
120    public void setConstantValueIndex(final int constantValueIndex) {
121        this.constantValueIndex = constantValueIndex;
122    }
123
124    /**
125     * @return String representation of constant value.
126     */
127    @Override
128    public String toString() {
129        Constant c = super.getConstantPool().getConstant(constantValueIndex);
130        String buf;
131        int i;
132        // Print constant to string depending on its type
133        switch (c.getTag()) {
134        case Const.CONSTANT_Long:
135            buf = String.valueOf(((ConstantLong) c).getBytes());
136            break;
137        case Const.CONSTANT_Float:
138            buf = String.valueOf(((ConstantFloat) c).getBytes());
139            break;
140        case Const.CONSTANT_Double:
141            buf = String.valueOf(((ConstantDouble) c).getBytes());
142            break;
143        case Const.CONSTANT_Integer:
144            buf = String.valueOf(((ConstantInteger) c).getBytes());
145            break;
146        case Const.CONSTANT_String:
147            i = ((ConstantString) c).getStringIndex();
148            c = super.getConstantPool().getConstantUtf8(i);
149            buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
150            break;
151        default:
152            throw new IllegalStateException("Type of ConstValue invalid: " + c);
153        }
154        return buf;
155    }
156}