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