X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FMethodGen.java;h=bc80d75d731129bb116f3cc7156a95065318fb0b;hb=0b535cb6b45f0d8a1d43eee75a3fe187ed383ef8;hp=7c740d11675fc39314c26663be10c1ae2c75d237;hpb=db68756051a8b3c9a08f7c2a27558713b263ad23;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/MethodGen.java b/src/org/ibex/classgen/MethodGen.java index 7c740d1..bc80d75 100644 --- a/src/org/ibex/classgen/MethodGen.java +++ b/src/org/ibex/classgen/MethodGen.java @@ -5,14 +5,12 @@ import java.util.*; /** A class representing a method in a generated classfile @see ClassFile#addMethod */ -public class MethodGen implements CGConst { +public class MethodGen extends Type.Class.Method.Body { private final static boolean EMIT_NOPS = false; private static final int NO_CODE = -1; public final Type.Class.Method method; - private final int flags; - private final ClassFile.AttrGen attrs; private final ClassFile.AttrGen codeAttrs; private final Vector exnTable = new Vector(); private final Hashtable thrownExceptions = new Hashtable(); @@ -30,26 +28,24 @@ public class MethodGen implements CGConst { // Constructors ////////////////////////////////////////////////////////////////////////////// MethodGen(Type.Class.Method method, int flags) { - if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags"); + method.super(flags, new ClassFile.AttrGen()); this.method = method; - this.flags = flags; - - attrs = new ClassFile.AttrGen(); codeAttrs = new ClassFile.AttrGen(); - - if (((flags & INTERFACE) != 0) || (flags & (ABSTRACT|NATIVE)) != 0) size = capacity = -1; - + if (!isConcrete()) size = capacity = -1; maxLocals = Math.max(method.getNumArgs() + (flags&STATIC)==0 ? 1 : 0, 4); } MethodGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException { - this.flags = in.readShort(); - if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new ClassFile.ClassReadExn("invalid flags"); - String name = cp.getUtf8KeyByIndex(in.readShort()); - this.method = c.method(name+cp.getUtf8KeyByIndex(in.readShort())); - this.attrs = new ClassFile.AttrGen(in,cp); + this(in.readShort(), cp.getUtf8KeyByIndex(in.readShort()), c, in, cp); } + + private MethodGen(short flags, String name, Type.Class c, DataInput in, ConstantPool cp) throws IOException { + this(flags, name, c.method(name+cp.getUtf8KeyByIndex(in.readShort())), c, in, cp); } + private MethodGen(short flags, String name, Type.Class.Method m, + Type.Class c, DataInput in, ConstantPool cp) throws IOException { + m.super(flags, new ClassFile.AttrGen(in,cp)); + this.method = m; - if ((flags & (NATIVE|ABSTRACT))==0) { + if (isConcrete()) { byte[] codeAttr = (byte[]) attrs.get("Code"); if (codeAttr == null) throw new ClassFile.ClassReadExn("code attr expected"); DataInputStream ci = new DataInputStream(new ByteArrayInputStream(codeAttr)); @@ -317,6 +313,9 @@ public class MethodGen implements CGConst { // Accessors ////////////////////////////////////////////////////////////////////////////// + public int getFlags() { return flags; } + public Hashtable getThrownExceptions() { return thrownExceptions; } + /** Returns the size (in instructions) of this method @return The size of the method (in instructions) */ @@ -569,7 +568,7 @@ public class MethodGen implements CGConst { */ void finish(ConstantPool cp) { cp.addUtf8(method.name); - cp.addUtf8(method.getDescriptor()); + cp.addUtf8(method.getTypeDescriptor()); for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();) cp.add(e.nextElement()); @@ -891,7 +890,7 @@ public class MethodGen implements CGConst { o.writeShort(flags); o.writeShort(cp.getUtf8Index(method.name)); - o.writeShort(cp.getUtf8Index(method.getDescriptor())); + o.writeShort(cp.getUtf8Index(method.getTypeDescriptor())); attrs.dump(o,cp); } @@ -944,7 +943,7 @@ public class MethodGen implements CGConst { // Debugging ////////////////////////////////////////////////////////////////////////////// - public void debugToString(StringBuffer sb, String constructorName) { + public void debugBodyToString(StringBuffer sb) { // This is intentionally a local variable so it can be removed by gcclass final String[] OP_NAMES = new String[]{ "nop", "aconst_null", "iconst_m1", "iconst_0", "iconst_1", "iconst_2", @@ -991,34 +990,18 @@ public class MethodGen implements CGConst { "", "", "", "", "", "", "", "", "", "" }; - - sb.append(" ").append(ClassFile.flagsToString(flags,false)); - sb.append(method.debugToString()); - if (thrownExceptions.size() > 0) { - sb.append("throws"); - for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();) - sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(","); - sb.setLength(sb.length()-1); - sb.append(" "); - } - if ((flags & (NATIVE|ABSTRACT))==0) { - sb.append("{\n"); - for(int i=0;i