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.expr.*;
14: import gnu.bytecode.*;
15:
16: /**
17: Modifies the value of a static class field and returns the new value.
18:
19: @version $Date: 2002/02/02 12:21:43 $
20: @author Daniel Bonniot
21: */
22:
23: public class SetStaticFieldProc extends gnu.mapping.Procedure1
24: implements Inlineable {
25: private Declaration fieldDecl;
26:
27: public SetStaticFieldProc(Declaration fieldDecl) {
28: this .fieldDecl = fieldDecl;
29: }
30:
31: public Object apply1(Object arg) {
32: Field field = fieldDecl.field;
33: try {
34: java.lang.reflect.Field reflectField = field
35: .getReflectField();
36: reflectField.set(null, arg);
37: } catch (NoSuchFieldException ex) {
38: throw new RuntimeException("no such field "
39: + field.getSourceName() + " in "
40: + field.getDeclaringClass().getName());
41: } catch (IllegalAccessException ex) {
42: throw new RuntimeException("illegal access for field "
43: + field.getSourceName());
44: }
45: return arg;
46: }
47:
48: public void compile(ApplyExp exp, Compilation comp, Target target) {
49: Field field = fieldDecl.field;
50: Type fieldType = field.getType();
51:
52: Expression arg = exp.getArgs()[0];
53: CodeAttr code = comp.getCode();
54:
55: // tells whether we want the value to be returned
56: boolean ignore = target instanceof IgnoreTarget;
57:
58: arg.compile(comp, fieldType);
59:
60: if (!ignore)
61: // Place a copy of the new value before the two operands
62: code.emitDup(fieldType.getSize() > 4 ? 2 : 1, 1);
63:
64: code.emitPutStatic(field);
65:
66: if (!ignore)
67: target.compileFromStack(comp, fieldType);
68: }
69:
70: public gnu.bytecode.Type getReturnType(Expression[] args) {
71: return fieldDecl.getType();
72: }
73: }
|