1 3 package org.python.core; 4 import java.io.*; 5 import java.util.StringTokenizer ; 6 import java.util.Hashtable ; 7 import java.util.Vector ; 8 9 13 public class BytecodeLoader { 14 15 static Vector init() { 16 Vector parents = new Vector (); 17 parents.addElement(imp.getSyspathJavaLoader()); 18 return parents; 19 } 20 21 22 static Class findParentClass(Vector parents, String name) 23 throws ClassNotFoundException 24 { 25 for (int i = 0; i < parents.size(); i++) { 26 try { 27 return ((ClassLoader )parents.elementAt(i)).loadClass(name); 28 } catch(ClassNotFoundException e) { } 29 } 30 throw new ClassNotFoundException (name); 32 } 33 34 static void compileClass(Class c) { 35 if (!Options.skipCompile) { 40 Compiler.compileClass(c); 42 } 43 } 44 45 46 private static Class loaderClass = null; 47 48 private static Loader makeLoader() { 49 if (loaderClass == null) { 50 synchronized (BytecodeLoader.class) { 51 String 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 e) { 57 loaderClass = BytecodeLoader1.class; 58 } 59 } else 60 loaderClass = BytecodeLoader1.class; 61 } 62 } 63 try { 64 return (Loader) loaderClass.newInstance(); 65 } catch (Exception e) { 66 return new BytecodeLoader1(); 67 } 68 } 69 70 77 public static Class makeClass(String name, Vector 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 cls = (Class )referents.elementAt(i); 86 ClassLoader cur = cls.getClassLoader(); 87 if (cur != null) 88 loader.addParent(cur); 89 } catch(SecurityException e) { } 90 } 91 } 92 return loader.loadClassFromBytes(name, data); 93 } 94 95 101 public static PyCode makeCode(String name, byte[] data) { 102 try { 103 Class c = makeClass(name, null, data); 104 return ((PyRunnable)c.newInstance()).getMain(); 105 } 106 catch (Exception e) { 107 throw Py.JavaError(e); 108 } 109 } 110 } 111 | Popular Tags |