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.Vector;
24  import java.util.Enumeration;
25  
26  /***
27   * ExceptionTable represents the exception handlers within the code
28   * of a method.
29   */
30  public class ExceptionTable {
31      /* A variable length list of ExceptionRange objects */
32      //@olsen: renamed theVector -> handlers
33      private Vector handlers = new Vector();
34  
35      /* public accessors */
36  
37      /***
38       * Return an enumeration of the exception handlers
39       * Each element in the enumeration is an ExceptionRange
40       */
41      public Enumeration handlers() {
42          return handlers.elements();
43      }
44  
45      /***
46       * Add an exception handler to the list
47       */
48      public void addElement(ExceptionRange range) {
49          handlers.addElement(range);
50      }
51  
52      public ExceptionTable() { }
53  
54      /***
55       * Compares this instance with another for structural equality.
56       */
57      //@olsen: added method
58      public boolean isEqual(Stack msg, Object obj) {
59          if (!(obj instanceof ExceptionTable)) {
60              msg.push("obj/obj.getClass() = "
61                       + (obj == null ? null : obj.getClass()));
62              msg.push("this.getClass() = "
63                       + this.getClass());
64              return false;
65          }
66          ExceptionTable other = (ExceptionTable)obj;
67  
68          if (this.handlers.size() != other.handlers.size()) {
69              msg.push("handlers.size() "
70                       + String.valueOf(other.handlers.size()));
71              msg.push("handlers.size() "
72                       + String.valueOf(this.handlers.size()));
73              return false;
74          }
75  
76          for (int i = 0; i < handlers.size(); i++) {
77              ClassAttribute h1 = (ClassAttribute)this.handlers.get(i);
78              ClassAttribute h2 = (ClassAttribute)other.handlers.get(i);
79              if (!h1.isEqual(msg, h2)) {
80                  msg.push("handlers[" + i + "] = "
81                           + String.valueOf(h2));
82                  msg.push("handlers[" + i + "] = "
83                           + String.valueOf(h1));
84                  return false;
85              }
86          }
87          return true;
88      }
89  
90     /* 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 */
91  
92      static ExceptionTable read(DataInputStream data, CodeEnv env)
93          throws IOException {
94          ExceptionTable excTable = new ExceptionTable();
95          int nExcepts = data.readUnsignedShort();
96          while (nExcepts-- > 0) {
97              excTable.addElement(ExceptionRange.read(data, env));
98          }
99          return excTable;
100     }
101 
102     void write(DataOutputStream out) throws IOException {
103         out.writeShort(handlers.size());
104         for (int i=0; i<handlers.size(); i++)
105             ((ExceptionRange) handlers.elementAt(i)).write(out);
106     }
107 
108     void print(PrintStream out, int indent) {
109         ClassPrint.spaces(out, indent);
110         out.println("Exception Table: ");
111         for (int i=0; i<handlers.size(); i++)
112             ((ExceptionRange) handlers.elementAt(i)).print(out, indent+2);
113     }
114 }