diff options
Diffstat (limited to 'src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java')
-rw-r--r-- | src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java b/src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java index 4351e2fc7b..5536a3bbcd 100644 --- a/src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java +++ b/src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java @@ -1,3 +1,7 @@ +/* FJBG -- Fast Java Bytecode Generator + * Copyright 2002-2011 LAMP/EPFL + * @author Michel Schinz + */ package ch.epfl.lamp.fjbg; @@ -6,30 +10,70 @@ import java.io.DataOutputStream; import java.io.IOException; /** - * Sourcefile attribute, which can be attached to class files to - * associate them with their enclosing method. A class may have - * at most one EclosingMethod attribute. + * EclosingMethod attribute + + * A class must have an EnclosingMethod attribute if and only if it is a + * local class or an anonymous class. A class may have no more than one + * EnclosingMethod attribute. See section 4.8.6 of the JVM specification. * - * @version 1.0 * @author Michel Schinz + * @version 1.0 */ public class JEnclosingMethodAttribute extends JAttribute { + /** Constant pool of the current classfile. */ + private JConstantPool pool; + protected final int classIdx; protected final int nameAndTypeIdx; public JEnclosingMethodAttribute(FJBGContext context, JClass clazz, - String className, String methodName, JType methodType) { + String className, + String methodName, + JType methodType) { super(context, clazz); - this.classIdx = clazz.getConstantPool().addClass(className); - this.nameAndTypeIdx = clazz.getConstantPool().addNameAndType(methodName, methodType.getSignature()); + this.pool = clazz.pool; + + this.classIdx = pool.addClass(className); + this.nameAndTypeIdx = pool.addNameAndType(methodName, methodType.getSignature()); + } + + public JEnclosingMethodAttribute(FJBGContext context, + JClass clazz, + Object owner, + String name, + int size, + DataInputStream stream) + throws IOException { + super(context, clazz, name); + this.pool = clazz.pool; + + this.classIdx = stream.readShort(); + this.nameAndTypeIdx = stream.readShort(); + + assert name.equals(getName()); } public String getName() { return "EnclosingMethod"; } + // Follows javap output format for EnclosingMethod attribute. + /*@Override*/ public String toString() { + StringBuffer buf = new StringBuffer(" EnclosingMethod:"); + buf.append("\n #"); + buf.append(classIdx); + if (nameAndTypeIdx != 0) { + buf.append(" of #"); + buf.append(nameAndTypeIdx); + } + buf.append(";\t// "); + buf.append(pool.lookupEntry(classIdx)); + buf.append("\n"); + return buf.toString(); + } + protected int getSize() { - return 4; + return 4; // 2 * Short.SIZE } protected void writeContentsTo(DataOutputStream stream) throws IOException { |