View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
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      /* The list of checked exceptions */
36      private Vector exceptionTable;
37  
38      /* public accessors */
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      //@olsen: added method
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      //@olsen: added method
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          // sort exceptions by name
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">/* package local methods *//package-summary.html">class="comment"> package local methods */
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 }