Source code: nice/tools/code/SetStaticFieldProc.java
1 /**************************************************************************/
2 /* N I C E */
3 /* A high-level object-oriented research language */
4 /* (c) Daniel Bonniot 2000 */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /**************************************************************************/
12
13 package nice.tools.code;
14
15 import gnu.expr.*;
16 import gnu.bytecode.*;
17
18 /**
19 Modifies the value of a static class field and returns the new value.
20
21 @version $Date: 2002/02/02 12:21:43 $
22 @author Daniel Bonniot
23 */
24
25 public class SetStaticFieldProc extends gnu.mapping.Procedure1
26 implements Inlineable
27 {
28 private Declaration fieldDecl;
29
30 public SetStaticFieldProc (Declaration fieldDecl)
31 {
32 this.fieldDecl = fieldDecl;
33 }
34
35 public Object apply1 (Object arg)
36 {
37 Field field = fieldDecl.field;
38 try
39 {
40 java.lang.reflect.Field reflectField = field.getReflectField();
41 reflectField.set(null, arg);
42 }
43 catch (NoSuchFieldException ex)
44 {
45 throw new RuntimeException ("no such field " + field.getSourceName()
46 + " in " +
47 field.getDeclaringClass().getName());
48 }
49 catch (IllegalAccessException ex)
50 {
51 throw new RuntimeException("illegal access for field "
52 + field.getSourceName());
53 }
54 return arg;
55 }
56
57 public void compile (ApplyExp exp, Compilation comp, Target target)
58 {
59 Field field = fieldDecl.field;
60 Type fieldType = field.getType();
61
62 Expression arg = exp.getArgs()[0];
63 CodeAttr code = comp.getCode();
64
65 // tells whether we want the value to be returned
66 boolean ignore = target instanceof IgnoreTarget;
67
68 arg.compile(comp, fieldType);
69
70 if(!ignore)
71 // Place a copy of the new value before the two operands
72 code.emitDup(fieldType.getSize() > 4 ? 2 : 1, 1);
73
74 code.emitPutStatic(field);
75
76 if(!ignore)
77 target.compileFromStack(comp, fieldType);
78 }
79
80 public gnu.bytecode.Type getReturnType (Expression[] args)
81 {
82 return fieldDecl.getType();
83 }
84 }