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  
23  /***
24   * ExceptionRange represents a range an exception handler within
25   * a method in class file.
26   */
27  public class ExceptionRange {
28      /* The start of the exception hander (inclusive) */
29      private InsnTarget excStartPC;
30  
31      /* The end of the exception hander (exclusive) */
32      private InsnTarget excEndPC;
33  
34      /* The exception handler code */
35      private InsnTarget excHandlerPC;
36  
37      /* The exception specification */
38      private ConstClass excCatchType;
39  
40      /* public accessors */
41  
42      /***
43       * return the start of the exception hander (inclusive)
44       */
45      public InsnTarget startPC() {
46          return excStartPC;
47      }
48  
49      /***
50       * return the end of the exception hander (exclusive)
51       */
52      public InsnTarget endPC() {
53          return excEndPC;
54      }
55  
56      /***
57       * return the exception handler code
58       */
59      public InsnTarget handlerPC() {
60          return excHandlerPC;
61      }
62  
63      /*** 
64       * return the exception specification
65       * a null return value means a catch of any (try/finally)
66       */
67      public ConstClass catchType() {
68          return excCatchType;
69      }
70  
71      /***
72       * constructor 
73       */
74      public ExceptionRange(InsnTarget startPC, InsnTarget endPC,
75                            InsnTarget handlerPC, ConstClass catchType) {
76          excStartPC = startPC;
77          excEndPC = endPC;
78          excHandlerPC = handlerPC;
79          excCatchType = catchType;
80      }
81  
82      /***
83       * Compares this instance with another for structural equality.
84       */
85      //@olsen: added method
86      public boolean isEqual(Stack msg, Object obj) {
87          if (!(obj instanceof ExceptionRange)) {
88              msg.push("obj/obj.getClass() = "
89                       + (obj == null ? null : obj.getClass()));
90              msg.push("this.getClass() = "
91                       + this.getClass());
92              return false;
93          }
94          ExceptionRange other = (ExceptionRange)obj;
95  
96          if (!this.excStartPC.isEqual(msg, other.excStartPC)) {
97              msg.push(String.valueOf("excStartPC = "
98                                      + other.excStartPC));
99              msg.push(String.valueOf("excStartPC = "
100                                     + this.excStartPC));
101             return false;
102         }
103         if (!this.excEndPC.isEqual(msg, other.excEndPC)) {
104             msg.push(String.valueOf("excEndPC = "
105                                     + other.excEndPC));
106             msg.push(String.valueOf("excEndPC = "
107                                     + this.excEndPC));
108             return false;
109         }
110         if (!this.excHandlerPC.isEqual(msg, other.excHandlerPC)) {
111             msg.push(String.valueOf("excHandlerPC = "
112                                     + other.excHandlerPC));
113             msg.push(String.valueOf("excHandlerPC = "
114                                     + this.excHandlerPC));
115             return false;
116         }
117         if (!this.excCatchType.isEqual(msg, other.excCatchType)) {
118             msg.push(String.valueOf("excCatchType = "
119                                     + other.excCatchType));
120             msg.push(String.valueOf("excCatchType = "
121                                     + this.excCatchType));
122             return false;
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 ExceptionRange read(DataInputStream data, CodeEnv env)
130         throws IOException {
131         InsnTarget startPC = env.getTarget(data.readUnsignedShort());
132         InsnTarget endPC = env.getTarget(data.readUnsignedShort());
133         InsnTarget handlerPC = env.getTarget(data.readUnsignedShort());
134         ConstClass catchType =
135             (ConstClass) env.pool().constantAt(data.readUnsignedShort());
136         return new ExceptionRange(startPC, endPC, handlerPC, catchType);
137     }
138 
139     void write(DataOutputStream out) throws IOException {
140         out.writeShort(excStartPC.offset());
141         out.writeShort(excEndPC.offset());
142         out.writeShort(excHandlerPC.offset());
143         out.writeShort(excCatchType == null ? 0 : excCatchType.getIndex());
144     }
145 
146     void print(PrintStream out, int indent) {
147         ClassPrint.spaces(out, indent);
148         out.print("Exc Range:");
149         if (excCatchType == null)
150             out.print("any");
151         else
152             out.print("'" + excCatchType.asString() + "'");
153         out.print(" start = " + Integer.toString(excStartPC.offset()));
154         out.print(" end = " + Integer.toString(excEndPC.offset()));
155         out.println(" handle = " + Integer.toString(excHandlerPC.offset()));
156     }
157 }