1 package org.python.core; 3 import java.util.Vector ; 4 import java.io.Serializable ; 5 import java.lang.reflect.Method ; 6 7 10 11 public class PyClass extends PyObject 12 { 13 16 public PyObject __dict__; 17 18 21 public PyTuple __bases__; 22 23 26 public String __name__; 27 28 PyObject __getattr__, __setattr__, __delattr__, __tojava__, 31 __del__, __contains__; 32 33 protected Class proxyClass; 36 37 protected java.util.HashMap super__methods; 39 40 public static PyClass __class__; 41 42 PyClass(boolean fakeArg) { super(); 44 proxyClass = null; 45 } 46 47 protected PyClass() { 48 proxyClass = null; 49 } 50 51 52 62 public PyClass(String name, PyTuple bases, PyObject dict) { 63 this(name, bases, dict, null); 64 } 65 66 80 public PyClass(String name, PyTuple bases, PyObject dict, 81 Class proxyClass) 82 { 83 this.proxyClass = proxyClass; 84 init(name, bases, dict); 85 } 86 87 protected Class getProxyClass() { 88 return proxyClass; 89 } 90 91 void init(String name, PyTuple bases, PyObject dict) { 92 __name__ = name; 95 __bases__ = bases; 96 __dict__ = dict; 97 98 findModule(dict); 99 100 if (proxyClass == null) { 101 Vector interfaces = new Vector (); 102 Class baseClass = null; 103 for (int i=0; i<bases.size(); i++) { 104 Class proxy = ((PyClass)bases.pyget(i)).getProxyClass(); 105 if (proxy != null) { 106 if (proxy.isInterface()) { 107 interfaces.addElement(proxy); 108 } else { 109 if (baseClass != null) { 110 throw Py.TypeError("no multiple inheritance "+ 111 "for Java classes: "+ 112 proxy.getName()+ 113 " and "+ 114 baseClass.getName()); 115 } 116 if (PyObject.class.isAssignableFrom(proxy)) 118 throw Py.TypeError("subclassing PyObject subclasses" + " not supported"); 119 baseClass = proxy; 120 } 121 } 122 } 123 if (baseClass != null || interfaces.size() != 0) { 124 String proxyName = __name__; 125 PyObject module = dict.__finditem__("__module__"); 126 if (module != null) 127 proxyName = module.toString() + "$" + __name__; 128 proxyClass = MakeProxies.makeProxy(baseClass, interfaces, 129 __name__, proxyName, 130 __dict__); 131 } 132 } 133 134 if (proxyClass != null) { 135 PyObject superDict = 137 PyJavaClass.lookup(proxyClass).__findattr__("__dict__"); PyObject snames = superDict.__finditem__("__supernames__"); 140 if (snames != null) { 141 PyObject iter = snames.__iter__(); 142 for (PyObject item; (item = iter.__iternext__()) != null; ) { 143 if (__dict__.__finditem__(item) == null) { 144 PyObject superFunc = superDict.__finditem__(item); 145 if (superFunc != null) 146 __dict__.__setitem__(item, superFunc); 147 } 148 } 149 } 150 151 153 java.lang.reflect.Method proxy_methods[] = proxyClass.getMethods(); 154 155 super__methods = new java.util.HashMap (); 156 157 for(int i = 0; i<proxy_methods.length; i++) { 158 java.lang.reflect.Method meth = proxy_methods[i]; 159 String meth_name = meth.getName(); 160 if (meth_name.startsWith("super__")) { 161 java.util.ArrayList samename = (java.util.ArrayList )super__methods.get(meth_name); 162 if (samename == null) { 163 samename = new java.util.ArrayList (); 164 super__methods.put(meth_name,samename); 165 } 166 samename.add(meth); 167 } 168 } 169 170 java.lang.reflect.Method [] empty_methods = new java.lang.reflect.Method [0]; 171 for (java.util.Iterator iter = super__methods.entrySet().iterator(); 172 iter.hasNext();) { 173 java.util.Map.Entry entry = (java.util.Map.Entry)iter.next(); 174 entry.setValue(((java.util.ArrayList )entry.getValue()).toArray(empty_methods)); 176 } 177 178 } 179 180 if (dict.__finditem__("__doc__") == null) { 183 dict.__setitem__("__doc__", Py.None); 184 } 185 186 __getattr__ = lookup("__getattr__", false); 188 __setattr__ = lookup("__setattr__", false); 189 __delattr__ = lookup("__delattr__", false); 190 __tojava__ = lookup("__tojava__", false); 191 __del__ = lookup("__del__", false); 192 __contains__ = lookup("__contains__", false); 193 } 194 195 protected void findModule(PyObject dict) { 196 PyObject module = dict.__finditem__("__module__"); 197 if (module == null || module == Py.None) { 198 PyFrame f = Py.getFrame(); 200 if (f != null) { 201 PyObject nm = f.f_globals.__finditem__("__name__"); 202 if (nm != null) 203 dict.__setitem__("__module__", nm); 204 } 205 } 206 } 207 208 public Object __tojava__(Class c) { 209 if ((c == Object .class || c == Class .class || c == Serializable .class) 210 && proxyClass != null) { 211 return proxyClass; 212 } 213 return super.__tojava__(c); 214 } 215 216 PyObject[] lookupGivingClass(String name, boolean stop_at_java) { 218 PyObject result = __dict__.__finditem__(name); 219 PyClass resolvedClass = this; 220 if (result == null && __bases__ != null) { 221 int n = __bases__.__len__(); 222 for (int i=0; i<n; i++) { 223 resolvedClass = (PyClass)(__bases__.__getitem__(i)); 224 PyObject[] res = resolvedClass.lookupGivingClass(name, 225 stop_at_java); 226 if (res[0] != null) 227 return res; 228 } 229 } 230 return new PyObject[] {result, resolvedClass}; 231 } 232 233 PyObject lookup(String name, boolean stop_at_java) { 234 PyObject[] result = lookupGivingClass(name, stop_at_java); 235 return result[0]; 236 } 237 238 public PyObject __findattr__(String name) { 239 if (name == "__dict__") return __dict__; 240 if (name == "__name__") return new PyString(__name__); 241 if (name == "__bases__") return __bases__; 242 243 PyObject[] result = lookupGivingClass(name, false); 244 245 if (result[0] == null) 246 return super.__findattr__(name); 247 return result[0].__get__(null, this); 249 } 250 251 public void __setattr__(String name, PyObject value) { 252 if (name == "__dict__") { 253 if (!value.isMappingType()) 254 throw Py.TypeError("__dict__ must be a dictionary object"); 255 __dict__ = value; 256 return; 257 } 258 if (name == "__name__") { 259 if (!(value instanceof PyString)) 260 throw Py.TypeError("__name__ must be a string object"); 261 __name__ = value.toString(); 262 return; 263 } 264 if (name == "__bases__") { 265 if (!(value instanceof PyTuple)) 266 throw Py.TypeError("__bases__ must be a tuple object"); 267 __bases__ = (PyTuple) value; 268 return; 269 } 270 271 __dict__.__setitem__(name, value); 272 } 273 274 public void __delattr__(String name) { 275 __dict__.__delitem__(name); 276 } 277 278 public void __rawdir__(PyDictionary accum) { 279 addKeys(accum, "__dict__"); 280 PyObject[] bases = __bases__.getArray(); 281 for (int i=0; i < bases.length; i++) 282 bases[i].__rawdir__(accum); 283 } 284 285 public PyObject __call__(PyObject[] args, String [] keywords) { 286 PyInstance inst; 287 if (__del__ == null) 288 inst = new PyInstance(this); 289 else 290 inst = new PyFinalizableInstance(this); 292 293 inst.__init__(args, keywords); 294 295 303 304 return inst; 305 } 306 307 308 public int __cmp__(PyObject other) { 309 if (!(other instanceof PyClass)) 310 return -2; 311 int c = __name__.compareTo(((PyClass)other).__name__); 312 return c < 0 ? -1 : c > 0 ? 1 : 0; 313 } 314 315 public PyString __str__() { 316 if (__dict__ == null) 320 return new PyString(__name__); 321 PyObject mod = __dict__.__finditem__("__module__"); 322 if (mod == null || !(mod instanceof PyString)) 323 return new PyString(__name__); 324 String smod = ((PyString)mod).toString(); 325 return new PyString(smod + "." + __name__); 326 } 327 328 public String toString() { 329 PyObject mod = __dict__.__finditem__("__module__"); 330 String smod; 331 if (mod == null || !(mod instanceof PyString)) 332 smod = "<unknown>"; 333 else 334 smod = ((PyString)mod).toString(); 335 return "<class "+smod+"."+__name__+" "+Py.idstr(this)+">"; 336 } 337 338 public boolean isSubClass(PyClass superclass) { 339 if (this == superclass) 340 return true; 341 if (this.proxyClass != null && superclass.proxyClass != null) { 342 if (superclass.proxyClass.isAssignableFrom(this.proxyClass)) 343 return true; 344 } 345 if (this.__bases__ == null || superclass.__bases__ == null) 346 return false; 347 PyObject[] bases = this.__bases__.getArray(); 348 int n = bases.length; 349 for(int i=0; i<n; i++) { 350 PyClass c = (PyClass)bases[i]; 351 if (c.isSubClass(superclass)) 352 return true; 353 } 354 return false; 355 } 356 357 360 public String safeRepr() throws PyIgnoreMethodTag { 361 return "class '"+__name__+"'"; 362 } 363 364 } 365 | Popular Tags |