01: // Copyright 2000 Finn Bock
02:
03: package org.python.util;
04:
05: import java.io.*;
06: import org.python.core.*;
07:
08: public class PythonObjectInputStream extends ObjectInputStream {
09: public PythonObjectInputStream(InputStream istr) throws IOException {
10: super (istr);
11: }
12:
13: protected Class resolveClass(ObjectStreamClass v)
14: throws IOException, ClassNotFoundException {
15: String clsName = v.getName();
16: //System.out.println(clsName);
17: if (clsName.startsWith("org.python.proxies")) {
18: int idx = clsName.lastIndexOf('$');
19: if (idx > 19)
20: clsName = clsName.substring(19, idx);
21: //System.out.println("new:" + clsName);
22:
23: idx = clsName.indexOf('$');
24: if (idx >= 0) {
25: String mod = clsName.substring(0, idx);
26: clsName = clsName.substring(idx + 1);
27:
28: PyObject module = importModule(mod);
29: PyObject pycls = module.__getattr__(clsName.intern());
30: Object cls = pycls.__tojava__(Class.class);
31:
32: if (cls != null && cls != Py.NoConversion)
33: return (Class) cls;
34: }
35: }
36: try {
37: return super .resolveClass(v);
38: } catch (ClassNotFoundException exc) {
39: PyObject m = importModule(clsName);
40: //System.out.println("m:" + m);
41: Object cls = m.__tojava__(Class.class);
42: //System.out.println("cls:" + cls);
43: if (cls != null && cls != Py.NoConversion)
44: return (Class) cls;
45: throw exc;
46: }
47: }
48:
49: private static PyObject importModule(String name) {
50: PyObject silly_list = new PyTuple(new PyString[] { Py
51: .newString("__doc__"), });
52: return __builtin__.__import__(name, null, null, silly_list);
53: }
54: }
|