1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.bcel.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from the abstract {@link Constant}
28  * and represents a reference to a float object.
29  *
30  * @version $Id$
31  * @see     Constant
32  */
33 public final class ConstantFloat extends Constant implements ConstantObject {
34 
35     private float bytes;
36 
37 
38     /**
39      * @param bytes Data
40      */
ConstantFloat(final float bytes)41     public ConstantFloat(final float bytes) {
42         super(Const.CONSTANT_Float);
43         this.bytes = bytes;
44     }
45 
46 
47     /**
48      * Initialize from another object. Note that both objects use the same
49      * references (shallow copy). Use clone() for a physical copy.
50      */
ConstantFloat(final ConstantFloat c)51     public ConstantFloat(final ConstantFloat c) {
52         this(c.getBytes());
53     }
54 
55 
56     /**
57      * Initialize instance from file data.
58      *
59      * @param file Input stream
60      * @throws IOException
61      */
ConstantFloat(final DataInput file)62     ConstantFloat(final DataInput file) throws IOException {
63         this(file.readFloat());
64     }
65 
66 
67     /**
68      * Called by objects that are traversing the nodes of the tree implicitely
69      * defined by the contents of a Java class. I.e., the hierarchy of methods,
70      * fields, attributes, etc. spawns a tree of objects.
71      *
72      * @param v Visitor object
73      */
74     @Override
accept( final Visitor v )75     public void accept( final Visitor v ) {
76         v.visitConstantFloat(this);
77     }
78 
79 
80     /**
81      * Dump constant float to file stream in binary format.
82      *
83      * @param file Output file stream
84      * @throws IOException
85      */
86     @Override
dump( final DataOutputStream file )87     public final void dump( final DataOutputStream file ) throws IOException {
88         file.writeByte(super.getTag());
89         file.writeFloat(bytes);
90     }
91 
92 
93     /**
94      * @return data, i.e., 4 bytes.
95      */
getBytes()96     public final float getBytes() {
97         return bytes;
98     }
99 
100 
101     /**
102      * @param bytes the raw bytes that represent this float
103      */
setBytes( final float bytes )104     public final void setBytes( final float bytes ) {
105         this.bytes = bytes;
106     }
107 
108 
109     /**
110      * @return String representation.
111      */
112     @Override
toString()113     public final String toString() {
114         return super.toString() + "(bytes = " + bytes + ")";
115     }
116 
117 
118     /** @return Float object
119      */
120     @Override
getConstantValue( final ConstantPool cp )121     public Object getConstantValue( final ConstantPool cp ) {
122         return new Float(bytes);
123     }
124 }
125