1 package org.python.core; 3 4 import java.lang.reflect.*; 5 import java.util.Hashtable ; 6 import java.io.*; 7 8 public class PyBeanEventProperty extends PyReflectedField 9 { 10 public Method addMethod; 11 public String eventName; 12 public Class eventClass; 13 public String __name__; 14 15 public PyBeanEventProperty(String eventName, Class eventClass, 16 Method addMethod, Method eventMethod) 17 { 18 __name__ = eventMethod.getName().intern(); 19 this.addMethod = addMethod; 20 this.eventName = eventName; 21 this.eventClass = eventClass; 22 } 23 24 public PyObject _doget(PyObject self) { 25 if (self == null) 26 return this; 27 28 initAdapter(); 29 30 Object jself = Py.tojava(self, addMethod.getDeclaringClass()); 31 32 Object field; 33 try { 34 field = adapterField.get(getAdapter(jself)); 35 } catch (Exception exc) { 36 throw Py.JavaError(exc); 37 } 38 39 PyCompoundCallable func; 40 if (field == null) { 41 func = new PyCompoundCallable(); 42 setFunction(jself, func); 43 return func; 44 } 45 if (field instanceof PyCompoundCallable) 46 return (PyCompoundCallable)field; 47 48 func = new PyCompoundCallable(); 49 setFunction(jself, func); 50 func.append((PyObject)field); 51 return func; 52 } 53 54 private synchronized static Class getAdapterClass(Class c) { 55 InternalTables tbl=PyJavaClass.getInternalTables(); 57 Object o = tbl.getAdapterClass(c); 58 if (o != null) 59 return (Class )o; 60 Class pc = Py.findClass("org.python.proxies."+c.getName()+"$Adapter"); 61 if (pc == null) { 62 pc = MakeProxies.makeAdapter(c); 66 } 67 tbl.putAdapterClass(c, pc); 68 return pc; 69 } 70 71 private synchronized Object getAdapter(Object self) { 72 InternalTables tbl=PyJavaClass.getInternalTables(); 73 String eventClassName = eventClass.getName(); 74 75 Object adapter = tbl.getAdapter(self, eventClassName); 76 if (adapter != null) 77 return adapter; 78 79 try { 80 adapter = adapterClass.newInstance(); 81 addMethod.invoke(self, new Object [] {adapter}); 82 } catch (Exception e) { 83 throw Py.JavaError(e); 84 } 85 tbl.putAdapter(self, eventClassName, adapter); 86 return adapter; 87 } 88 89 private Field adapterField; 90 private Class adapterClass; 91 92 private void initAdapter() { 93 if (adapterClass == null) { 94 adapterClass = getAdapterClass(eventClass); 95 } 96 if (adapterField == null) { 97 try { 98 adapterField = adapterClass.getField(__name__); 99 } catch (NoSuchFieldException exc) { 100 throw Py.AttributeError("Internal bean event error: "+ 101 __name__); 102 } 103 } 104 } 105 106 private void setFunction(Object self, PyObject callable) { 107 initAdapter(); 108 try { 109 adapterField.set(getAdapter(self), callable); 110 } catch (Exception exc) { 111 throw Py.JavaError(exc); 112 } 113 } 114 115 public boolean _doset(PyObject self, PyObject value) { 116 Object jself = Py.tojava(self, addMethod.getDeclaringClass()); 117 if (!(value instanceof PyCompoundCallable)) { 118 PyCompoundCallable func = new PyCompoundCallable(); 119 setFunction(jself, func); 120 func.append(value); 121 } else { 122 setFunction(jself, value); 123 } 124 return true; 125 } 126 127 public String toString() { 128 return "<beanEventProperty "+__name__+" for event "+ 129 eventClass.toString()+" "+Py.idstr(this)+">"; 130 } 131 } 132 | Popular Tags |