diff options
Diffstat (limited to 'src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java')
-rw-r--r-- | src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java | 63 |
1 files changed, 50 insertions, 13 deletions
diff --git a/src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java b/src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java index 44a3d551aa..153f156c55 100644 --- a/src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java +++ b/src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java @@ -1,21 +1,40 @@ +/* FJBG -- Fast Java Bytecode Generator + * Copyright 2002-2011 LAMP/EPFL + * @author Michel Schinz + */ package ch.epfl.lamp.fjbg; -import java.util.*; -import java.io.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; /** * Code attribute, containing code of methods. * - * @author Michel Schinz - * @version 1.0 + * A Code attribute contains the JVM instructions and auxiliary information + * for a single method, instance initialization method, or class or interface + * initialization method. See section 4.8.3 of the JVM specification. + * + * @author Michel Schinz, Stephane Micheloud + * @version 1.1 */ public class JCodeAttribute extends JAttribute { protected final JCode code; + protected final JMethod owner; + protected static int UNKNOWN_STACK_SIZE = Integer.MIN_VALUE; + protected final int maxStackSize; + protected final int maxLocals; public JCodeAttribute(FJBGContext context, JClass clazz, JMethod owner) { super(context, clazz); + this.owner = owner; + + this.maxStackSize = UNKNOWN_STACK_SIZE; + this.maxLocals = 0; // unknown this.code = owner.getCode(); assert clazz == owner.getOwner(); @@ -28,19 +47,18 @@ public class JCodeAttribute extends JAttribute { int size, DataInputStream stream) throws IOException { - super(context, clazz); - - stream.readShort(); // skip max stack size - stream.readShort(); // skip max locals + super(context, clazz, name); + this.owner = (JMethod)owner; + this.maxStackSize = stream.readShort(); + this.maxLocals = stream.readShort(); this.code = context.JCode(clazz, (JMethod)owner, stream); int handlersCount = stream.readShort(); for (int i = 0; i < handlersCount; ++i) code.addExceptionHandler(code.new ExceptionHandler(stream)); - List/*<JAttribute>*/ attributes = - JAttribute.readFrom(context, clazz, owner, stream); + JAttribute.readFrom(context, clazz, code, stream); Iterator attrIt = attributes.iterator(); while (attrIt.hasNext()) code.addAttribute((JAttribute)attrIt.next()); @@ -50,6 +68,26 @@ public class JCodeAttribute extends JAttribute { public String getName() { return "Code"; } + // Follows javap output format for Code attribute. + /*@Override*/ public String toString() { + StringBuffer buf = new StringBuffer(" Code:"); + buf.append("\n Stack="); + buf.append(maxStackSize); + buf.append(", Locals="); + buf.append(maxLocals); + buf.append(", Args_size="); + buf.append(owner.getArgsSize()); + buf.append(code); + buf.append("\n"); + Iterator it = code.getAttributes().iterator(); + while (it.hasNext()) { + JAttribute attr = (JAttribute)it.next(); + buf.append(attr); + buf.append("\n"); + } + return buf.toString(); + } + protected int getSize() { int handlersNum = code.getExceptionHandlers().size(); @@ -71,10 +109,10 @@ public class JCodeAttribute extends JAttribute { } protected void writeContentsTo(DataOutputStream stream) throws IOException { - List/*<JExceptionHandler>*/ handlers = code.getExceptionHandlers(); + List/*<ExceptionHandler>*/ handlers = code.getExceptionHandlers(); stream.writeShort(code.getMaxStackSize()); - stream.writeShort(code.getOwner().getMaxLocals()); + stream.writeShort(owner.getMaxLocals()); code.writeTo(stream); @@ -82,7 +120,6 @@ public class JCodeAttribute extends JAttribute { Iterator handlerIt = handlers.iterator(); while (handlerIt.hasNext()) ((JCode.ExceptionHandler)handlerIt.next()).writeTo(stream); - JAttribute.writeTo(code.getAttributes(), stream); } } |