KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > ExceptionsAttr


1 package gnu.bytecode;
2 import java.io.*;
3
4 /**
5   * Represents the contents of a standard "Exceptions" attribute.
6   * @author Geoff Berry
7   */

8
9 public class ExceptionsAttr extends Attribute
10 {
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   {
19     super("Exceptions");
20     addToFrontOf(meth);
21   }
22
23   /** Set the Exceptions attribute to refer to classes whose indices
24       in the constant pool of `cl' are given by `indices'. */

25   public void setExceptions (short[] indices, ClassType cl)
26   {
27     exception_table = indices;
28     exceptions = new ClassType[indices.length];
29     ConstantPool cp = cl.getConstants ();
30     for (int i = indices.length - 1; i >= 0; -- i)
31       exceptions[i] =
32     (ClassType)((CpoolClass)cp.getPoolEntry(indices[i])).getClassType ();
33   }
34
35   /** Set the Exceptions attribute to refer to the given exception types.
36     * @param excep_types the types of the exceptions. */

37   public void setExceptions (ClassType[] excep_types)
38   {
39     exceptions = excep_types;
40   }
41
42   public void assignConstants (ClassType cl)
43   {
44     super.assignConstants(cl);
45     ConstantPool cp = cl.getConstants();
46     int count = exceptions.length;
47     exception_table = new short[ count ];
48     for (int i = count - 1; i >= 0; --i)
49       {
50     exception_table[i] = (short)cp.addClass(exceptions[i]).index;
51       }
52   }
53
54   /** The size of this Attribute (in bytes) is 2 (for
55       number_of_exception) plus 2 * number_of_exceptions. */

56   public final int getLength()
57   {
58     return 2 + 2 * (exceptions == null ? 0 : exceptions.length);
59   }
60
61   /** The types of the exceptions in this attr. */
62   public final ClassType[] getExceptions()
63   {
64     return exceptions;
65   }
66
67   public void write (DataOutputStream dstr) throws java.io.IOException JavaDoc
68   {
69     int count = exceptions.length;
70     dstr.writeShort(count);
71     for (int i = 0; i < count; i++)
72       {
73     dstr.writeShort(exception_table[i]);
74       }
75   }
76
77   public void print (ClassTypeWriter dst)
78   {
79     dst.print("Attribute \"");
80     dst.print(getName());
81     dst.print("\", length:");
82     dst.print(getLength());
83     dst.print(", count: ");
84     int count = exceptions.length;
85     dst.println(count);
86     for (int i = 0; i < count; i++)
87       {
88     int catch_type_index = exception_table[i] & 0xffff;
89     dst.print(" ");
90     dst.printOptionalIndex(catch_type_index);
91     dst.printConstantTersely(catch_type_index, ConstantPool.CLASS);
92     dst.println();
93       }
94   }
95 }
96
Popular Tags