KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3 import java.lang.reflect.*;
4
5 public class PyBeanProperty extends PyReflectedField {
6     public Method getMethod, setMethod;
7     public Class JavaDoc myType;
8     String JavaDoc __name__;
9
10     public PyBeanProperty(String JavaDoc name, Class JavaDoc myType,
11                           Method getMethod, Method setMethod)
12     {
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 JavaDoc iself = Py.tojava(self, getMethod.getDeclaringClass());
32
33         try {
34             Object JavaDoc value = getMethod.invoke(iself, (Object JavaDoc[])Py.EmptyObjects);
35             return Py.java2py(value);
36         } catch (Exception JavaDoc e) {
37             throw Py.JavaError(e);
38         }
39     }
40
41     public boolean _doset(PyObject self, PyObject value) {
42         if (self == null) {
43             if (field != null) {
44                 return super._doset(null, value);
45             }
46             throw Py.AttributeError("instance attr: "+__name__);
47         }
48
49         if (setMethod == null) {
50             throw Py.AttributeError("read-only attr: "+__name__);
51         }
52
53         Object JavaDoc iself = Py.tojava(self, setMethod.getDeclaringClass());
54
55         Object JavaDoc jvalue=null;
56
57         // Special handling of tuples
58
// try to call a class constructor
59
if (value instanceof PyTuple) {
60             try {
61                 PyTuple vtup = (PyTuple)value;
62                 value = PyJavaClass.lookup(myType).__call__(vtup.getArray()); // xxx PyObject subclasses
63
} catch (Throwable JavaDoc t) {
64                 // If something goes wrong ignore it?
65
}
66         }
67         if (jvalue == null) {
68             jvalue = Py.tojava(value, myType);
69         }
70
71         try {
72             setMethod.invoke(iself, new Object JavaDoc[] {jvalue});
73         } catch (Exception JavaDoc e) {
74             throw Py.JavaError(e);
75         }
76         return true;
77     }
78
79     public PyBeanProperty copy() {
80         return new PyBeanProperty(__name__, myType, getMethod, setMethod);
81     }
82
83     public String JavaDoc toString() {
84         String JavaDoc typeName = "unknown";
85         if (myType != null) {
86             typeName = myType.getName();
87         }
88         return "<beanProperty "+__name__+" type: "+typeName+" "+
89             Py.idstr(this)+">";
90     }
91 }
92
93
Popular Tags