View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   package com.sun.org.apache.bcel.internal.classfile;
6   
7   /* ====================================================================
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2001 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution,
26   *    if any, must include the following acknowledgment:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowledgment may appear in the software itself,
30   *    if and wherever such third-party acknowledgments normally appear.
31   *
32   * 4. The names "Apache" and "Apache Software Foundation" and
33   *    "Apache BCEL" must not be used to endorse or promote products
34   *    derived from this software without prior written permission. For
35   *    written permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache",
38   *    "Apache BCEL", nor may "Apache" appear in their name, without
39   *    prior written permission of the Apache Software Foundation.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   */
60  
61  import  com.sun.org.apache.bcel.internal.Constants;
62  import  java.io.*;
63  
64  /**
65   * This class represents an entry in the exception table of the <em>Code</em>
66   * attribute and is used only there. It contains a range in which a
67   * particular exception handler is active.
68   *
69   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
70   * @see     Code
71   */
72  public final class CodeException
73    implements Cloneable, Constants, Node, Serializable
74  {
75    private int start_pc;   // Range in the code the exception handler is
76    private int end_pc;     // active. start_pc is inclusive, end_pc exclusive
77    private int handler_pc; /* Starting address of exception handler, i.e.,
78                             * an offset from start of code.
79                             */
80    private int catch_type; /* If this is zero the handler catches any
81                             * exception, otherwise it points to the
82                             * exception class which is to be caught.
83                             */
84    /**
85     * Initialize from another object.
86     */
87    public CodeException(CodeException c) {
88      this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
89    }
90  
91    /**
92     * Construct object from file stream.
93     * @param file Input stream
94     * @throws IOException
95     */
96    CodeException(DataInputStream file) throws IOException
97    {
98      this(file.readUnsignedShort(), file.readUnsignedShort(),
99           file.readUnsignedShort(), file.readUnsignedShort());
100   }
101 
102   /**
103    * @param start_pc Range in the code the exception handler is active,
104    * start_pc is inclusive while
105    * @param end_pc is exclusive
106    * @param handler_pc Starting address of exception handler, i.e.,
107    * an offset from start of code.
108    * @param catch_type If zero the handler catches any
109    * exception, otherwise it points to the exception class which is
110    * to be caught.
111    */
112   public CodeException(int start_pc, int end_pc, int handler_pc,
113                        int catch_type)
114   {
115     this.start_pc   = start_pc;
116     this.end_pc     = end_pc;
117     this.handler_pc = handler_pc;
118     this.catch_type = catch_type;
119   }
120 
121   /**
122    * Called by objects that are traversing the nodes of the tree implicitely
123    * defined by the contents of a Java class. I.e., the hierarchy of methods,
124    * fields, attributes, etc. spawns a tree of objects.
125    *
126    * @param v Visitor object
127    */
128   public void accept(Visitor v) {
129     v.visitCodeException(this);
130   }
131   /**
132    * Dump code exception to file stream in binary format.
133    *
134    * @param file Output file stream
135    * @throws IOException
136    */
137   public final void dump(DataOutputStream file) throws IOException
138   {
139     file.writeShort(start_pc);
140     file.writeShort(end_pc);
141     file.writeShort(handler_pc);
142     file.writeShort(catch_type);
143   }
144 
145   /**
146    * @return 0, if the handler catches any exception, otherwise it points to
147    * the exception class which is to be caught.
148    */
149   public final int getCatchType() { return catch_type; }
150 
151   /**
152    * @return Exclusive end index of the region where the handler is active.
153    */
154   public final int getEndPC() { return end_pc; }
155 
156   /**
157    * @return Starting address of exception handler, relative to the code.
158    */
159   public final int getHandlerPC() { return handler_pc; }
160 
161   /**
162    * @return Inclusive start index of the region where the handler is active.
163    */
164   public final int getStartPC() { return start_pc; }
165 
166   /**
167    * @param catch_type.
168    */
169   public final void setCatchType(int catch_type) {
170     this.catch_type = catch_type;
171   }
172 
173   /**
174    * @param end_pc end of handled block
175    */
176   public final void setEndPC(int end_pc) {
177     this.end_pc = end_pc;
178   }
179 
180   /**
181    * @param handler_pc where the actual code is
182    */
183   public final void setHandlerPC(int handler_pc) {
184     this.handler_pc = handler_pc;
185   }
186 
187   /**
188    * @param start_pc start of handled block
189    */
190   public final void setStartPC(int start_pc) {
191     this.start_pc = start_pc;
192   }
193 
194   /**
195    * @return String representation.
196    */
197   public final String toString() {
198     return "CodeException(start_pc = " + start_pc +
199       ", end_pc = " + end_pc +
200       ", handler_pc = " + handler_pc + ", catch_type = " + catch_type + ")";
201   }
202 
203   /**
204    * @return String representation.
205    */
206   public final String toString(ConstantPool cp, boolean verbose) {
207     String str;
208 
209     if(catch_type == 0)
210       str = "<Any exception>(0)";
211     else
212       str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false) +
213         (verbose? "(" + catch_type + ")" : "");
214 
215     return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
216   }
217 
218   public final String toString(ConstantPool cp) {
219     return toString(cp, true);
220   }
221 
222   /**
223    * @return deep copy of this object
224    */
225   public CodeException copy() {
226     try {
227       return (CodeException)clone();
228     } catch(CloneNotSupportedException e) {}
229 
230     return null;
231   }
232 }