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 // FIXME: What should this be for JDK 1.1 ?
73 o.writeShort(0); // minor_version
74 o.writeShort(46); // major_version
76 o.writeShort(cp.size()); // constant_pool_count
77 cp.dump(o); // constant_pool
80 o.writeShort(cp.getIndex(thisType)); // this_class
81 o.writeShort(cp.getIndex(superType)); // super_class
83 o.writeShort(interfaces.size()); // interfaces_count
84 for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
86 o.writeShort(fields.size()); // fields_count
87 for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
89 o.writeShort(methods.size()); // methods_count
90 for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o); // methods
92 o.writeShort(attributes.size()); // attributes_count
93 attributes.dump(o); // attributes
96 // FEATURE: Make some of these checked exceptions?
97 public static class Exn extends RuntimeException {
98 public Exn(String s) { super(s); }
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);
105 public final static FieldRef fieldRef(Type.Object c, String name, Type type) {
106 return new FieldRef(c,name,type);
109 public static class NameAndType {
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);
118 public int hashCode() { return name.hashCode() ^ type.hashCode(); }
121 public static abstract class FieldMethodRef {
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);
130 public int hashCode() { return klass.hashCode() ^ nameAndType.hashCode(); }
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); }
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);
143 MethodGen mg = cg.addMethod("main",Type.VOID,new Type[]{Type.arrayType(Type.STRING)},ACC_STATIC|ACC_PUBLIC);
147 mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
149 mg.add(GETSTATIC,cg.fieldRef(new Type.Object("java.lang.System"),"out",new Type.Object("java.io.PrintStream")));
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});
155 mg.add(GETSTATIC,cg.fieldRef(me,"foo",Type.INT));
159 mg.add(PUTSTATIC,cg.fieldRef(me,"foo",Type.INT));
161 mg.add(IF_ICMPLT,top);
163 cg.dump("Test.class");