1 package org.ibex.classgen;
6 public class ClassGen implements CGConst {
7 private Type.Object thisType;
8 private Type.Object superType;
10 private String sourceFile;
12 private Vector interfaces = new Vector();
13 private Vector fields = new Vector();
14 private Vector methods = new Vector();
17 private final AttrGen attributes;
19 public ClassGen(String name, String superName, int flags) {
20 this(new Type.Object(name),new Type.Object(superName),flags);
23 public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
24 if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
25 throw new IllegalArgumentException("invalid flags");
26 this.thisType = thisType;
27 this.superType = superType;
31 attributes = new AttrGen(cp);
34 public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
35 MethodGen mg = new MethodGen(this,name,ret,args,flags);
36 methods.addElement(mg);
40 public final FieldGen addField(String name, Type type, int flags) {
41 FieldGen fg = new FieldGen(this,name,type,flags);
42 fields.addElement(fg);
46 public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; }
48 public void dump(String s) throws IOException { dump(new File(s)); }
49 public void dump(File f) throws IOException {
51 String[] a = thisType.components();
53 for(i=0;i<a.length-1;i++) {
57 f = new File(f,a[i] + ".class");
59 dump(new FileOutputStream(f));
62 public void dump(OutputStream os) throws IOException {
63 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os));
68 private void _dump(DataOutput o) throws IOException {
71 for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
72 if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
76 for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
80 o.writeInt(0xcafebabe); // magic
81 o.writeShort(3); // minor_version
82 o.writeShort(45); // major_version
84 o.writeShort(cp.size()); // constant_pool_count
85 cp.dump(o); // constant_pool
88 o.writeShort(cp.getIndex(thisType)); // this_class
89 o.writeShort(cp.getIndex(superType)); // super_class
91 o.writeShort(interfaces.size()); // interfaces_count
92 for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
94 o.writeShort(fields.size()); // fields_count
95 for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
97 o.writeShort(methods.size()); // methods_count
98 for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
100 o.writeShort(attributes.size()); // attributes_count
101 attributes.dump(o); // attributes
104 public static class Exn extends RuntimeException {
105 public Exn(String s) { super(s); }
108 public static abstract class FieldOrMethodRef {
113 FieldOrMethodRef(Type.Object klass, String name, String descriptor) { this.klass = klass; this.name = name; this.descriptor = descriptor; }
114 FieldOrMethodRef(FieldOrMethodRef o) { this.klass = o.klass; this.name = o.name; this.descriptor = o.descriptor; }
115 public boolean equals(Object o_) {
116 if(!(o_ instanceof FieldOrMethodRef)) return false;
117 FieldOrMethodRef o = (FieldOrMethodRef) o_;
118 return o.klass.equals(klass) && o.name.equals(name) && o.descriptor.equals(descriptor);
120 public int hashCode() { return klass.hashCode() ^ name.hashCode() ^ descriptor.hashCode(); }
123 static class AttrGen {
124 private final CPGen cp;
125 private final Hashtable ht = new Hashtable();
127 public AttrGen(CPGen cp) {
131 public void add(String s, Object data) {
136 public boolean contains(String s) { return ht.get(s) != null; }
138 public int size() { return ht.size(); }
140 public void dump(DataOutput o) throws IOException {
141 for(Enumeration e = ht.keys(); e.hasMoreElements();) {
142 String name = (String) e.nextElement();
143 Object val = ht.get(name);
144 o.writeShort(cp.getUtf8Index(name));
145 if(val instanceof byte[]) {
146 byte[] buf = (byte[]) val;
147 o.writeInt(buf.length);
149 } else if(val instanceof CPGen.Ent) {
151 o.writeShort(((CPGen.Ent)val).getIndex());
153 throw new Error("should never happen");
159 /*public static void main(String[] args) throws Exception {
160 Type.Object me = new Type.Object("Test");
161 ClassGen cg = new ClassGen("Test","java.lang.Object",ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
162 FieldGen fg = cg.addField("foo",Type.INT,ACC_PUBLIC|ACC_STATIC);
164 MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
168 mg.add(PUTSTATIC, fieldRef(me,"foo",Type.INT));
170 mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
172 mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
173 mg.add(INVOKEVIRTUAL,cg.methodRef(new Type.Object("java.io.PrintStream"),"println",Type.VOID, new Type[]{Type.INT}));
174 //mg.add(IINC,new int[]{0,1});
176 mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
180 mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
182 mg.add(IF_ICMPLT,top);
184 cg.dump("Test.class");