KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > CodeException


1 /*
2  * Copyright 2000-2004 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 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import org.apache.bcel.Constants;
24
25 /**
26  * This class represents an entry in the exception table of the <em>Code</em>
27  * attribute and is used only there. It contains a range in which a
28  * particular exception handler is active.
29  *
30  * @version $Id: CodeException.java 386056 2006-03-15 11:31:56Z tcurdt $
31  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
32  * @see Code
33  */

34 public final class CodeException implements Cloneable JavaDoc, Constants, Node, Serializable JavaDoc {
35
36     private int start_pc; // Range in the code the exception handler is
37
private int end_pc; // active. start_pc is inclusive, end_pc exclusive
38
private int handler_pc; /* Starting address of exception handler, i.e.,
39      * an offset from start of code.
40      */

41     private int catch_type; /* If this is zero the handler catches any
42      * exception, otherwise it points to the
43      * exception class which is to be caught.
44      */

45
46
47     /**
48      * Initialize from another object.
49      */

50     public CodeException(CodeException c) {
51         this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
52     }
53
54
55     /**
56      * Construct object from file stream.
57      * @param file Input stream
58      * @throws IOException
59      */

60     CodeException(DataInputStream JavaDoc file) throws IOException JavaDoc {
61         this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
62                 .readUnsignedShort());
63     }
64
65
66     /**
67      * @param start_pc Range in the code the exception handler is active,
68      * start_pc is inclusive while
69      * @param end_pc is exclusive
70      * @param handler_pc Starting address of exception handler, i.e.,
71      * an offset from start of code.
72      * @param catch_type If zero the handler catches any
73      * exception, otherwise it points to the exception class which is
74      * to be caught.
75      */

76     public CodeException(int start_pc, int end_pc, int handler_pc, int catch_type) {
77         this.start_pc = start_pc;
78         this.end_pc = end_pc;
79         this.handler_pc = handler_pc;
80         this.catch_type = catch_type;
81     }
82
83
84     /**
85      * Called by objects that are traversing the nodes of the tree implicitely
86      * defined by the contents of a Java class. I.e., the hierarchy of methods,
87      * fields, attributes, etc. spawns a tree of objects.
88      *
89      * @param v Visitor object
90      */

91     public void accept( Visitor v ) {
92         v.visitCodeException(this);
93     }
94
95
96     /**
97      * Dump code exception to file stream in binary format.
98      *
99      * @param file Output file stream
100      * @throws IOException
101      */

102     public final void dump( DataOutputStream JavaDoc file ) throws IOException JavaDoc {
103         file.writeShort(start_pc);
104         file.writeShort(end_pc);
105         file.writeShort(handler_pc);
106         file.writeShort(catch_type);
107     }
108
109
110     /**
111      * @return 0, if the handler catches any exception, otherwise it points to
112      * the exception class which is to be caught.
113      */

114     public final int getCatchType() {
115         return catch_type;
116     }
117
118
119     /**
120      * @return Exclusive end index of the region where the handler is active.
121      */

122     public final int getEndPC() {
123         return end_pc;
124     }
125
126
127     /**
128      * @return Starting address of exception handler, relative to the code.
129      */

130     public final int getHandlerPC() {
131         return handler_pc;
132     }
133
134
135     /**
136      * @return Inclusive start index of the region where the handler is active.
137      */

138     public final int getStartPC() {
139         return start_pc;
140     }
141
142
143     /**
144      * @param catch_type the type of exception that is caught
145      */

146     public final void setCatchType( int catch_type ) {
147         this.catch_type = catch_type;
148     }
149
150
151     /**
152      * @param end_pc end of handled block
153      */

154     public final void setEndPC( int end_pc ) {
155         this.end_pc = end_pc;
156     }
157
158
159     /**
160      * @param handler_pc where the actual code is
161      */

162     public final void setHandlerPC( int handler_pc ) {
163         this.handler_pc = handler_pc;
164     }
165
166
167     /**
168      * @param start_pc start of handled block
169      */

170     public final void setStartPC( int start_pc ) {
171         this.start_pc = start_pc;
172     }
173
174
175     /**
176      * @return String representation.
177      */

178     public final String JavaDoc toString() {
179         return "CodeException(start_pc = " + start_pc + ", end_pc = " + end_pc + ", handler_pc = "
180                 + handler_pc + ", catch_type = " + catch_type + ")";
181     }
182
183
184     /**
185      * @return String representation.
186      */

187     public final String JavaDoc toString( ConstantPool cp, boolean verbose ) {
188         String JavaDoc str;
189         if (catch_type == 0) {
190             str = "<Any exception>(0)";
191         } else {
192             str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false)
193                     + (verbose ? "(" + catch_type + ")" : "");
194         }
195         return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
196     }
197
198
199     public final String JavaDoc toString( ConstantPool cp ) {
200         return toString(cp, true);
201     }
202
203
204     /**
205      * @return deep copy of this object
206      */

207     public CodeException copy() {
208         try {
209             return (CodeException) clone();
210         } catch (CloneNotSupportedException JavaDoc e) {
211         }
212         return null;
213     }
214 }
215
Popular Tags