01: package gnu.bytecode;
02:
03: import java.io.*;
04:
05: /**
06: * Represents the contents of a standard "Exceptions" attribute.
07: * @author Geoff Berry
08: */
09:
10: public class ExceptionsAttr extends Attribute {
11: // The exception types.
12: ClassType[] exceptions;
13: // The exception table.
14: short[] exception_table;
15:
16: /** Add a new ExceptionsAttr to a Method. */
17: public ExceptionsAttr(Method meth) {
18: super ("Exceptions");
19: addToFrontOf(meth);
20: }
21:
22: /** Set the Exceptions attribute to refer to classes whose indices
23: in the constant pool of `cl' are given by `indices'. */
24: public void setExceptions(short[] indices, ClassType cl) {
25: exception_table = indices;
26: exceptions = new ClassType[indices.length];
27: ConstantPool cp = cl.getConstants();
28: for (int i = indices.length - 1; i >= 0; --i)
29: exceptions[i] = (ClassType) ((CpoolClass) cp
30: .getPoolEntry(indices[i])).getClassType();
31: }
32:
33: /** Set the Exceptions attribute to refer to the given exception types.
34: * @param excep_types the types of the exceptions. */
35: public void setExceptions(ClassType[] excep_types) {
36: exceptions = excep_types;
37: }
38:
39: public void assignConstants(ClassType cl) {
40: super .assignConstants(cl);
41: ConstantPool cp = cl.getConstants();
42: int count = exceptions.length;
43: exception_table = new short[count];
44: for (int i = count - 1; i >= 0; --i) {
45: exception_table[i] = (short) cp.addClass(exceptions[i]).index;
46: }
47: }
48:
49: /** The size of this Attribute (in bytes) is 2 (for
50: number_of_exception) plus 2 * number_of_exceptions. */
51: public final int getLength() {
52: return 2 + 2 * (exceptions == null ? 0 : exceptions.length);
53: }
54:
55: /** The types of the exceptions in this attr. */
56: public final ClassType[] getExceptions() {
57: return exceptions;
58: }
59:
60: public void write(DataOutputStream dstr) throws java.io.IOException {
61: int count = exceptions.length;
62: dstr.writeShort(count);
63: for (int i = 0; i < count; i++) {
64: dstr.writeShort(exception_table[i]);
65: }
66: }
67:
68: public void print(ClassTypeWriter dst) {
69: dst.print("Attribute \"");
70: dst.print(getName());
71: dst.print("\", length:");
72: dst.print(getLength());
73: dst.print(", count: ");
74: int count = exceptions.length;
75: dst.println(count);
76: for (int i = 0; i < count; i++) {
77: int catch_type_index = exception_table[i] & 0xffff;
78: dst.print(" ");
79: dst.printOptionalIndex(catch_type_index);
80: dst.printConstantTersely(catch_type_index,
81: ConstantPool.CLASS);
82: dst.println();
83: }
84: }
85: }
|