1 package org.python.core; 3 import java.lang.reflect.*; 4 5 public class PyBeanProperty extends PyReflectedField { 6 public Method getMethod, setMethod; 7 public Class myType; 8 String __name__; 9 10 public PyBeanProperty(String name, Class 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 iself = Py.tojava(self, getMethod.getDeclaringClass()); 32 33 try { 34 Object value = getMethod.invoke(iself, (Object [])Py.EmptyObjects); 35 return Py.java2py(value); 36 } catch (Exception 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 iself = Py.tojava(self, setMethod.getDeclaringClass()); 54 55 Object jvalue=null; 56 57 if (value instanceof PyTuple) { 60 try { 61 PyTuple vtup = (PyTuple)value; 62 value = PyJavaClass.lookup(myType).__call__(vtup.getArray()); } catch (Throwable t) { 64 } 66 } 67 if (jvalue == null) { 68 jvalue = Py.tojava(value, myType); 69 } 70 71 try { 72 setMethod.invoke(iself, new Object [] {jvalue}); 73 } catch (Exception 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 toString() { 84 String 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 |