01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2000 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.tools.code;
12:
13: import gnu.bytecode.*;
14: import gnu.mapping.*;
15: import gnu.expr.*;
16:
17: /**
18: Get the value of an object's field.
19:
20: @author Daniel Bonniot
21: */
22:
23: public class GetFieldProc extends Procedure1 implements Inlineable {
24: public GetFieldProc(Declaration fieldDecl) {
25: this .fieldDecl = fieldDecl;
26:
27: // Fail fast.
28: if (fieldDecl == null)
29: throw new NullPointerException();
30: }
31:
32: private Declaration fieldDecl;
33:
34: Field getField() {
35: return fieldDecl.field;
36: }
37:
38: public void compile(ApplyExp exp, Compilation comp, Target target) {
39: Field field = fieldDecl.field;
40: boolean isStatic = field.getStaticFlag();
41: CodeAttr code = comp.getCode();
42:
43: if (!isStatic) {
44: ClassType ctype = field.getDeclaringClass();
45: exp.getArgs()[0].compile(comp, ctype);
46: code.emitGetField(field);
47: } else
48: code.emitGetStatic(field);
49:
50: target.compileFromStack(comp, field.getType());
51: }
52:
53: public gnu.bytecode.Type getReturnType(Expression[] args) {
54: return fieldDecl.getType();
55: }
56:
57: // Interpretation
58:
59: public Object apply1(Object arg1) {
60: Field field = fieldDecl.field;
61: try {
62: java.lang.reflect.Field reflectField = field
63: .getReflectField();
64: return reflectField.get(arg1);
65: } catch (NoSuchFieldException ex) {
66: throw new RuntimeException("no such field "
67: + field.getSourceName() + " in "
68: + field.getDeclaringClass());
69: } catch (IllegalAccessException ex) {
70: throw new RuntimeException("illegal access for field "
71: + field.getSourceName());
72: }
73: }
74: }
|