KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyReflectedField


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Modifier JavaDoc;
6
7
8 public class PyReflectedField extends PyObject {
9     public Field JavaDoc field;
10
11     public PyReflectedField() {}
12
13     public PyReflectedField(Field JavaDoc field) {
14         this.field = field;
15     }
16
17     public PyObject _doget(PyObject self) {
18         Object JavaDoc iself = null;
19         if (!Modifier.isStatic(field.getModifiers())) {
20             if (self == null)
21                 return this;
22             iself = Py.tojava(self, field.getDeclaringClass());
23         }
24         Object JavaDoc value;
25
26         try {
27             value = field.get(iself);
28         } catch (IllegalAccessException JavaDoc exc) {
29             throw Py.JavaError(exc);
30         }
31
32         return Py.java2py(value);
33     }
34
35     public boolean _doset(PyObject self, PyObject value) {
36         Object JavaDoc iself = null;
37         if (!Modifier.isStatic(field.getModifiers())) {
38             if (self == null) {
39                 throw Py.AttributeError("set instance variable as static: "+
40                                         field.toString());
41             }
42             iself = Py.tojava(self, field.getDeclaringClass());
43         }
44         Object JavaDoc fvalue = Py.tojava(value, field.getType());
45
46         try {
47             field.set(iself, fvalue);
48         } catch (IllegalAccessException JavaDoc exc) {
49             throw Py.JavaError(exc);
50         }
51         return true;
52     }
53
54     public String JavaDoc toString() {
55         return "<reflected field "+field.toString()+" "+Py.idstr(this)+">";
56     }
57 }
58
Popular Tags