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