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 package org.apache.bcel.classfile;
19
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.io.Serializable;
24
25 import org.apache.bcel.Constants;
26
27 /**
28 * This class represents an entry in the exception table of the <em>Code</em>
29 * attribute and is used only there. It contains a range in which a
30 * particular exception handler is active.
31 *
32 * @version $Id: CodeException.java 1152072 2011-07-29 01:54:05Z dbrosius $
33 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
34 * @see Code
35 */
36 public final class CodeException implements Cloneable, Constants, Node, Serializable {
37
38 private static final long serialVersionUID = 2972500041254967221L;
39 private int start_pc; // Range in the code the exception handler is
40 private int end_pc; // active. start_pc is inclusive, end_pc exclusive
41 private int handler_pc; /* Starting address of exception handler, i.e.,
42 * an offset from start of code.
43 */
44 private int catch_type; /* If this is zero the handler catches any
45 * exception, otherwise it points to the
46 * exception class which is to be caught.
47 */
48
49
50 /**
51 * Initialize from another object.
52 */
53 public CodeException(CodeException c) {
54 this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
55 }
56
57
58 /**
59 * Construct object from file stream.
60 * @param file Input stream
61 * @throws IOException
62 */
63 CodeException(DataInput file) throws IOException {
64 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
65 .readUnsignedShort());
66 }
67
68
69 /**
70 * @param start_pc Range in the code the exception handler is active,
71 * start_pc is inclusive while
72 * @param end_pc is exclusive
73 * @param handler_pc Starting address of exception handler, i.e.,
74 * an offset from start of code.
75 * @param catch_type If zero the handler catches any
76 * exception, otherwise it points to the exception class which is
77 * to be caught.
78 */
79 public CodeException(int start_pc, int end_pc, int handler_pc, int catch_type) {
80 this.start_pc = start_pc;
81 this.end_pc = end_pc;
82 this.handler_pc = handler_pc;
83 this.catch_type = catch_type;
84 }
85
86
87 /**
88 * Called by objects that are traversing the nodes of the tree implicitely
89 * defined by the contents of a Java class. I.e., the hierarchy of methods,
90 * fields, attributes, etc. spawns a tree of objects.
91 *
92 * @param v Visitor object
93 */
94 public void accept( Visitor v ) {
95 v.visitCodeException(this);
96 }
97
98
99 /**
100 * Dump code exception to file stream in binary format.
101 *
102 * @param file Output file stream
103 * @throws IOException
104 */
105 public final void dump( DataOutputStream file ) throws IOException {
106 file.writeShort(start_pc);
107 file.writeShort(end_pc);
108 file.writeShort(handler_pc);
109 file.writeShort(catch_type);
110 }
111
112
113 /**
114 * @return 0, if the handler catches any exception, otherwise it points to
115 * the exception class which is to be caught.
116 */
117 public final int getCatchType() {
118 return catch_type;
119 }
120
121
122 /**
123 * @return Exclusive end index of the region where the handler is active.
124 */
125 public final int getEndPC() {
126 return end_pc;
127 }
128
129
130 /**
131 * @return Starting address of exception handler, relative to the code.
132 */
133 public final int getHandlerPC() {
134 return handler_pc;
135 }
136
137
138 /**
139 * @return Inclusive start index of the region where the handler is active.
140 */
141 public final int getStartPC() {
142 return start_pc;
143 }
144
145
146 /**
147 * @param catch_type the type of exception that is caught
148 */
149 public final void setCatchType( int catch_type ) {
150 this.catch_type = catch_type;
151 }
152
153
154 /**
155 * @param end_pc end of handled block
156 */
157 public final void setEndPC( int end_pc ) {
158 this.end_pc = end_pc;
159 }
160
161
162 /**
163 * @param handler_pc where the actual code is
164 */
165 public final void setHandlerPC( int handler_pc ) {
166 this.handler_pc = handler_pc;
167 }
168
169
170 /**
171 * @param start_pc start of handled block
172 */
173 public final void setStartPC( int start_pc ) {
174 this.start_pc = start_pc;
175 }
176
177
178 /**
179 * @return String representation.
180 */
181 @Override
182 public final String toString() {
183 return "CodeException(start_pc = " + start_pc + ", end_pc = " + end_pc + ", handler_pc = "
184 + handler_pc + ", catch_type = " + catch_type + ")";
185 }
186
187
188 /**
189 * @return String representation.
190 */
191 public final String toString( ConstantPool cp, boolean verbose ) {
192 String str;
193 if (catch_type == 0) {
194 str = "<Any exception>(0)";
195 } else {
196 str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false)
197 + (verbose ? "(" + catch_type + ")" : "");
198 }
199 return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
200 }
201
202
203 public final String toString( ConstantPool cp ) {
204 return toString(cp, true);
205 }
206
207
208 /**
209 * @return deep copy of this object
210 */
211 public CodeException copy() {
212 try {
213 return (CodeException) clone();
214 } catch (CloneNotSupportedException e) {
215 }
216 return null;
217 }
218 }