KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > reflect > StaticSet


1 package gnu.kawa.reflect;
2 import gnu.bytecode.Type;
3 import gnu.bytecode.ClassType;
4 import gnu.mapping.*;
5 import gnu.expr.*;
6
7 public class StaticSet extends Procedure1 implements Inlineable
8 {
9   ClassType ctype;
10   String JavaDoc fname;
11   gnu.bytecode.Field field;
12   java.lang.reflect.Field JavaDoc reflectField;
13
14   StaticSet (Class JavaDoc clas, String JavaDoc fname)
15   {
16     ctype = (ClassType) gnu.bytecode.Type.make(clas);
17     this.fname = fname;
18   }
19
20   public StaticSet (ClassType ctype, String JavaDoc name, Type ftype, int flags)
21   {
22     this.ctype = ctype;
23     this.fname = name;
24     field = ctype.getField(name);
25     if (field == null)
26       field = ctype.addField(name, ftype, flags);
27   }
28
29   public Object JavaDoc apply1 (Object JavaDoc arg1)
30   {
31     if (reflectField == null)
32       {
33     Class JavaDoc clas = ctype.getReflectClass();
34     try
35       {
36         reflectField = clas.getField (fname);
37       }
38     catch (NoSuchFieldException JavaDoc ex)
39       {
40         throw new RuntimeException JavaDoc("no such field "+fname
41                        +" in "+clas.getName());
42       }
43       }
44     try
45       {
46     reflectField.set(null, arg1);
47     return Values.empty;
48       }
49     catch (IllegalAccessException JavaDoc ex)
50       {
51     throw new RuntimeException JavaDoc("illegal access for field "+fname);
52       }
53   }
54
55   public void compile (ApplyExp exp, Compilation comp, Target target)
56   {
57     if (field == null)
58       {
59     field = ctype.getField(fname);
60     if (field == null)
61       field = ctype.addField(fname, Type.make(reflectField.getType()),
62                  reflectField.getModifiers());
63       }
64     exp.getArgs()[0].compile(comp, field.getType());
65     gnu.bytecode.CodeAttr code = comp.getCode();
66     code.emitPutStatic(field);
67     comp.compileConstant(Values.empty, target);
68   }
69
70   public gnu.bytecode.Type getReturnType (Expression[] args)
71   {
72     return Type.void_type;
73   }
74 }
75
Popular Tags