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 StaticSet extends Procedure1 implements Inlineable {
09: ClassType ctype;
10: String fname;
11: gnu.bytecode.Field field;
12: java.lang.reflect.Field reflectField;
13:
14: StaticSet(Class clas, String fname) {
15: ctype = (ClassType) gnu.bytecode.Type.make(clas);
16: this .fname = fname;
17: }
18:
19: public StaticSet(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 apply1(Object arg1) {
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: reflectField.set(null, arg1);
39: return Values.empty;
40: } catch (IllegalAccessException ex) {
41: throw new RuntimeException("illegal access for field "
42: + fname);
43: }
44: }
45:
46: public void compile(ApplyExp exp, Compilation comp, Target target) {
47: if (field == null) {
48: field = ctype.getField(fname);
49: if (field == null)
50: field = ctype.addField(fname, Type.make(reflectField
51: .getType()), reflectField.getModifiers());
52: }
53: exp.getArgs()[0].compile(comp, field.getType());
54: gnu.bytecode.CodeAttr code = comp.getCode();
55: code.emitPutStatic(field);
56: comp.compileConstant(Values.empty, target);
57: }
58:
59: public gnu.bytecode.Type getReturnType(Expression[] args) {
60: return Type.void_type;
61: }
62: }
|