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     */
018    package org.apache.bcel.classfile;
019    
020    import java.io.DataInput;
021    import java.io.DataOutputStream;
022    import java.io.IOException;
023    
024    import org.apache.bcel.Constants;
025    
026    /** 
027     * This class is derived from the abstract 
028     * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class 
029     * and represents a reference to a float object.
030     *
031     * @version $Id: ConstantFloat.java 1152072 2011-07-29 01:54:05Z dbrosius $
032     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033     * @see     Constant
034     */
035    public final class ConstantFloat extends Constant implements ConstantObject {
036    
037        private static final long serialVersionUID = -2316732495687628398L;
038        private float bytes;
039    
040    
041        /** 
042         * @param bytes Data
043         */
044        public ConstantFloat(float bytes) {
045            super(Constants.CONSTANT_Float);
046            this.bytes = bytes;
047        }
048    
049    
050        /**
051         * Initialize from another object. Note that both objects use the same
052         * references (shallow copy). Use clone() for a physical copy.
053         */
054        public ConstantFloat(ConstantFloat c) {
055            this(c.getBytes());
056        }
057    
058    
059        /** 
060         * Initialize instance from file data.
061         *
062         * @param file Input stream
063         * @throws IOException
064         */
065        ConstantFloat(DataInput file) throws IOException {
066            this(file.readFloat());
067        }
068    
069    
070        /**
071         * Called by objects that are traversing the nodes of the tree implicitely
072         * defined by the contents of a Java class. I.e., the hierarchy of methods,
073         * fields, attributes, etc. spawns a tree of objects.
074         *
075         * @param v Visitor object
076         */
077        @Override
078        public void accept( Visitor v ) {
079            v.visitConstantFloat(this);
080        }
081    
082    
083        /**
084         * Dump constant float to file stream in binary format.
085         *
086         * @param file Output file stream
087         * @throws IOException
088         */
089        @Override
090        public final void dump( DataOutputStream file ) throws IOException {
091            file.writeByte(tag);
092            file.writeFloat(bytes);
093        }
094    
095    
096        /**
097         * @return data, i.e., 4 bytes.
098         */
099        public final float getBytes() {
100            return bytes;
101        }
102    
103    
104        /**
105         * @param bytes the raw bytes that represent this float
106         */
107        public final void setBytes( float bytes ) {
108            this.bytes = bytes;
109        }
110    
111    
112        /**
113         * @return String representation.
114         */
115        @Override
116        public final String toString() {
117            return super.toString() + "(bytes = " + bytes + ")";
118        }
119    
120    
121        /** @return Float object
122         */
123        public Object getConstantValue( ConstantPool cp ) {
124            return new Float(bytes);
125        }
126    }