001: package org.python.core;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Modifier;
006:
007: public class PyGetSetDescr extends PyDescriptor {
008:
009: private Method get_meth;
010:
011: private Method set_meth;
012:
013: private Method del_meth;
014:
015: private Class getset_type;
016:
017: public PyGetSetDescr(PyType dtype, String name, Class c,
018: String get, String set) {
019: this (dtype, name, c, get, set, null);
020: }
021:
022: public PyGetSetDescr(String name, Class c, String get, String set) {
023: this (PyType.fromClass(c), name, c, get, set, null);
024: }
025:
026: public PyGetSetDescr(PyType dtype, String name, Class c,
027: String get, String set, String del) {
028: this .name = name;
029: this .dtype = dtype;
030: try {
031: get_meth = c.getMethod(get, new Class[] {});
032: } catch (NoSuchMethodException e) {
033: throw Py.SystemError("method " + get + " doesn't exist: "
034: + c.getName());
035: }
036: if (Modifier.isStatic(get_meth.getModifiers()))
037: throw Py.SystemError("static " + get + " not supported: "
038: + c.getName());
039: getset_type = get_meth.getReturnType();
040: if (set != null) {
041: try {
042: set_meth = c
043: .getMethod(set, new Class[] { getset_type });
044: } catch (NoSuchMethodException e) {
045: throw Py.SystemError("method " + set
046: + " doesn't exist: " + c.getName());
047: }
048: if (Modifier.isStatic(set_meth.getModifiers()))
049: throw Py.SystemError("static " + set
050: + " not supported: " + c.getName());
051: }
052: if (del != null) {
053: try {
054: del_meth = c.getMethod(del, new Class[] {});
055: } catch (NoSuchMethodException e) {
056: throw Py.SystemError("method " + set
057: + " doesn't exist: " + c.getName());
058: }
059: if (Modifier.isStatic(del_meth.getModifiers()))
060: throw Py.SystemError("static " + del
061: + " not supported: " + c.getName());
062: }
063: }
064:
065: public PyGetSetDescr(String name, Class c, String get, String set,
066: String del) {
067: this (PyType.fromClass(c), name, c, get, set, del);
068: }
069:
070: public String toString() {
071: return "<attribute '" + name + "' of '" + dtype.fastGetName()
072: + "' objects>";
073: }
074:
075: /**
076: * @see org.python.core.PyObject#__get__(org.python.core.PyObject,
077: * org.python.core.PyObject)
078: */
079: public PyObject __get__(PyObject obj, PyObject type) {
080: try {
081: if (obj != null) {
082: PyType objtype = obj.getType();
083: if (objtype != dtype && !objtype.isSubType(dtype))
084: throw get_wrongtype(objtype);
085: Object v = get_meth.invoke(obj, new Object[0]);
086: if (v == null) {
087: obj.noAttributeError(name);
088: }
089: return Py.java2py(v);
090: }
091: return this ;
092: } catch (IllegalArgumentException e) {
093: throw Py.JavaError(e);
094: } catch (IllegalAccessException e) {
095: throw Py.JavaError(e); // unexpected
096: } catch (InvocationTargetException e) {
097: throw Py.JavaError(e);
098: }
099: }
100:
101: /**
102: * @see org.python.core.PyObject#__set__(org.python.core.PyObject,
103: * org.python.core.PyObject)
104: */
105: public void __set__(PyObject obj, PyObject value) {
106: try {
107: // obj != null
108: PyType objtype = obj.getType();
109: if (objtype != dtype && !objtype.isSubType(dtype))
110: throw get_wrongtype(objtype);
111: Object converted = value.__tojava__(getset_type);
112: if (converted == Py.NoConversion) {
113: throw Py.TypeError(""); // xxx
114: }
115: set_meth.invoke(obj, new Object[] { converted });
116: } catch (IllegalArgumentException e) {
117: throw Py.JavaError(e);
118: } catch (IllegalAccessException e) {
119: throw Py.JavaError(e); // unexpected
120: } catch (InvocationTargetException e) {
121: throw Py.JavaError(e);
122: }
123: }
124:
125: public void __delete__(PyObject obj) {
126: try {
127: if (obj != null) {
128: PyType objtype = obj.getType();
129: if (objtype != dtype && !objtype.isSubType(dtype))
130: throw get_wrongtype(objtype);
131: del_meth.invoke(obj, new Object[0]);
132: }
133: } catch (IllegalArgumentException e) {
134: throw Py.JavaError(e);
135: } catch (IllegalAccessException e) {
136: throw Py.JavaError(e); // unexpected
137: } catch (InvocationTargetException e) {
138: throw Py.JavaError(e);
139: }
140: }
141:
142: /**
143: * @see org.python.core.PyObject#implementsDescrSet()
144: */
145: public boolean implements DescrSet() {
146: return set_meth != null;
147: }
148:
149: public boolean implements DescrDelete() {
150: return del_meth != null;
151: }
152:
153: /**
154: * @see org.python.core.PyObject#isDataDescr()
155: */
156: public boolean isDataDescr() {
157: return true;
158: }
159:
160: }
|