tons of stuff
[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 final FieldGen addField(String name, Type type, int flags) {
40         FieldGen fg = new FieldGen(this,name,type,flags);
41         fields.addElement(fg);
42         return fg;
43     }
44     
45     public void dump(String s) throws IOException { dump(new File(s)); }
46     public void dump(File f) throws IOException {
47         if(f.isDirectory()) {
48             String[] a = thisType.components();
49             int i;
50             for(i=0;i<a.length-1;i++) {
51                 f = new File(f,a[i]);
52                 f.mkdir();
53             }
54             f = new File(f,a[i] + ".class");
55         }
56         dump(new FileOutputStream(f));
57     }
58     public void dump(OutputStream os) throws IOException {
59         DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
60         _dump(dos);
61         dos.flush();
62     }
63     
64     private void _dump(DataOutput o) throws IOException {
65         cp.add(thisType);
66         cp.add(superType);
67         for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
68         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
69         cp.seal();
70         
71         o.writeInt(0xcafebabe); // magic
72         // FIXME: What should this be for JDK 1.1 ?
73         o.writeShort(0); // minor_version
74         o.writeShort(46); // major_version
75         
76         o.writeShort(cp.size()); // constant_pool_count
77         cp.dump(o); // constant_pool
78         
79         o.writeShort(flags);
80         o.writeShort(cp.getIndex(thisType)); // this_class
81         o.writeShort(cp.getIndex(superType)); // super_class
82         
83         o.writeShort(interfaces.size()); // interfaces_count
84         for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
85         
86         o.writeShort(fields.size()); // fields_count
87         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
88
89         o.writeShort(methods.size()); // methods_count
90         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
91         
92         o.writeShort(attributes.size()); // attributes_count
93         attributes.dump(o); // attributes        
94     }
95     
96     // FEATURE: Make some of these checked exceptions?
97     public static class Exn extends RuntimeException {
98         public Exn(String s) { super(s); }
99     }
100
101     // FEATURE: Remove these - they are just here to be compatible with the old api
102     public final static MethodRef methodRef(Type.Object c, String name, Type ret, Type[] args) {        
103         return new MethodRef(c,name,ret,args);
104     }
105     public final static FieldRef fieldRef(Type.Object c, String name, Type type) {
106         return new FieldRef(c,name,type);
107     }
108     
109     public static class NameAndType {
110         String name;
111         String type;
112         NameAndType(String name, String type) { this.name = name; this.type = type; }
113         public boolean equals(Object o_) {
114             if(!(o_ instanceof NameAndType)) return false;
115             NameAndType o = (NameAndType) o_;
116             return o.name.equals(name) && o.type.equals(type);
117         }
118         public int hashCode() { return name.hashCode() ^ type.hashCode(); }
119     }
120     
121     public static abstract class FieldMethodRef {
122         Type.Object klass;
123         NameAndType nameAndType;
124         FieldMethodRef(Type.Object klass, NameAndType nameAndType) { this.klass = klass; this.nameAndType = nameAndType; }
125         public boolean equals(Object o_) {
126             if(!(o_ instanceof FieldMethodRef)) return false;
127             FieldMethodRef o = (FieldMethodRef) o_;
128             return o.klass.equals(klass) && o.nameAndType.equals(nameAndType);
129         }
130         public int hashCode() { return klass.hashCode() ^ nameAndType.hashCode(); }
131     }
132     
133     public static class InterfaceMethodRef extends FieldMethodRef {
134         public InterfaceMethodRef(MethodRef r) { super(r.klass,r.nameAndType); }
135         public InterfaceMethodRef(Type.Object c, NameAndType t) { super(c,t); }
136     }
137     
138     public static void main(String[] args) throws Exception {
139         Type.Object me = new Type.Object("Test");
140         ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
141         FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
142         
143         MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
144         mg.setMaxLocals(1);
145         mg.addPushConst(0);
146         //mg.add(ISTORE_0);
147         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
148         int top = mg.size();
149         mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
150         //mg.add(ILOAD_0);
151         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
152         mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
153         //mg.add(IINC,new int[]{0,1});
154         //mg.add(ILOAD_0);
155         mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
156         mg.addPushConst(1);
157         mg.add(IADD);
158         mg.add(DUP);
159         mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));       
160         mg.addPushConst(10);
161         mg.add(IF_ICMPLT,top);
162         mg.add(RETURN);
163         cg.dump("Test.class");
164     }
165 }