KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > ExceptionHandler


1 /*
2  * Copyright 2004 Brian S O'Neill
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.cojen.classfile;
18
19 import java.io.DataInput JavaDoc;
20 import java.io.DataOutput JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.cojen.classfile.constant.ConstantClassInfo;
23
24 /**
25  * This class corresponds to the exception_table structure as defined in
26  * section 4.7.4 of <i>The Java Virtual Machine Specification</i>.
27  *
28  * @author Brian S O'Neill
29  */

30 public class ExceptionHandler implements LocationRange {
31     private Location mStart;
32     private Location mEnd;
33     private Location mCatch;
34     private ConstantClassInfo mCatchType;
35     
36     /**
37      * @param startLocation
38      * @param endLocation
39      * @param catchLocation
40      * @param catchType if null, then catch every object.
41      */

42     public ExceptionHandler(Location startLocation,
43                             Location endLocation,
44                             Location catchLocation,
45                             ConstantClassInfo catchType) {
46         mStart = startLocation;
47         mEnd = endLocation;
48         mCatch = catchLocation;
49         mCatchType = catchType;
50     }
51     
52     public Location getStartLocation() {
53         return mStart;
54     }
55     
56     public Location getEndLocation() {
57         return mEnd;
58     }
59     
60     public Location getCatchLocation() {
61         return mCatch;
62     }
63     
64     /**
65      * Returns null if every object is caught by this handler.
66      */

67     public ConstantClassInfo getCatchType() {
68         return mCatchType;
69     }
70
71     public void writeTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
72         int start_pc = getStartLocation().getLocation();
73         int end_pc = getEndLocation().getLocation();
74         int handler_pc = getCatchLocation().getLocation();
75         int catch_type;
76         ConstantClassInfo catchType = getCatchType();
77         if (catchType == null) {
78             catch_type = 0;
79         }
80         else {
81             catch_type = catchType.getIndex();
82         }
83
84         check("exception start PC", start_pc);
85         check("exception end PC", end_pc);
86         check("exception handler PC", handler_pc);
87
88         dout.writeShort(start_pc);
89         dout.writeShort(end_pc);
90         dout.writeShort(handler_pc);
91         dout.writeShort(catch_type);
92     }
93
94     private void check(String JavaDoc type, int addr) throws IllegalStateException JavaDoc {
95         if (addr < 0 || addr > 65535) {
96             throw new IllegalStateException JavaDoc("Value for " + type + " out of " +
97                                             "valid range: " + addr);
98
99         }
100     }
101
102     public static ExceptionHandler readFrom(ConstantPool cp,
103                                             DataInput JavaDoc din) throws IOException JavaDoc {
104         int start_pc = din.readUnsignedShort();
105         int end_pc = din.readUnsignedShort();
106         int handler_pc = din.readUnsignedShort();
107         int catch_type = din.readUnsignedShort();
108
109         ConstantClassInfo catchTypeConstant;
110         if (catch_type == 0) {
111             catchTypeConstant = null;
112         } else {
113             catchTypeConstant = (ConstantClassInfo)cp.getConstant(catch_type);
114         }
115
116         return new ExceptionHandler(new FixedLocation(start_pc),
117                                     new FixedLocation(end_pc),
118                                     new FixedLocation(handler_pc),
119                                     catchTypeConstant);
120     }
121 }
122
Popular Tags