ConstantFloat.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * This class is derived from the abstract {@link Constant} and represents a reference to a float object.
  24.  *
  25.  * @see Constant
  26.  */
  27. public final class ConstantFloat extends Constant implements ConstantObject {

  28.     private float bytes;

  29.     /**
  30.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
  31.      * physical copy.
  32.      *
  33.      * @param c Source to copy.
  34.      */
  35.     public ConstantFloat(final ConstantFloat c) {
  36.         this(c.getBytes());
  37.     }

  38.     /**
  39.      * Initialize instance from file data.
  40.      *
  41.      * @param file Input stream
  42.      * @throws IOException if an I/O error occurs.
  43.      */
  44.     ConstantFloat(final DataInput file) throws IOException {
  45.         this(file.readFloat());
  46.     }

  47.     /**
  48.      * @param bytes Data
  49.      */
  50.     public ConstantFloat(final float bytes) {
  51.         super(Const.CONSTANT_Float);
  52.         this.bytes = bytes;
  53.     }

  54.     /**
  55.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  56.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  57.      *
  58.      * @param v Visitor object
  59.      */
  60.     @Override
  61.     public void accept(final Visitor v) {
  62.         v.visitConstantFloat(this);
  63.     }

  64.     /**
  65.      * Dump constant float to file stream in binary format.
  66.      *
  67.      * @param file Output file stream
  68.      * @throws IOException if an I/O error occurs.
  69.      */
  70.     @Override
  71.     public void dump(final DataOutputStream file) throws IOException {
  72.         file.writeByte(super.getTag());
  73.         file.writeFloat(bytes);
  74.     }

  75.     /**
  76.      * @return data, i.e., 4 bytes.
  77.      */
  78.     public float getBytes() {
  79.         return bytes;
  80.     }

  81.     /**
  82.      * @return Float object
  83.      */
  84.     @Override
  85.     public Object getConstantValue(final ConstantPool cp) {
  86.         return Float.valueOf(bytes);
  87.     }

  88.     /**
  89.      * @param bytes the raw bytes that represent this float
  90.      */
  91.     public void setBytes(final float bytes) {
  92.         this.bytes = bytes;
  93.     }

  94.     /**
  95.      * @return String representation.
  96.      */
  97.     @Override
  98.     public String toString() {
  99.         return super.toString() + "(bytes = " + bytes + ")";
  100.     }
  101. }