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 final FieldGen addField(String name, Type type, int flags) {
40 FieldGen fg = new FieldGen(this,name,type,flags);
41 fields.addElement(fg);
45 public void dump(String s) throws IOException { dump(new File(s)); }
46 public void dump(File f) throws IOException {
48 String[] a = thisType.components();
50 for(i=0;i<a.length-1;i++) {
54 f = new File(f,a[i] + ".class");
56 dump(new FileOutputStream(f));
58 public void dump(OutputStream os) throws IOException {
59 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
64 private void _dump(DataOutput o) throws IOException {
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();
71 o.writeInt(0xcafebabe); // magic
72 o.writeShort(3); // minor_version
73 o.writeShort(45); // major_version
75 o.writeShort(cp.size()); // constant_pool_count
76 cp.dump(o); // constant_pool
79 o.writeShort(cp.getIndex(thisType)); // this_class
80 o.writeShort(cp.getIndex(superType)); // super_class
82 o.writeShort(interfaces.size()); // interfaces_count
83 for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
85 o.writeShort(fields.size()); // fields_count
86 for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
88 o.writeShort(methods.size()); // methods_count
89 for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
91 o.writeShort(attributes.size()); // attributes_count
92 attributes.dump(o); // attributes
95 // FEATURE: Make some of these checked exceptions?
96 public static class Exn extends RuntimeException {
97 public Exn(String s) { super(s); }
100 // FEATURE: Remove these - they are just here to be compatible with the old api
101 public final static MethodRef methodRef(Type.Object c, String name, Type ret, Type[] args) {
102 return new MethodRef(c,name,ret,args);
104 public final static FieldRef fieldRef(Type.Object c, String name, Type type) {
105 return new FieldRef(c,name,type);
108 public static class NameAndType {
111 NameAndType(String name, String type) { this.name = name; this.type = type; }
112 public boolean equals(Object o_) {
113 if(!(o_ instanceof NameAndType)) return false;
114 NameAndType o = (NameAndType) o_;
115 return o.name.equals(name) && o.type.equals(type);
117 public int hashCode() { return name.hashCode() ^ type.hashCode(); }
120 public static abstract class FieldMethodRef {
122 NameAndType nameAndType;
123 FieldMethodRef(Type.Object klass, NameAndType nameAndType) { this.klass = klass; this.nameAndType = nameAndType; }
124 public boolean equals(Object o_) {
125 if(!(o_ instanceof FieldMethodRef)) return false;
126 FieldMethodRef o = (FieldMethodRef) o_;
127 return o.klass.equals(klass) && o.nameAndType.equals(nameAndType);
129 public int hashCode() { return klass.hashCode() ^ nameAndType.hashCode(); }
132 public static class InterfaceMethodRef extends FieldMethodRef {
133 public InterfaceMethodRef(MethodRef r) { super(r.klass,r.nameAndType); }
134 public InterfaceMethodRef(Type.Object c, NameAndType t) { super(c,t); }
137 public static void main(String[] args) throws Exception {
138 Type.Object me = new Type.Object("Test");
139 ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
140 FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
142 MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
146 mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
148 mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
150 mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
151 mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
152 //mg.add(IINC,new int[]{0,1});
154 mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
158 mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
160 mg.add(IF_ICMPLT,top);
162 cg.dump("Test.class");