1 package org.ibex.classgen;
6 public class ClassGen implements CGConst {
7 private Type.Object thisType;
8 private Type.Object superType;
11 private Vector interfaces = new Vector();
12 private Vector fields = new Vector();
13 private Vector methods = new Vector();
16 private final AttrGen attributes;
18 public ClassGen(String name, String superName, int flags) {
19 this(new Type.Object(name),new Type.Object(superName),flags);
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;
30 attributes = new AttrGen(cp);
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);
39 public void dump(String s) throws IOException { dump(new File(s)); }
40 public void dump(File f) throws IOException {
42 String[] a = thisType.components();
44 for(i=0;i<a.length-1;i++) {
48 f = new File(f,a[i] + ".class");
50 dump(new FileOutputStream(f));
52 public void dump(OutputStream os) throws IOException {
53 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
58 private void _dump(DataOutput o) throws IOException {
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();
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
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
83 // FEATURE: Make some of these checked exceptions?
84 public static class Exn extends RuntimeException {
85 public Exn(String s) { super(s); }
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);
95 mg.add(GETSTATIC,mg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
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});
101 mg.add(IF_ICMPLT,top);
103 cg.dump("Test.class");