Home | History | Annotate | Download | only in annotation
      1 /*
      2  * Javassist, a Java-bytecode translator toolkit.
      3  * Copyright (C) 2004 Bill Burke. All Rights Reserved.
      4  *
      5  * The contents of this file are subject to the Mozilla Public License Version
      6  * 1.1 (the "License"); you may not use this file except in compliance with
      7  * the License.  Alternatively, the contents of this file may be used under
      8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
      9  *
     10  * Software distributed under the License is distributed on an "AS IS" basis,
     11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     12  * for the specific language governing rights and limitations under the
     13  * License.
     14  */
     15 
     16 package javassist.bytecode.annotation;
     17 
     18 import javassist.ClassPool;
     19 import javassist.bytecode.ConstPool;
     20 import java.io.IOException;
     21 import java.lang.reflect.Method;
     22 
     23 /**
     24  * Floating-point number constant value.
     25  *
     26  * @author <a href="mailto:bill (at) jboss.org">Bill Burke</a>
     27  * @author Shigeru Chiba
     28  * @version $Revision: 1.7 $
     29  */
     30 public class FloatMemberValue extends MemberValue {
     31     int valueIndex;
     32 
     33     /**
     34      * Constructs a float constant value.  The initial value is specified
     35      * by the constant pool entry at the given index.
     36      *
     37      * @param index     the index of a CONSTANT_Float_info structure.
     38      */
     39     public FloatMemberValue(int index, ConstPool cp) {
     40         super('F', cp);
     41         this.valueIndex = index;
     42     }
     43 
     44     /**
     45      * Constructs a float constant value.
     46      *
     47      * @param f         the initial value.
     48      */
     49     public FloatMemberValue(float f, ConstPool cp) {
     50         super('F', cp);
     51         setValue(f);
     52     }
     53 
     54     /**
     55      * Constructs a float constant value.  The initial value is 0.0.
     56      */
     57     public FloatMemberValue(ConstPool cp) {
     58         super('F', cp);
     59         setValue(0.0F);
     60     }
     61 
     62     Object getValue(ClassLoader cl, ClassPool cp, Method m) {
     63         return new Float(getValue());
     64     }
     65 
     66     Class getType(ClassLoader cl) {
     67         return float.class;
     68     }
     69 
     70     /**
     71      * Obtains the value of the member.
     72      */
     73     public float getValue() {
     74         return cp.getFloatInfo(valueIndex);
     75     }
     76 
     77     /**
     78      * Sets the value of the member.
     79      */
     80     public void setValue(float newValue) {
     81         valueIndex = cp.addFloatInfo(newValue);
     82     }
     83 
     84     /**
     85      * Obtains the string representation of this object.
     86      */
     87     public String toString() {
     88         return Float.toString(getValue());
     89     }
     90 
     91     /**
     92      * Writes the value.
     93      */
     94     public void write(AnnotationsWriter writer) throws IOException {
     95         writer.constValueIndex(getValue());
     96     }
     97 
     98     /**
     99      * Accepts a visitor.
    100      */
    101     public void accept(MemberValueVisitor visitor) {
    102         visitor.visitFloatMemberValue(this);
    103     }
    104 }
    105