KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > GetFieldProc


1 package kawa.lang;
2 import gnu.bytecode.*;
3 import gnu.mapping.*;
4 import gnu.expr.*;
5
6 // Should be called PrimGetField for consistency.
7

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