01: package gnu.kawa.reflect;
02:
03: import gnu.bytecode.Type;
04: import gnu.bytecode.ClassType;
05: import gnu.mapping.*;
06: import gnu.expr.*;
07:
08: public class StaticGet extends Procedure0 implements Inlineable {
09: ClassType ctype;
10: String fname;
11: gnu.bytecode.Field field;
12: java.lang.reflect.Field reflectField;
13:
14: StaticGet(Class clas, String fname) {
15: ctype = (ClassType) gnu.bytecode.Type.make(clas);
16: this .fname = fname;
17: }
18:
19: public StaticGet(ClassType ctype, String name, Type ftype, int flags) {
20: this .ctype = ctype;
21: this .fname = name;
22: field = ctype.getField(name);
23: if (field == null)
24: field = ctype.addField(name, ftype, flags);
25: }
26:
27: public Object apply0() {
28: if (reflectField == null) {
29: Class clas = ctype.getReflectClass();
30: try {
31: reflectField = clas.getField(fname);
32: } catch (NoSuchFieldException ex) {
33: throw new RuntimeException("no such field " + fname
34: + " in " + clas.getName());
35: }
36: }
37: try {
38: return reflectField.get(null);
39: } catch (IllegalAccessException ex) {
40: throw new RuntimeException("illegal access for field "
41: + fname);
42: }
43: }
44:
45: private gnu.bytecode.Field getField() {
46: if (field == null) {
47: field = ctype.getField(fname);
48: if (field == null)
49: field = ctype.addField(fname, Type.make(reflectField
50: .getType()), reflectField.getModifiers());
51: }
52: return field;
53: }
54:
55: public void compile(ApplyExp exp, Compilation comp, Target target) {
56: getField();
57: gnu.bytecode.CodeAttr code = comp.getCode();
58: code.emitGetStatic(field);
59: target.compileFromStack(comp, field.getType());
60: }
61:
62: public gnu.bytecode.Type getReturnType(Expression[] args) {
63: return getField().getType();
64: }
65: }
|