sort of working
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassGen.java
1 package org.ibex.classgen;
2
3 import java.util.*;
4 import java.io.*;
5
6 public class ClassGen implements CGConst {
7     private Type.Object thisType;
8     private Type.Object superType;
9     int flags;
10     
11     private Vector interfaces = new Vector();
12     private Vector fields = new Vector();
13     private Vector methods = new Vector();
14     
15     final CPGen cp;
16     private final AttrGen attributes;
17     
18     public ClassGen(String name, String superName, int flags) {
19         this(new Type.Object(name),new Type.Object(superName),flags);
20     }
21     
22     public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
23         if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
24             throw new IllegalArgumentException("invalid flags");
25         this.thisType = thisType;
26         this.superType = superType;
27         this.flags = flags;
28         
29         cp = new CPGen();
30         attributes = new AttrGen(cp);
31     }
32     
33     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
34         MethodGen mg = new MethodGen(this,name,ret,args,flags);
35         methods.addElement(mg);
36         return mg;
37     }
38     
39     public void dump(String s) throws IOException { dump(new File(s)); }
40     public void dump(File f) throws IOException {
41         if(f.isDirectory()) {
42             String[] a = thisType.components();
43             int i;
44             for(i=0;i<a.length-1;i++) {
45                 f = new File(f,a[i]);
46                 f.mkdir();
47             }
48             f = new File(f,a[i] + ".class");
49         }
50         dump(new FileOutputStream(f));
51     }
52     public void dump(OutputStream os) throws IOException {
53         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
54         _dump(dos);
55         dos.flush();
56     }
57     
58     private void _dump(DataOutput o) throws IOException {
59         cp.add(thisType);
60         cp.add(superType);
61         for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
62         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
63         cp.seal();
64         
65         o.writeInt(0xcafebabe); // magic
66         o.writeShort(0); // minor_version
67         o.writeShort(46); // major_version
68         o.writeShort(cp.size()); // constant_pool_count
69         cp.dump(o); // constant_pool
70         o.writeShort(flags);
71         o.writeShort(cp.get(thisType).index); // this_class
72         o.writeShort(cp.get(superType).index); // super_class
73         o.writeShort(interfaces.size()); // interfaces_count
74         for(int i=0;i<interfaces.size();i++) o.writeShort(cp.get((Type.Object)interfaces.elementAt(i)).index); // interfaces
75         o.writeShort(fields.size()); // fields_count
76         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
77         o.writeShort(methods.size()); // methods_count
78         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
79         o.writeShort(attributes.size()); // attributes_count
80         attributes.dump(o); // attributes        
81     }
82     
83     // FEATURE: Make some of these checked exceptions?
84     public static class Exn extends RuntimeException {
85         public Exn(String s) { super(s); }
86     }
87
88     public static void main(String[] args) throws Exception {
89         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
90         MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
91         mg.setMaxLocals(1);
92         mg.addPushConst(0);
93         mg.add(ISTORE_0);
94         int top = mg.size();
95         mg.add(GETSTATIC,mg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
96         mg.add(ILOAD_0);
97         mg.add(INVOKEVIRTUAL,mg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
98         mg.add(IINC,new int[]{0,1});
99         mg.add(ILOAD_0);
100         mg.addPushConst(10);
101         mg.add(IF_ICMPLT,top);
102         mg.add(RETURN);
103         cg.dump("Test.class");
104     }
105 }