1 package org.python.core; 3 import java.lang.reflect.Modifier ; 4 5 8 9 public class PyJavaInstance 10 extends PyInstance 11 implements java.io.Externalizable 12 { 13 public PyJavaInstance() { 14 } 15 16 public PyJavaInstance(PyJavaClass iclass) { 17 super(iclass, null); 18 } 19 20 public PyJavaInstance(Object proxy) { 21 super(PyJavaClass.lookup(proxy.getClass()), null); 22 javaProxy = proxy; 23 } 24 25 31 public void readExternal(java.io.ObjectInput in) 32 throws java.io.IOException , ClassNotFoundException 33 { 34 Object o = in.readObject(); 35 javaProxy = o; 36 instclass = PyJavaClass.lookup(o.getClass()); 37 } 38 39 44 public void writeExternal(java.io.ObjectOutput out) 45 throws java.io.IOException 46 { 47 out.writeObject(javaProxy); 49 } 50 51 52 public void __init__(PyObject[] args, String [] keywords) { 53 55 Class pc = instclass.proxyClass; 56 if (pc != null) { 57 int mods = pc.getModifiers(); 58 if (Modifier.isInterface(mods)) { 59 throw Py.TypeError("can't instantiate interface ("+ 60 instclass.__name__+")"); 61 } 62 else if (Modifier.isAbstract(mods)) { 63 throw Py.TypeError("can't instantiate abstract class ("+ 64 instclass.__name__+")"); 65 } 66 } 67 68 PyReflectedConstructor init = ((PyJavaClass)instclass).__init__; 69 if (init == null) { 70 throw Py.TypeError("no public constructors for "+ 71 instclass.__name__); 72 } 73 init.__call__(this, args, keywords); 74 } 75 76 protected void noField(String name, PyObject value) { 77 throw Py.TypeError("can't set arbitrary attribute in java instance: "+ 78 name); 79 } 80 81 protected void unassignableField(String name, PyObject value) { 82 throw Py.TypeError("can't assign to this attribute in java " + 83 "instance: " + name); 84 } 85 86 public int hashCode() { 87 if (javaProxy != null) { 88 return javaProxy.hashCode(); 89 } else { 90 return super.hashCode(); 91 } 92 } 93 94 public PyObject _is(PyObject o) { 95 if (o instanceof PyJavaInstance) { 96 return javaProxy == ((PyJavaInstance)o).javaProxy 97 ? Py.One : Py.Zero; 98 } 99 return Py.Zero; 100 } 101 102 public PyObject _isnot(PyObject o) { 103 return _is(o).__not__(); 104 } 105 106 public int __cmp__(PyObject o) { 107 if (!(o instanceof PyJavaInstance)) 108 return -2; 109 PyJavaInstance i = (PyJavaInstance)o; 110 if (javaProxy.equals(i.javaProxy)) 111 return 0; 112 return -2; 113 } 114 115 public PyString __str__() { 116 return new PyString(javaProxy.toString()); 117 } 118 119 public PyString __repr__() { 120 return __str__(); 121 } 122 123 public void __delattr__(String attr) { 124 throw Py.TypeError("can't delete attr from java instance: "+attr); 125 } 126 } 127 | Popular Tags |