001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: import java.lang.reflect.Modifier;
005:
006: /**
007: * A wrapper around a java instance.
008: */
009:
010: public class PyJavaInstance extends PyInstance implements
011: java.io.Externalizable {
012: public PyJavaInstance() {
013: }
014:
015: public PyJavaInstance(PyJavaClass iclass) {
016: super (iclass, null);
017: }
018:
019: public PyJavaInstance(Object proxy) {
020: super (PyJavaClass.lookup(proxy.getClass()), null);
021: javaProxy = proxy;
022: }
023:
024: /**
025: * Implementation of the Externalizable interface.
026: * @param in the input stream.
027: * @exception java.io.IOException
028: * @exception ClassNotFoundException
029: */
030: public void readExternal(java.io.ObjectInput in)
031: throws java.io.IOException, ClassNotFoundException {
032: Object o = in.readObject();
033: javaProxy = o;
034: instclass = PyJavaClass.lookup(o.getClass());
035: }
036:
037: /**
038: * Implementation of the Externalizable interface.
039: * @param out the output stream.
040: * @exception java.io.IOException
041: */
042: public void writeExternal(java.io.ObjectOutput out)
043: throws java.io.IOException {
044: //System.out.println("writing java instance");
045: out.writeObject(javaProxy);
046: }
047:
048: public void __init__(PyObject[] args, String[] keywords) {
049: //javaProxies = new Object[1];
050:
051: Class pc = instclass.proxyClass;
052: if (pc != null) {
053: int mods = pc.getModifiers();
054: if (Modifier.isInterface(mods)) {
055: throw Py.TypeError("can't instantiate interface ("
056: + instclass.__name__ + ")");
057: } else if (Modifier.isAbstract(mods)) {
058: throw Py.TypeError("can't instantiate abstract class ("
059: + instclass.__name__ + ")");
060: }
061: }
062:
063: PyReflectedConstructor init = ((PyJavaClass) instclass).__init__;
064: if (init == null) {
065: throw Py.TypeError("no public constructors for "
066: + instclass.__name__);
067: }
068: init.__call__(this , args, keywords);
069: }
070:
071: protected void noField(String name, PyObject value) {
072: throw Py
073: .TypeError("can't set arbitrary attribute in java instance: "
074: + name);
075: }
076:
077: protected void unassignableField(String name, PyObject value) {
078: throw Py.TypeError("can't assign to this attribute in java "
079: + "instance: " + name);
080: }
081:
082: public int hashCode() {
083: if (javaProxy != null) {
084: return javaProxy.hashCode();
085: } else {
086: return super .hashCode();
087: }
088: }
089:
090: public PyObject _is(PyObject o) {
091: if (o instanceof PyJavaInstance) {
092: return javaProxy == ((PyJavaInstance) o).javaProxy ? Py.One
093: : Py.Zero;
094: }
095: return Py.Zero;
096: }
097:
098: public PyObject _isnot(PyObject o) {
099: return _is(o).__not__();
100: }
101:
102: public int __cmp__(PyObject o) {
103: if (!(o instanceof PyJavaInstance))
104: return -2;
105: PyJavaInstance i = (PyJavaInstance) o;
106: if (javaProxy.equals(i.javaProxy))
107: return 0;
108: return -2;
109: }
110:
111: public PyString __str__() {
112: return new PyString(javaProxy.toString());
113: }
114:
115: public PyString __repr__() {
116: return __str__();
117: }
118:
119: public void __delattr__(String attr) {
120: throw Py.TypeError("can't delete attr from java instance: "
121: + attr);
122: }
123: }
|