]> source.dussan.org Git - javassist.git/commitdiff
for JASSIST-37 (again)
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Sat, 20 Oct 2007 17:01:47 +0000 (17:01 +0000)
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Sat, 20 Oct 2007 17:01:47 +0000 (17:01 +0000)
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@412 30ef5769-5b8d-40dd-aea6-55b5d6557bb3

src/main/javassist/CtField.java

index efc6b94bafde0f0ec3ffcf2182454f884946040f..6903daf2a8607a5879f8a19cd3ffedd8617aca4e 100644 (file)
@@ -435,6 +435,14 @@ public class CtField extends CtMember {
             return new LongInitializer(l);
         }
 
+        /**
+         * Makes an initializer that assigns a constant float value.
+         * The field must be float type.
+         */
+        public static Initializer constant(float l) {
+            return new FloatInitializer(l);
+        }
+
         /**
          * Makes an initializer that assigns a constant double value.
          * The field must be double type.
@@ -1173,6 +1181,42 @@ public class CtField extends CtMember {
         }
     }
 
+    static class FloatInitializer extends Initializer {
+        float value;
+
+        FloatInitializer(float v) { value = v; }
+
+        void check(String desc) throws CannotCompileException {
+            if (!desc.equals("F"))
+                throw new CannotCompileException("type mismatch");
+        }
+
+        int compile(CtClass type, String name, Bytecode code,
+                    CtClass[] parameters, Javac drv)
+            throws CannotCompileException
+        {
+            code.addAload(0);
+            code.addFconst(value);
+            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
+            return 3;   // stack size
+        }
+
+        int compileIfStatic(CtClass type, String name, Bytecode code,
+                            Javac drv) throws CannotCompileException
+        {
+            code.addFconst(value);
+            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
+            return 2;   // stack size
+        }
+
+        int getConstantValue(ConstPool cp, CtClass type) {
+            if (type == CtClass.floatType)
+                return cp.addFloatInfo(value);
+            else
+                return 0;
+        }
+    }
+
     static class DoubleInitializer extends Initializer {
         double value;