01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.core;
03:
04: import java.lang.reflect.*;
05:
06: public class PyBeanProperty extends PyReflectedField {
07: public Method getMethod, setMethod;
08: public Class myType;
09: String __name__;
10:
11: public PyBeanProperty(String name, Class myType, Method getMethod,
12: Method setMethod) {
13: __name__ = name;
14: this .getMethod = getMethod;
15: this .setMethod = setMethod;
16: this .myType = myType;
17: }
18:
19: public PyObject _doget(PyObject self) {
20: if (self == null) {
21: if (field != null) {
22: return super ._doget(null);
23: }
24: throw Py.AttributeError("instance attr: " + __name__);
25: }
26:
27: if (getMethod == null) {
28: throw Py.AttributeError("write-only attr: " + __name__);
29: }
30:
31: Object iself = Py.tojava(self, getMethod.getDeclaringClass());
32:
33: try {
34: Object value = getMethod.invoke(iself,
35: (Object[]) Py.EmptyObjects);
36: return Py.java2py(value);
37: } catch (Exception e) {
38: throw Py.JavaError(e);
39: }
40: }
41:
42: public boolean _doset(PyObject self, PyObject value) {
43: if (self == null) {
44: if (field != null) {
45: return super ._doset(null, value);
46: }
47: throw Py.AttributeError("instance attr: " + __name__);
48: }
49:
50: if (setMethod == null) {
51: throw Py.AttributeError("read-only attr: " + __name__);
52: }
53:
54: Object iself = Py.tojava(self, setMethod.getDeclaringClass());
55:
56: Object jvalue = null;
57:
58: // Special handling of tuples
59: // try to call a class constructor
60: if (value instanceof PyTuple) {
61: try {
62: PyTuple vtup = (PyTuple) value;
63: value = PyJavaClass.lookup(myType).__call__(
64: vtup.getArray()); // xxx PyObject subclasses
65: } catch (Throwable t) {
66: // If something goes wrong ignore it?
67: }
68: }
69: if (jvalue == null) {
70: jvalue = Py.tojava(value, myType);
71: }
72:
73: try {
74: setMethod.invoke(iself, new Object[] { jvalue });
75: } catch (Exception e) {
76: throw Py.JavaError(e);
77: }
78: return true;
79: }
80:
81: public PyBeanProperty copy() {
82: return new PyBeanProperty(__name__, myType, getMethod,
83: setMethod);
84: }
85:
86: public String toString() {
87: String typeName = "unknown";
88: if (myType != null) {
89: typeName = myType.getName();
90: }
91: return "<beanProperty " + __name__ + " type: " + typeName + " "
92: + Py.idstr(this ) + ">";
93: }
94: }
|