1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jdo.impl.enhancer.classfile;
19
20 import java.io.*;
21 import java.util.Stack;
22 import java.util.Arrays;
23 import java.util.Comparator;
24 import java.util.Vector;
25 import java.util.Enumeration;
26
27 /***
28 * ExceptionsAttribute represents a method attribute in a class file
29 * listing the checked exceptions for the method.
30 */
31 public class ExceptionsAttribute extends ClassAttribute {
32 public final static String expectedAttrName = "Exceptions";
33
34
35 private Vector exceptionTable;
36
37
38
39 /***
40 * Return an enumeration of the checked exceptions
41 */
42 public Enumeration exceptions() {
43 return exceptionTable.elements();
44 }
45
46 /***
47 * Returns the vector of the checked exceptions.
48 */
49
50 public Vector getExceptions() {
51 return exceptionTable;
52 }
53
54 /***
55 * Constructor
56 */
57 public ExceptionsAttribute(ConstUtf8 attrName, Vector excTable) {
58 super(attrName);
59 exceptionTable = excTable;
60 }
61
62 /***
63 * Convenience Constructor - for single exception
64 */
65 public ExceptionsAttribute(ConstUtf8 attrName, ConstClass exc) {
66 super(attrName);
67 exceptionTable = new Vector(1);
68 exceptionTable.addElement(exc);
69 }
70
71 /***
72 * Compares this instance with another for structural equality.
73 */
74
75 public boolean isEqual(Stack msg, Object obj) {
76 if (!(obj instanceof ExceptionsAttribute)) {
77 msg.push("obj/obj.getClass() = "
78 + (obj == null ? null : obj.getClass()));
79 msg.push("this.getClass() = "
80 + this.getClass());
81 return false;
82 }
83 ExceptionsAttribute other = (ExceptionsAttribute)obj;
84
85 if (!super.isEqual(msg, other)) {
86 return false;
87 }
88
89 if (this.exceptionTable.size() != other.exceptionTable.size()) {
90 msg.push("exceptionTable.size() "
91 + String.valueOf(other.exceptionTable.size()));
92 msg.push("exceptionTable.size() "
93 + String.valueOf(this.exceptionTable.size()));
94 return false;
95 }
96
97
98 class ConstClassComparator implements Comparator {
99 public int compare(Object o1, Object o2) {
100 ConstClass c1 = (ConstClass)o1;
101 ConstClass c2 = (ConstClass)o2;
102 String s1 = c1.className().asString();
103 String s2 = c2.className().asString();
104 return s1.compareTo(s2);
105 }
106 }
107 ConstClassComparator comparator = new ConstClassComparator();
108 ConstClass[] thisExceptionTable
109 = (ConstClass[])this.exceptionTable.toArray(new ConstClass[0]);
110 ConstClass[] otherExceptionTable
111 = (ConstClass[])other.exceptionTable.toArray(new ConstClass[0]);
112 Arrays.sort(thisExceptionTable, comparator);
113 Arrays.sort(otherExceptionTable, comparator);
114 for (int i = 0; i < exceptionTable.size(); i++) {
115 ConstClass c1 = thisExceptionTable[i];
116 ConstClass c2 = otherExceptionTable[i];
117 if (!c1.isEqual(msg, c2)) {
118 msg.push("exceptionTable[i] = " + String.valueOf(c2));
119 msg.push("exceptionTable[i] = " + String.valueOf(c1));
120 return false;
121 }
122 }
123 return true;
124 }
125
126 /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">
127
128 static ExceptionsAttribute read(ConstUtf8 attrName,
129 DataInputStream data, ConstantPool pool)
130 throws IOException {
131 int nExcepts = data.readUnsignedShort();
132 Vector excTable = new Vector();
133 while (nExcepts-- > 0) {
134 int excIndex = data.readUnsignedShort();
135 ConstClass exc_class = null;
136 if (excIndex != 0)
137 exc_class = (ConstClass) pool.constantAt(excIndex);
138 excTable.addElement(exc_class);
139 }
140
141 return new ExceptionsAttribute(attrName, excTable);
142 }
143
144 void write(DataOutputStream out) throws IOException {
145 out.writeShort(attrName().getIndex());
146 out.writeInt(2+2*exceptionTable.size());
147 out.writeShort(exceptionTable.size());
148 for (int i=0; i<exceptionTable.size(); i++)
149 out.writeShort(((ConstClass) exceptionTable.elementAt(i)).getIndex());
150 }
151
152 void print(PrintStream out, int indent) {
153 ClassPrint.spaces(out, indent);
154 out.print("Exceptions:");
155 for (int i=0; i<exceptionTable.size(); i++)
156 out.print(" " + ((ConstClass) exceptionTable.elementAt(i)).asString());
157 out.println();
158 }
159
160 }