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