summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JCodeAttribute.java
blob: 44a3d551aa1b6bac563edb179b7ec65fa41fc010 (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
84
85
86
87
88
package ch.epfl.lamp.fjbg;

import java.util.*;
import java.io.*;

/**
 * Code attribute, containing code of methods.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JCodeAttribute extends JAttribute {
    protected final JCode code;

    public JCodeAttribute(FJBGContext context, JClass clazz, JMethod owner) {
        super(context, clazz);
        this.code = owner.getCode();

        assert clazz == owner.getOwner();
    }

    public JCodeAttribute(FJBGContext context,
                          JClass clazz,
                          Object owner,
                          String name,
                          int size,
                          DataInputStream stream)
        throws IOException {
        super(context, clazz);

        stream.readShort();     // skip max stack size
        stream.readShort();     // skip max locals

        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);
        Iterator attrIt = attributes.iterator();
        while (attrIt.hasNext())
            code.addAttribute((JAttribute)attrIt.next());

        assert name.equals(getName());
    }

    public String getName() { return "Code"; }

    protected int getSize() {
        int handlersNum = code.getExceptionHandlers().size();

        int attrsSize = 0;
        Iterator attrsIt = code.getAttributes().iterator();
        while (attrsIt.hasNext()) {
            JAttribute attr = (JAttribute)attrsIt.next();
            attrsSize += attr.getSize() + 6;
        }

        return 2                // max stack
            + 2                 // max locals
            + 4                 // code size
            + code.getSize()    // code
            + 2                 // exception table size
            + 8 * handlersNum   // exception table
            + 2                 // attributes count
            + attrsSize;        // attributes
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        List/*<JExceptionHandler>*/ handlers = code.getExceptionHandlers();

        stream.writeShort(code.getMaxStackSize());
        stream.writeShort(code.getOwner().getMaxLocals());

        code.writeTo(stream);

        stream.writeShort(handlers.size());
        Iterator handlerIt = handlers.iterator();
        while (handlerIt.hasNext())
            ((JCode.ExceptionHandler)handlerIt.next()).writeTo(stream);

        JAttribute.writeTo(code.getAttributes(), stream);
    }
}