X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FMethodGen.java;h=2dcb4e3913e987e273b23317bf41b63cc0c3d8a0;hp=88c35bbeaf1144e6262df98c2b6682fd2cfe3674;hb=83234d9a36beec5161709321fbe71b5b57b2f73f;hpb=50a344886756b00af0e30d548a7a86822ef7e962 diff --git a/src/org/ibex/classgen/MethodGen.java b/src/org/ibex/classgen/MethodGen.java index 88c35bb..2dcb4e3 100644 --- a/src/org/ibex/classgen/MethodGen.java +++ b/src/org/ibex/classgen/MethodGen.java @@ -4,20 +4,21 @@ import java.io.*; import java.util.*; /** A class representing a method in a generated classfile - @see ClassGen#addMethod */ + @see ClassFile#addMethod */ public class MethodGen implements CGConst { private final static boolean EMIT_NOPS = false; private static final int NO_CODE = -1; private static final int FINISHED = -2; - - private final CPGen cp; + + private final ClassFile owner; + private final ConstantPool cp; private final String name; private final Type ret; private final Type[] args; private final int flags; - private final ClassGen.AttrGen attrs; - private final ClassGen.AttrGen codeAttrs; + private final ClassFile.AttrGen attrs; + private final ClassFile.AttrGen codeAttrs; private final Hashtable exnTable = new Hashtable(); private final Hashtable thrownExceptions = new Hashtable(); @@ -29,8 +30,41 @@ public class MethodGen implements CGConst { private byte[] op; private Object[] arg; - MethodGen(DataInput in) { throw new Error("Brian is lame"); } - MethodGen(ClassGen owner, String name, Type ret, Type[] args, int flags) { + public String toString() { StringBuffer sb = new StringBuffer(); toString(sb, ""); return sb.toString(); } + public void toString(StringBuffer sb, String constructorName) { + sb.append(ClassFile.flagsToString(flags)); + sb.append(ret); + sb.append(" "); + + if (name.equals("")) sb.append("static "); + else { + if (name.equals("")) sb.append(constructorName); + else sb.append(name); + sb.append("("); + for(int i=0; i= 256) this.op[i] = op = LDC_W; break; default: @@ -479,7 +513,7 @@ public class MethodGen implements CGConst { } int codeSize = p; - if(codeSize >= 65536) throw new ClassGen.Exn("method too large in size"); + if(codeSize >= 65536) throw new ClassFile.Exn("method too large in size"); o.writeShort(maxStack); o.writeShort(maxLocals); @@ -501,7 +535,7 @@ public class MethodGen implements CGConst { switch(op) { case IINC: { Pair pair = (Pair) arg; - if(pair.i1 > 255 || pair.i2 < -128 || pair.i2 > 127) throw new ClassGen.Exn("overflow of iinc arg"); + if(pair.i1 > 255 || pair.i2 < -128 || pair.i2 > 127) throw new ClassFile.Exn("overflow of iinc arg"); o.writeByte(pair.i1); o.writeByte(pair.i2); break; @@ -539,7 +573,7 @@ public class MethodGen implements CGConst { if((opdata & OP_BRANCH_FLAG) != 0) { int v = pc[((Integer)arg).intValue()] - pc[i]; if(argLength == 2) { - if(v < -32768 || v > 32767) throw new ClassGen.Exn("overflow of s2 offset"); + if(v < -32768 || v > 32767) throw new ClassFile.Exn("overflow of s2 offset"); o.writeShort(v); } else if(argLength == 4) { o.writeInt(v); @@ -547,7 +581,7 @@ public class MethodGen implements CGConst { throw new Error("should never happen"); } } else if((opdata & OP_CPENT_FLAG) != 0) { - int v = cp.getIndex((CPGen.Ent)arg); + int v = cp.getIndex((ConstantPool.Ent)arg); if(argLength == 1) o.writeByte(v); else if(argLength == 2) o.writeShort(v); else throw new Error("should never happen"); @@ -556,10 +590,10 @@ public class MethodGen implements CGConst { } else { int iarg = ((Integer)arg).intValue(); if(argLength == 1) { - if(iarg < -128 || iarg >= 256) throw new ClassGen.Exn("overflow of s/u1 option"); + if(iarg < -128 || iarg >= 256) throw new ClassFile.Exn("overflow of s/u1 option"); o.writeByte(iarg); } else if(argLength == 2) { - if(iarg < -32768 || iarg >= 65536) throw new ClassGen.Exn("overflow of s/u2 option"); + if(iarg < -32768 || iarg >= 65536) throw new ClassFile.Exn("overflow of s/u2 option"); o.writeShort(iarg); } else { throw new Error("should never happen"); @@ -586,7 +620,7 @@ public class MethodGen implements CGConst { baos.reset(); o.writeShort(thrownExceptions.size()); for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();) - o.writeShort(cp.getIndex((CPGen.Ent)thrownExceptions.get(e.nextElement()))); + o.writeShort(cp.getIndex((ConstantPool.Ent)thrownExceptions.get(e.nextElement()))); attrs.add("Exceptions", baos.toByteArray()); size = capacity = FINISHED; @@ -595,7 +629,7 @@ public class MethodGen implements CGConst { void dump(DataOutput o) throws IOException { o.writeShort(flags); o.writeShort(cp.getUtf8Index(name)); - o.writeShort(cp.getUtf8Index(getDescriptor())); + o.writeShort(cp.getUtf8Index(owner.getType().method(name, ret, args).getDescriptor())); o.writeShort(attrs.size()); attrs.dump(o); }