• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * or the Apache License Version 2.0.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  */
16 
17 package javassist.bytecode.annotation;
18 
19 import java.io.IOException;
20 import java.lang.reflect.Method;
21 
22 import javassist.ClassPool;
23 import javassist.bytecode.ConstPool;
24 
25 /**
26  * Floating-point number constant value.
27  *
28  * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
29  * @author Shigeru Chiba
30  * @version $Revision: 1.7 $
31  */
32 public class FloatMemberValue extends MemberValue {
33     int valueIndex;
34 
35     /**
36      * Constructs a float constant value.  The initial value is specified
37      * by the constant pool entry at the given index.
38      *
39      * @param index     the index of a CONSTANT_Float_info structure.
40      */
FloatMemberValue(int index, ConstPool cp)41     public FloatMemberValue(int index, ConstPool cp) {
42         super('F', cp);
43         this.valueIndex = index;
44     }
45 
46     /**
47      * Constructs a float constant value.
48      *
49      * @param f         the initial value.
50      */
FloatMemberValue(float f, ConstPool cp)51     public FloatMemberValue(float f, ConstPool cp) {
52         super('F', cp);
53         setValue(f);
54     }
55 
56     /**
57      * Constructs a float constant value.  The initial value is 0.0.
58      */
FloatMemberValue(ConstPool cp)59     public FloatMemberValue(ConstPool cp) {
60         super('F', cp);
61         setValue(0.0F);
62     }
63 
64     @Override
getValue(ClassLoader cl, ClassPool cp, Method m)65     Object getValue(ClassLoader cl, ClassPool cp, Method m) {
66         return Float.valueOf(getValue());
67     }
68 
69     @Override
getType(ClassLoader cl)70     Class<?> getType(ClassLoader cl) {
71         return float.class;
72     }
73 
74     /**
75      * Obtains the value of the member.
76      */
getValue()77     public float getValue() {
78         return cp.getFloatInfo(valueIndex);
79     }
80 
81     /**
82      * Sets the value of the member.
83      */
setValue(float newValue)84     public void setValue(float newValue) {
85         valueIndex = cp.addFloatInfo(newValue);
86     }
87 
88     /**
89      * Obtains the string representation of this object.
90      */
91     @Override
toString()92     public String toString() {
93         return Float.toString(getValue());
94     }
95 
96     /**
97      * Writes the value.
98      */
99     @Override
write(AnnotationsWriter writer)100     public void write(AnnotationsWriter writer) throws IOException {
101         writer.constValueIndex(getValue());
102     }
103 
104     /**
105      * Accepts a visitor.
106      */
107     @Override
accept(MemberValueVisitor visitor)108     public void accept(MemberValueVisitor visitor) {
109         visitor.visitFloatMemberValue(this);
110     }
111 }
112