KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.core;
4 import java.io.*;
5 import java.util.StringTokenizer JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 /**
10  * Utility class for loading of compiled python modules and
11  * java classes defined in python modules.
12  */

13 public class BytecodeLoader {
14
15     static Vector JavaDoc init() {
16         Vector JavaDoc parents = new Vector JavaDoc();
17         parents.addElement(imp.getSyspathJavaLoader());
18         return parents;
19     }
20
21
22     static Class JavaDoc findParentClass(Vector JavaDoc parents, String JavaDoc name)
23         throws ClassNotFoundException JavaDoc
24     {
25         for (int i = 0; i < parents.size(); i++) {
26             try {
27                 return ((ClassLoader JavaDoc)parents.elementAt(i)).loadClass(name);
28             } catch(ClassNotFoundException JavaDoc e) { }
29         }
30         // couldn't find the .class file on sys.path
31
throw new ClassNotFoundException JavaDoc(name);
32     }
33
34     static void compileClass(Class JavaDoc c) {
35         // This method has caused much trouble. Using it breaks jdk1.2rc1
36
// Not using it can make SUN's jdk1.1.6 JIT slightly unhappy.
37
// Don't use by default, but allow python.options.compileClass to
38
// override
39
if (!Options.skipCompile) {
40             // System.err.println("compile: "+name);
41
Compiler.compileClass(c);
42         }
43     }
44
45
46     private static Class JavaDoc loaderClass = null;
47
48     private static Loader makeLoader() {
49         if (loaderClass == null) {
50             synchronized (BytecodeLoader.class) {
51                 String JavaDoc version = System.getProperty("java.version");
52                 if (version.compareTo("1.2") >= 0) {
53                     try {
54                         loaderClass =
55                             Class.forName("org.python.core.BytecodeLoader2");
56                     } catch (Throwable JavaDoc e) {
57                         loaderClass = BytecodeLoader1.class;
58                     }
59                 } else
60                     loaderClass = BytecodeLoader1.class;
61             }
62         }
63         try {
64             return (Loader) loaderClass.newInstance();
65         } catch (Exception JavaDoc e) {
66             return new BytecodeLoader1();
67         }
68     }
69
70     /**
71      * Turn the java byte code in data into a java class.
72      * @param name the name of the class
73      * @param referents a list of superclass and interfaces that
74      * the new class will reference.
75      * @param data the java byte code.
76      */

77     public static Class JavaDoc makeClass(String JavaDoc name, Vector JavaDoc referents,
78                                   byte[] data)
79     {
80         Loader loader = makeLoader();
81
82         if (referents != null) {
83             for (int i = 0; i < referents.size(); i++) {
84                 try {
85                     Class JavaDoc cls = (Class JavaDoc)referents.elementAt(i);
86                     ClassLoader JavaDoc cur = cls.getClassLoader();
87                     if (cur != null)
88                          loader.addParent(cur);
89                 } catch(SecurityException JavaDoc e) { }
90             }
91         }
92         return loader.loadClassFromBytes(name, data);
93     }
94
95     /**
96      * Turn the java byte code for a compiled python module into a
97      * java class.
98      * @param name the name of the class
99      * @param data the java byte code.
100      */

101     public static PyCode makeCode(String JavaDoc name, byte[] data) {
102         try {
103             Class JavaDoc c = makeClass(name, null, data);
104             return ((PyRunnable)c.newInstance()).getMain();
105         }
106         catch (Exception JavaDoc e) {
107             throw Py.JavaError(e);
108         }
109     }
110 }
111
Popular Tags