    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.bcel.classfile;
    020
    021import java.io.DataInput;
    022import java.io.DataOutputStream;
    023import java.io.IOException;
    024
    025import org.apache.bcel.Const;
    026
    027/**
    028 * This class is derived from the abstract {@link Constant} and represents a reference to a float object.
    029 *
    030 * @see Constant
    031 */
    032public final class ConstantFloat extends Constant implements ConstantObject {
    033
    034    private float bytes;
    035
    036    /**
    037     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
    038     * physical copy.
    039     *
    040     * @param c Source to copy.
    041     */
    042    public ConstantFloat(final ConstantFloat c) {
    043        this(c.getBytes());
    044    }
    045
    046    /**
    047     * Initialize instance from file data.
    048     *
    049     * @param file Input stream.
    050     * @throws IOException if an I/O error occurs.
    051     */
    052    ConstantFloat(final DataInput file) throws IOException {
    053        this(file.readFloat());
    054    }
    055
    056    /**
    057     * Constructs a ConstantFloat.
    058     *
    059     * @param bytes Data.
    060     */
    061    public ConstantFloat(final float bytes) {
    062        super(Const.CONSTANT_Float);
    063        this.bytes = bytes;
    064    }
    065
    066    /**
    067     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    068     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    069     *
    070     * @param v Visitor object.
    071     */
    072    @Override
    073    public void accept(final Visitor v) {
    074        v.visitConstantFloat(this);
    075    }
    076
    077    /**
    078     * Dumps constant float to file stream in binary format.
    079     *
    080     * @param file Output file stream.
    081     * @throws IOException if an I/O error occurs.
    082     */
    083    @Override
    084    public void dump(final DataOutputStream file) throws IOException {
    085        file.writeByte(super.getTag());
    086        file.writeFloat(bytes);
    087    }
    088
    089    /**
    090     * Gets the data.
    091     *
    092     * @return data, that is, 4 bytes.
    093     */
    094    public float getBytes() {
    095        return bytes;
    096    }
    097
    098    /**
    099     * Gets the Float object.
    100     *
    101     * @param cp the constant pool (not used).
    102     * @return Float object.
    103     */
    104    @Override
    105    public Object getConstantValue(final ConstantPool cp) {
    106        return Float.valueOf(bytes);
    107    }
    108
    109    /**
    110     * Sets the raw bytes that represent the float value.
    111     *
    112     * @param bytes the raw bytes that represent the float value.
    113     */
    114    public void setBytes(final float bytes) {
    115        this.bytes = bytes;
    116    }
    117
    118    /**
    119     * @return String representation.
    120     */
    121    @Override
    122    public String toString() {
    123        return super.toString() + "(bytes = " + bytes + ")";
    124    }
    125}