summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JExceptionsAttribute.java
blob: b91d0f2e93da058dc6bcaa02472fc54adadc5f0d (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
89
90
/* 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;

/**
 * Exceptions attribute

 * This table is used by compilers to indicate which Exceptions a method
 * is declared to throw. See section 2.6.4 of the JVM specification.
 *
 * @author Stephane Micheloud
 * @version 1.0
 */

public class JExceptionsAttribute extends JAttribute {
    /** Constant pool of the current classfile. */
    private JConstantPool pool;

    protected int[] indexTable;
    protected int count;

    public JExceptionsAttribute(FJBGContext context,
                                JClass clazz,
                                JMethod owner) {
        super(context, clazz);
        this.pool = clazz.pool;

        this.count = 0;
        this.indexTable = new int[8]; // some size > count

        assert clazz == owner.getOwner();
    }

    public JExceptionsAttribute(FJBGContext context,
                                JClass clazz,
                                Object owner, //JMethod
                                String name,
                                int size,
                                DataInputStream stream)
        throws IOException {
        super(context, clazz, name);
        this.pool = clazz.pool;

        this.count = stream.readShort();
        this.indexTable = new int[count];
        for (int i = 0; i < count; ++i)
            indexTable[i] = stream.readShort();

        assert name.equals(getName());
    }

    public void addEntry(int classIndex) {
        if (count >= indexTable.length) {
            int[] newIT = new int[indexTable.length * 2];
            System.arraycopy(indexTable, 0, newIT, 0, indexTable.length);
            indexTable = newIT;
        }
        indexTable[count++] = classIndex;
    }

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

    // Follows javap output format for Exceptions attribute.
    /*@Override*/ public String toString() {
        StringBuffer buf = new StringBuffer("  Exceptions: ");
        for (int i = 0; i < indexTable.length; ++i) {
            buf.append("\n   throws ");
            buf.append(JClass.toExternalName(pool.lookupClass(indexTable[i])));
        }
        buf.append("\n");
        return buf.toString();
    }

    protected int getSize() {
        return 2 + indexTable.length * 2;
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        stream.writeShort(count);
        for (int i = 0; i < count; ++i)
            stream.writeShort(indexTable[i]);
    }
}