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  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
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   * @see     Code
33   */
34  public final class CodeException implements Cloneable, Node, Constants {
35  
36      private int startPc; // Range in the code the exception handler is
37      private int endPc; // active. startPc is inclusive, endPc exclusive
38      private int handlerPc; /* Starting address of exception handler, i.e.,
39       * an offset from start of code.
40       */
41      private int catchType; /* 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 CodeExceptiondeException.html#CodeException">CodeException(final 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(final DataInput file) throws IOException {
61          this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
62                  .readUnsignedShort());
63      }
64  
65  
66      /**
67       * @param startPc Range in the code the exception handler is active,
68       * startPc is inclusive while
69       * @param endPc is exclusive
70       * @param handlerPc Starting address of exception handler, i.e.,
71       * an offset from start of code.
72       * @param catchType 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(final int startPc, final int endPc, final int handlerPc, final int catchType) {
77          this.startPc = startPc;
78          this.endPc = endPc;
79          this.handlerPc = handlerPc;
80          this.catchType = catchType;
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      @Override
92      public void accept( final Visitor v ) {
93          v.visitCodeException(this);
94      }
95  
96  
97      /**
98       * Dump code exception to file stream in binary format.
99       *
100      * @param file Output file stream
101      * @throws IOException
102      */
103     public void dump( final DataOutputStream file ) throws IOException {
104         file.writeShort(startPc);
105         file.writeShort(endPc);
106         file.writeShort(handlerPc);
107         file.writeShort(catchType);
108     }
109 
110 
111     /**
112      * @return 0, if the handler catches any exception, otherwise it points to
113      * the exception class which is to be caught.
114      */
115     public int getCatchType() {
116         return catchType;
117     }
118 
119 
120     /**
121      * @return Exclusive end index of the region where the handler is active.
122      */
123     public int getEndPC() {
124         return endPc;
125     }
126 
127 
128     /**
129      * @return Starting address of exception handler, relative to the code.
130      */
131     public int getHandlerPC() {
132         return handlerPc;
133     }
134 
135 
136     /**
137      * @return Inclusive start index of the region where the handler is active.
138      */
139     public int getStartPC() {
140         return startPc;
141     }
142 
143 
144     /**
145      * @param catchType the type of exception that is caught
146      */
147     public void setCatchType( final int catchType ) {
148         this.catchType = catchType;
149     }
150 
151 
152     /**
153      * @param endPc end of handled block
154      */
155     public void setEndPC( final int endPc ) {
156         this.endPc = endPc;
157     }
158 
159 
160     /**
161      * @param handlerPc where the actual code is
162      */
163     public void setHandlerPC( final int handlerPc ) { // TODO unused
164         this.handlerPc = handlerPc;
165     }
166 
167 
168     /**
169      * @param startPc start of handled block
170      */
171     public void setStartPC( final int startPc ) { // TODO unused
172         this.startPc = startPc;
173     }
174 
175 
176     /**
177      * @return String representation.
178      */
179     @Override
180     public String toString() {
181         return "CodeException(startPc = " + startPc + ", endPc = " + endPc + ", handlerPc = "
182                 + handlerPc + ", catchType = " + catchType + ")";
183     }
184 
185 
186     /**
187      * @return String representation.
188      */
189     public String toString( final ConstantPool cp, final boolean verbose ) {
190         String str;
191         if (catchType == 0) {
192             str = "<Any exception>(0)";
193         } else {
194             str = Utility.compactClassName(cp.getConstantString(catchType, Const.CONSTANT_Class), false)
195                     + (verbose ? "(" + catchType + ")" : "");
196         }
197         return startPc + "\t" + endPc + "\t" + handlerPc + "\t" + str;
198     }
199 
200 
201     public String toString( final ConstantPool cp ) {
202         return toString(cp, true);
203     }
204 
205 
206     /**
207      * @return deep copy of this object
208      */
209     public CodeException copy() {
210         try {
211             return (CodeException) clone();
212         } catch (final CloneNotSupportedException e) {
213             // TODO should this throw?
214         }
215         return null;
216     }
217 }