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