KickJava   Java API By Example, From Geeks To Geeks.

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


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 StaticGet extends Procedure0 implements Inlineable
8 {
9   ClassType ctype;
10   String JavaDoc fname;
11   gnu.bytecode.Field field;
12   java.lang.reflect.Field JavaDoc reflectField;
13
14   StaticGet (Class JavaDoc clas, String JavaDoc fname)
15   {
16     ctype = (ClassType) gnu.bytecode.Type.make(clas);
17     this.fname = fname;
18   }
19
20   public StaticGet (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 apply0 ()
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     return reflectField.get(null);
47       }
48     catch (IllegalAccessException JavaDoc ex)
49       {
50     throw new RuntimeException JavaDoc("illegal access for field "+fname);
51       }
52   }
53
54   private gnu.bytecode.Field getField ()
55   {
56     if (field == null)
57       {
58     field = ctype.getField(fname);
59     if (field == null)
60       field = ctype.addField(fname, Type.make(reflectField.getType()),
61                  reflectField.getModifiers());
62       }
63     return field;
64   }
65
66   public void compile (ApplyExp exp, Compilation comp, Target target)
67   {
68     getField();
69     gnu.bytecode.CodeAttr code = comp.getCode();
70     code.emitGetStatic(field);
71     target.compileFromStack(comp, field.getType());
72   }
73
74   public gnu.bytecode.Type getReturnType (Expression[] args)
75   {
76     return getField().getType();
77   }
78 }
79
Popular Tags