    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 the abstract {@link Constant}
    028 * and represents a reference to an int object.
    029 *
    030 * @version $Id$
    031 * @see     Constant
    032 */
    033public final class ConstantInteger extends Constant implements ConstantObject {
    034
    035    private int bytes;
    036
    037
    038    /**
    039     * @param bytes Data
    040     */
    041    public ConstantInteger(final int bytes) {
    042        super(Const.CONSTANT_Integer);
    043        this.bytes = bytes;
    044    }
    045
    046
    047    /**
    048     * Initialize from another object.
    049     */
    050    public ConstantInteger(final ConstantInteger c) {
    051        this(c.getBytes());
    052    }
    053
    054
    055    /**
    056     * Initialize instance from file data.
    057     *
    058     * @param file Input stream
    059     * @throws IOException
    060     */
    061    ConstantInteger(final DataInput file) throws IOException {
    062        this(file.readInt());
    063    }
    064
    065
    066    /**
    067     * Called by objects that are traversing the nodes of the tree implicitely
    068     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    069     * fields, attributes, etc. spawns a tree of objects.
    070     *
    071     * @param v Visitor object
    072     */
    073    @Override
    074    public void accept( final Visitor v ) {
    075        v.visitConstantInteger(this);
    076    }
    077
    078
    079    /**
    080     * Dump constant integer to file stream in binary format.
    081     *
    082     * @param file Output file stream
    083     * @throws IOException
    084     */
    085    @Override
    086    public final void dump( final DataOutputStream file ) throws IOException {
    087        file.writeByte(super.getTag());
    088        file.writeInt(bytes);
    089    }
    090
    091
    092    /**
    093     * @return data, i.e., 4 bytes.
    094     */
    095    public final int getBytes() {
    096        return bytes;
    097    }
    098
    099
    100    /**
    101     * @param bytes the raw bytes that represent this integer
    102     */
    103    public final void setBytes( final int bytes ) {
    104        this.bytes = bytes;
    105    }
    106
    107
    108    /**
    109     * @return String representation.
    110     */
    111    @Override
    112    public final String toString() {
    113        return super.toString() + "(bytes = " + bytes + ")";
    114    }
    115
    116
    117    /** @return Integer object
    118     */
    119    @Override
    120    public Object getConstantValue( final ConstantPool cp ) {
    121        return Integer.valueOf(bytes);
    122    }
    123}