View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
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.Vector;
23  import java.util.Enumeration;
24  
25  /***
26   * ExceptionTable represents the exception handlers within the code
27   * of a method.
28   */
29  public class ExceptionTable {
30      /* A variable length list of ExceptionRange objects */
31      //@olsen: renamed theVector -> handlers
32      private Vector handlers = new Vector();
33  
34      /* public accessors */
35  
36      /***
37       * Return an enumeration of the exception handlers
38       * Each element in the enumeration is an ExceptionRange
39       */
40      public Enumeration handlers() {
41          return handlers.elements();
42      }
43  
44      /***
45       * Add an exception handler to the list
46       */
47      public void addElement(ExceptionRange range) {
48          handlers.addElement(range);
49      }
50  
51      public ExceptionTable() { }
52  
53      /***
54       * Compares this instance with another for structural equality.
55       */
56      //@olsen: added method
57      public boolean isEqual(Stack msg, Object obj) {
58          if (!(obj instanceof ExceptionTable)) {
59              msg.push("obj/obj.getClass() = "
60                       + (obj == null ? null : obj.getClass()));
61              msg.push("this.getClass() = "
62                       + this.getClass());
63              return false;
64          }
65          ExceptionTable other = (ExceptionTable)obj;
66  
67          if (this.handlers.size() != other.handlers.size()) {
68              msg.push("handlers.size() "
69                       + String.valueOf(other.handlers.size()));
70              msg.push("handlers.size() "
71                       + String.valueOf(this.handlers.size()));
72              return false;
73          }
74  
75          for (int i = 0; i < handlers.size(); i++) {
76              ClassAttribute h1 = (ClassAttribute)this.handlers.get(i);
77              ClassAttribute h2 = (ClassAttribute)other.handlers.get(i);
78              if (!h1.isEqual(msg, h2)) {
79                  msg.push("handlers[" + i + "] = "
80                           + String.valueOf(h2));
81                  msg.push("handlers[" + i + "] = "
82                           + String.valueOf(h1));
83                  return false;
84              }
85          }
86          return true;
87      }
88  
89     /* 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 */
90  
91      static ExceptionTable read(DataInputStream data, CodeEnv env)
92          throws IOException {
93          ExceptionTable excTable = new ExceptionTable();
94          int nExcepts = data.readUnsignedShort();
95          while (nExcepts-- > 0) {
96              excTable.addElement(ExceptionRange.read(data, env));
97          }
98          return excTable;
99      }
100 
101     void write(DataOutputStream out) throws IOException {
102         out.writeShort(handlers.size());
103         for (int i=0; i<handlers.size(); i++)
104             ((ExceptionRange) handlers.elementAt(i)).write(out);
105     }
106 
107     void print(PrintStream out, int indent) {
108         ClassPrint.spaces(out, indent);
109         out.println("Exception Table: ");
110         for (int i=0; i<handlers.size(); i++)
111             ((ExceptionRange) handlers.elementAt(i)).print(out, indent+2);
112     }
113 }