ConstantFloat xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.classfile;
    18
    19  import java.io.DataInputStream;
    20  import java.io.DataOutputStream;
    21  import java.io.IOException;
    22  import org.apache.bcel.Constants;
    23
    24  /***
    25   * This class is derived from the abstract
    26   * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class
    27   * and represents a reference to a float object.
    28   *
    29   * @version $Id: ConstantFloat.java 386056 2006-03-15 11:31:56Z tcurdt $
    30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    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       */
    41      public ConstantFloat(float bytes) {
    42          super(Constants.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       */
    51      public ConstantFloat(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       */
    62      ConstantFloat(DataInputStream 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      public void accept( Visitor v ) {
    75          v.visitConstantFloat(this);
    76      }
    77
    78
    79      /***
    80       * Dump constant float to file stream in binary format.
    81       *
    82       * @param file Output file stream
    83       * @throws IOException
    84       */
    85      public final void dump( DataOutputStream file ) throws IOException {
    86          file.writeByte(tag);
    87          file.writeFloat(bytes);
    88      }
    89
    90
    91      /***
    92       * @return data, i.e., 4 bytes.
    93       */
    94      public final float getBytes() {
    95          return bytes;
    96      }
    97
    98
    99      /***
    100      * @param bytes the raw bytes that represent this float
    101      */
    102     public final void setBytes( float bytes ) {
    103         this.bytes = bytes;
    104     }
    105
    106
    107     /***
    108      * @return String representation.
    109      */
    110     public final String toString() {
    111         return super.toString() + "(bytes = " + bytes + ")";
    112     }
    113
    114
    115     /*** @return Float object
    116      */
    117     public Object getConstantValue( ConstantPool cp ) {
    118         return new Float(bytes);
    119     }
    120 }