KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyJavaInstance


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3 import java.lang.reflect.Modifier JavaDoc;
4
5 /**
6  * A wrapper around a java instance.
7  */

8
9 public class PyJavaInstance
10     extends PyInstance
11     implements java.io.Externalizable JavaDoc
12 {
13     public PyJavaInstance() {
14     }
15
16     public PyJavaInstance(PyJavaClass iclass) {
17         super(iclass, null);
18     }
19
20     public PyJavaInstance(Object JavaDoc proxy) {
21         super(PyJavaClass.lookup(proxy.getClass()), null);
22         javaProxy = proxy;
23     }
24
25     /**
26      * Implementation of the Externalizable interface.
27      * @param in the input stream.
28      * @exception java.io.IOException
29      * @exception ClassNotFoundException
30      */

31     public void readExternal(java.io.ObjectInput JavaDoc in)
32         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
33     {
34         Object JavaDoc o = in.readObject();
35         javaProxy = o;
36         instclass = PyJavaClass.lookup(o.getClass());
37     }
38
39     /**
40      * Implementation of the Externalizable interface.
41      * @param out the output stream.
42      * @exception java.io.IOException
43      */

44     public void writeExternal(java.io.ObjectOutput JavaDoc out)
45         throws java.io.IOException JavaDoc
46     {
47         //System.out.println("writing java instance");
48
out.writeObject(javaProxy);
49     }
50
51
52     public void __init__(PyObject[] args, String JavaDoc[] keywords) {
53         //javaProxies = new Object[1];
54

55         Class JavaDoc 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 JavaDoc name, PyObject value) {
77         throw Py.TypeError("can't set arbitrary attribute in java instance: "+
78                            name);
79     }
80
81     protected void unassignableField(String JavaDoc 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 JavaDoc attr) {
124         throw Py.TypeError("can't delete attr from java instance: "+attr);
125     }
126 }
127
Popular Tags