summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JEnclosingMethodAttribute.java
blob: f663f00ae1df157ff9f5c6344ff40cc64412ac9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2013 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 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.
 *
 * @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) {
        super(context, clazz);
        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; // 2 * Short.SIZE
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        stream.writeShort(classIdx);
        stream.writeShort(nameAndTypeIdx);
    }
}