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 package org.apache.bcel.classfile; 18 19 import java.io.DataInput; 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 23 import org.apache.bcel.Const; 24 25 /** 26 * This class is derived from the abstract {@link Constant} and represents a reference to a float object. 27 * 28 * @see Constant 29 */ 30 public final class ConstantFloat extends Constant implements ConstantObject { 31 32 private float bytes; 33 34 /** 35 * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a 36 * physical copy. 37 * 38 * @param c Source to copy. 39 */ 40 public ConstantFloat(final ConstantFloat c) { 41 this(c.getBytes()); 42 } 43 44 /** 45 * Initialize instance from file data. 46 * 47 * @param file Input stream 48 * @throws IOException if an I/O error occurs. 49 */ 50 ConstantFloat(final DataInput file) throws IOException { 51 this(file.readFloat()); 52 } 53 54 /** 55 * @param bytes Data 56 */ 57 public ConstantFloat(final float bytes) { 58 super(Const.CONSTANT_Float); 59 this.bytes = bytes; 60 } 61 62 /** 63 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 64 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 65 * 66 * @param v Visitor object 67 */ 68 @Override 69 public void accept(final Visitor v) { 70 v.visitConstantFloat(this); 71 } 72 73 /** 74 * Dump constant float to file stream in binary format. 75 * 76 * @param file Output file stream 77 * @throws IOException if an I/O error occurs. 78 */ 79 @Override 80 public void dump(final DataOutputStream file) throws IOException { 81 file.writeByte(super.getTag()); 82 file.writeFloat(bytes); 83 } 84 85 /** 86 * @return data, i.e., 4 bytes. 87 */ 88 public float getBytes() { 89 return bytes; 90 } 91 92 /** 93 * @return Float object 94 */ 95 @Override 96 public Object getConstantValue(final ConstantPool cp) { 97 return Float.valueOf(bytes); 98 } 99 100 /** 101 * @param bytes the raw bytes that represent this float 102 */ 103 public void setBytes(final float bytes) { 104 this.bytes = bytes; 105 } 106 107 /** 108 * @return String representation. 109 */ 110 @Override 111 public String toString() { 112 return super.toString() + "(bytes = " + bytes + ")"; 113 } 114 }