|
4 | 4 | import java.io.*;
|
5 | 5 | import java.util.StringTokenizer;
|
6 | 6 | import java.util.Hashtable;
|
| 7 | +import java.util.Vector; |
7 | 8 |
|
8 | 9 | public class BytecodeLoader extends ClassLoader
|
9 | 10 | {
|
10 | 11 |
|
11 |
| - public InputStream getResourceAsStream(String res) { |
12 |
| - ClassLoader classLoader = Py.getSystemState().getClassLoader(); |
13 |
| - if (classLoader != null) return classLoader.getResourceAsStream(res); |
14 |
| - |
15 |
| - classLoader = this.getClass().getClassLoader(); |
16 |
| - |
17 |
| - InputStream ret; |
18 |
| - |
19 |
| - if (classLoader != null) ret = classLoader.getResourceAsStream(res); |
20 |
| - else ret = ClassLoader.getSystemResourceAsStream(res); |
21 |
| - |
22 |
| - if (ret != null) return ret; |
23 |
| - |
24 |
| - if(res.charAt(0) == '/') res = res.substring(1); |
25 |
| - |
26 |
| - res.replace('/',File.separatorChar); |
27 |
| - |
28 |
| - PyList path = Py.getSystemState().path; |
29 |
| - for (int i=0; i < path.__len__(); i++) { |
30 |
| - String dir = path.get(i).__str__().toString(); |
31 |
| - if (dir.length() == 0) dir = null; |
32 |
| - try { |
33 |
| - return new BufferedInputStream(new FileInputStream(new File(dir,res))); |
34 |
| - } |
35 |
| - catch (IOException e) { |
36 |
| - continue; |
| 12 | + private Vector parents; |
| 13 | + |
| 14 | + public BytecodeLoader() { |
| 15 | + this(null); |
| 16 | + } |
| 17 | + |
| 18 | + public BytecodeLoader(Vector referents) { |
| 19 | + parents = new Vector(); |
| 20 | + |
| 21 | + parents.addElement(imp.getSyspathJavaLoader()); |
| 22 | + |
| 23 | + if (referents != null) { |
| 24 | + for(int i = 0; i < referents.size(); i++) { |
| 25 | + try { |
| 26 | + ClassLoader cur = ((Class)referents.elementAt(i)).getClassLoader(); |
| 27 | + if (cur != null && !parents.contains(cur)) { |
| 28 | + parents.addElement(cur); |
| 29 | + } |
| 30 | + } catch(SecurityException e) { |
| 31 | + } |
37 | 32 | }
|
38 | 33 | }
|
39 |
| - |
40 |
| - return null; |
| 34 | + |
41 | 35 | }
|
42 | 36 |
|
43 | 37 | // override from abstract base class
|
44 | 38 | protected Class loadClass(String name, boolean resolve)
|
45 | 39 | throws ClassNotFoundException
|
46 |
| - { |
47 |
| -// System.err.println("loadClass("+name+", "+resolve+")"); |
48 |
| - // First, if the Python runtime system has a default class loader, |
49 |
| - // defer to it. |
50 |
| - ClassLoader classLoader = Py.getSystemState().getClassLoader(); |
51 |
| - if (classLoader != null) |
52 |
| - return classLoader.loadClass(name); |
53 |
| - // Search the sys.path for a .class file matching the named class. |
54 |
| - // TBD: This registry option is temporary. |
55 |
| - try { |
56 |
| - return Class.forName(name); |
57 |
| - } |
58 |
| - catch(ClassNotFoundException e) { |
59 |
| - } |
60 |
| - |
| 40 | + { |
61 | 41 | Class c = findLoadedClass(name);
|
62 | 42 | if (c != null)
|
63 | 43 | return c;
|
64 | 44 |
|
65 |
| - /* previously: if |
66 |
| - Options.extendedClassLoader && |
67 |
| - // KLUDGE ALERT: without this test, running jpython |
68 |
| - // interactively from the build directory will fail with |
69 |
| - // ClassCircularityError or LinkageError. There's gotta be a |
70 |
| - // better way. |
71 |
| - !name.startsWith("org.python") |
72 |
| - */ |
73 |
| - { |
74 |
| - PyList path = Py.getSystemState().path; |
75 |
| - for (int i=0; i < path.__len__(); i++) { |
76 |
| - String dir = path.get(i).__str__().toString(); |
77 |
| - FileInputStream fis = open(dir, name); |
78 |
| - if (fis == null) |
79 |
| - continue; |
80 |
| - try { |
81 |
| - int size = fis.available(); |
82 |
| - byte[] buffer = new byte[size]; |
83 |
| - fis.read(buffer); |
84 |
| - fis.close(); |
85 |
| - return loadClassFromBytes(name, buffer); |
86 |
| - } |
87 |
| - catch (IOException e) { |
88 |
| - continue; |
89 |
| - } |
| 45 | + for (int i = 0; i < parents.size(); i++) { |
| 46 | + try { |
| 47 | + return ((ClassLoader)parents.elementAt(i)).loadClass(name); |
| 48 | + } catch(ClassNotFoundException e) { |
90 | 49 | }
|
91 | 50 | }
|
| 51 | + |
92 | 52 |
|
93 | 53 | // couldn't find the .class file on sys.path
|
94 | 54 | throw new ClassNotFoundException(name);
|
95 | 55 | }
|
96 | 56 |
|
97 |
| - private FileInputStream open(String dir, String name) { |
98 |
| - String accum = ""; |
99 |
| - boolean first = true; |
100 |
| - for (StringTokenizer t = new StringTokenizer(name, "."); |
101 |
| - t.hasMoreTokens();) |
102 |
| - { |
103 |
| - String token = t.nextToken(); |
104 |
| - if (!first) |
105 |
| - accum += File.separator; |
106 |
| - accum += token; |
107 |
| - first = false; |
108 |
| - } |
109 |
| - try { |
110 |
| - if (dir.length() == 0) |
111 |
| - dir = null; |
112 |
| - return new FileInputStream(new File(dir, accum+".class")); |
113 |
| - } |
114 |
| - catch (FileNotFoundException e) { |
115 |
| - return null; |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 | 57 | private Class loadClassFromBytes(String name, byte[] data) {
|
120 | 58 | // System.err.println("loadClassFromBytes("+name+", byte[])");
|
121 | 59 | Class c = defineClass(name, data, 0, data.length);
|
|
0 commit comments