1   package org.apache.bcel.generic;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache BCEL" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache BCEL", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import org.apache.bcel.Constants;
58  import org.apache.bcel.classfile.*;
59  
60  /*** 
61   * This class represents an exception handler, i.e., specifies the  region where
62   * a handler is active and an instruction where the actual handling is done.
63   * pool as parameters. Opposed to the JVM specification the end of the handled
64   * region is set to be inclusive, i.e. all instructions between start and end
65   * are protected including the start and end instructions (handles) themselves.
66   * The end of the region is automatically mapped to be exclusive when calling
67   * getCodeException(), i.e., there is no difference semantically.
68   *
69   * @version $Id: CodeExceptionGen.java,v 1.2 2002/07/11 19:39:04 mdahm Exp $
70   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
71   * @see     MethodGen
72   * @see     CodeException
73   * @see     InstructionHandle
74   */
75  public final class CodeExceptionGen
76    implements InstructionTargeter, Cloneable, java.io.Serializable {
77    private InstructionHandle start_pc;
78    private InstructionHandle end_pc;
79    private InstructionHandle handler_pc;
80    private ObjectType        catch_type;
81    
82    /***
83     * Add an exception handler, i.e., specify region where a handler is active and an
84     * instruction where the actual handling is done.
85     *
86     * @param start_pc Start of handled region (inclusive)
87     * @param end_pc End of handled region (inclusive)
88     * @param handler_pc Where handling is done
89     * @param catch_type which exception is handled, null for ANY
90     */
91    public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
92  			  InstructionHandle handler_pc, ObjectType catch_type) {
93      setStartPC(start_pc);
94      setEndPC(end_pc);
95      setHandlerPC(handler_pc);
96      this.catch_type = catch_type;
97    }
98  
99    /***
100    * Get CodeException object.<BR>
101    *
102    * This relies on that the instruction list has already been dumped
103    * to byte code or or that the `setPositions' methods has been
104    * called for the instruction list.
105    *
106    * @param cp constant pool
107    */
108   public CodeException getCodeException(ConstantPoolGen cp) {
109     return new CodeException(start_pc.getPosition(),
110 			     end_pc.getPosition() + end_pc.getInstruction().getLength(),
111 			     handler_pc.getPosition(),
112 			     (catch_type == null)? 0 : cp.addClass(catch_type));
113   }
114 
115   /* Set start of handler 
116    * @param start_pc Start of handled region (inclusive)
117    */
118   public void setStartPC(InstructionHandle start_pc) {
119     BranchInstruction.notifyTarget(this.start_pc, start_pc, this);
120     this.start_pc = start_pc; 
121   }
122 
123   /* Set end of handler 
124    * @param end_pc End of handled region (inclusive)
125    */
126   public void setEndPC(InstructionHandle end_pc) {
127     BranchInstruction.notifyTarget(this.end_pc, end_pc, this);
128     this.end_pc = end_pc;
129   }
130 
131   /* Set handler code
132    * @param handler_pc Start of handler
133    */
134   public void setHandlerPC(InstructionHandle handler_pc) {
135     BranchInstruction.notifyTarget(this.handler_pc, handler_pc, this);
136     this.handler_pc = handler_pc;
137   }
138 
139   /***
140    * @param old_ih old target, either start or end
141    * @param new_ih new target
142    */
143   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
144     boolean targeted = false;
145 
146     if(start_pc == old_ih) {
147       targeted = true;
148       setStartPC(new_ih);
149     }
150 
151     if(end_pc == old_ih) {
152       targeted = true;
153       setEndPC(new_ih);
154     }
155 
156     if(handler_pc == old_ih) {
157       targeted = true;
158       setHandlerPC(new_ih);
159     }
160 
161     if(!targeted)
162       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " +
163 				  end_pc + ", " + handler_pc + "}");
164   }
165 
166   /***
167    * @return true, if ih is target of this handler
168    */
169   public boolean containsTarget(InstructionHandle ih) {
170     return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
171   }
172 
173   /*** Sets the type of the Exception to catch. Set 'null' for ANY. */
174   public void              setCatchType(ObjectType catch_type)        { this.catch_type = catch_type; }
175   /*** Gets the type of the Exception to catch, 'null' for ANY. */
176   public ObjectType        getCatchType()                             { return catch_type; }
177 
178   /*** @return start of handled region (inclusive)
179    */
180   public InstructionHandle getStartPC()                               { return start_pc; }
181 
182   /*** @return end of handled region (inclusive)
183    */
184   public InstructionHandle getEndPC()                                 { return end_pc; }
185 
186   /*** @return start of handler
187    */
188   public InstructionHandle getHandlerPC()                             { return handler_pc; }
189 
190   public String toString() {
191     return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
192   }
193 
194   public Object clone() {
195     try {
196       return super.clone();
197     } catch(CloneNotSupportedException e) {
198       System.err.println(e);
199       return null;
200     }
201   }
202 }
This page was automatically generated by Maven