imported brians code
[nestedvm.git] / src / org / xwt / mips / ClassLoader.java
1 package org.xwt.mips;
2
3 import java.io.*;
4
5 // FEATURE: This is just a quick hack, it is really ugly and broken
6
7 public class ClassLoader extends java.lang.ClassLoader {
8     public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
9         Class c;
10         if(name.startsWith("mips.")) {
11             String path = name.substring(5).replace('.','/') + ".mips";
12             try {
13                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
14                 new ClassFileCompiler(path,name,bos).go();
15                 bos.close();
16                 byte[] buf = bos.toByteArray();
17                 c = defineClass(name,buf,0,buf.length);
18             } catch(IOException e) {
19                 throw new ClassNotFoundException(name);
20             } catch(Compiler.Exn e) {
21                 throw new ClassNotFoundException(e.getMessage());
22             }
23         } else {
24             c = findSystemClass(name);
25         }
26             
27         if(c == null) throw new ClassNotFoundException(name);
28         if(resolve) resolveClass(c);
29         return c;
30     }
31     
32     public Class classFromBinary(String path) throws ClassNotFoundException {
33         if(!path.endsWith(".mips")) throw new IllegalArgumentException("isn't a .mips");
34         return loadClass("mips." + path.substring(0,path.length()-5).replace('/','.'));
35     }
36     
37     public Runtime runtimeFromBinary(String path) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
38         return (Runtime) classFromBinary(path).newInstance();
39     }
40     
41     public static void main(String[] args) throws Exception {
42         System.exit(new ClassLoader().runtimeFromBinary(args[0]).run(args));
43     }
44 }