01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.core;
03:
04: import java.lang.reflect.Field;
05: import java.lang.reflect.Modifier;
06:
07: public class PyReflectedField extends PyObject {
08: public Field field;
09:
10: public PyReflectedField() {
11: }
12:
13: public PyReflectedField(Field field) {
14: this .field = field;
15: }
16:
17: public PyObject _doget(PyObject self) {
18: Object 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 value;
25:
26: try {
27: value = field.get(iself);
28: } catch (IllegalAccessException 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 iself = null;
37: if (!Modifier.isStatic(field.getModifiers())) {
38: if (self == null) {
39: throw Py
40: .AttributeError("set instance variable as static: "
41: + field.toString());
42: }
43: iself = Py.tojava(self, field.getDeclaringClass());
44: }
45: Object fvalue = Py.tojava(value, field.getType());
46:
47: try {
48: field.set(iself, fvalue);
49: } catch (IllegalAccessException exc) {
50: throw Py.JavaError(exc);
51: }
52: return true;
53: }
54:
55: public String toString() {
56: return "<reflected field " + field.toString() + " "
57: + Py.idstr(this ) + ">";
58: }
59: }
|