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