KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > generic > CodeExceptionGen


1 package com.sun.org.apache.bcel.internal.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 com.sun.org.apache.bcel.internal.Constants;
58 import com.sun.org.apache.bcel.internal.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.1.1.1 2001/10/29 20:00:08 jvanzyl 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 implements InstructionTargeter, Cloneable JavaDoc {
76   private InstructionHandle start_pc;
77   private InstructionHandle end_pc;
78   private InstructionHandle handler_pc;
79   private ObjectType catch_type;
80   
81   /**
82    * Add an exception handler, i.e., specify region where a handler is active and an
83    * instruction where the actual handling is done.
84    *
85    * @param start_pc Start of handled region (inclusive)
86    * @param end_pc End of handled region (inclusive)
87    * @param handler_pc Where handling is done
88    * @param catch_type which exception is handled, null for ANY
89    */

90   public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
91               InstructionHandle handler_pc, ObjectType catch_type) {
92     setStartPC(start_pc);
93     setEndPC(end_pc);
94     setHandlerPC(handler_pc);
95     this.catch_type = catch_type;
96   }
97
98   /**
99    * Get CodeException object.<BR>
100    *
101    * This relies on that the instruction list has already been dumped
102    * to byte code or or that the `setPositions' methods has been
103    * called for the instruction list.
104    *
105    * @param cp constant pool
106    */

107   public CodeException getCodeException(ConstantPoolGen cp) {
108     return new CodeException(start_pc.getPosition(),
109                  end_pc.getPosition() + end_pc.getInstruction().getLength(),
110                  handler_pc.getPosition(),
111                  (catch_type == null)? 0 : cp.addClass(catch_type));
112   }
113
114   /* Set start of handler
115    * @param start_pc Start of handled region (inclusive)
116    */

117   public void setStartPC(InstructionHandle start_pc) {
118     BranchInstruction.notifyTarget(this.start_pc, start_pc, this);
119     this.start_pc = start_pc;
120   }
121
122   /* Set end of handler
123    * @param end_pc End of handled region (inclusive)
124    */

125   public void setEndPC(InstructionHandle end_pc) {
126     BranchInstruction.notifyTarget(this.end_pc, end_pc, this);
127     this.end_pc = end_pc;
128   }
129
130   /* Set handler code
131    * @param handler_pc Start of handler
132    */

133   public void setHandlerPC(InstructionHandle handler_pc) {
134     BranchInstruction.notifyTarget(this.handler_pc, handler_pc, this);
135     this.handler_pc = handler_pc;
136   }
137
138   /**
139    * @param old_ih old target, either start or end
140    * @param new_ih new target
141    */

142   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
143     boolean targeted = false;
144
145     if(start_pc == old_ih) {
146       targeted = true;
147       setStartPC(new_ih);
148     }
149
150     if(end_pc == old_ih) {
151       targeted = true;
152       setEndPC(new_ih);
153     }
154
155     if(handler_pc == old_ih) {
156       targeted = true;
157       setHandlerPC(new_ih);
158     }
159
160     if(!targeted)
161       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " +
162                   end_pc + ", " + handler_pc + "}");
163   }
164
165   /**
166    * @return true, if ih is target of this handler
167    */

168   public boolean containsTarget(InstructionHandle ih) {
169     return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
170   }
171
172   /** Sets the type of the Exception to catch. Set 'null' for ANY. */
173   public void setCatchType(ObjectType catch_type) { this.catch_type = catch_type; }
174   /** Gets the type of the Exception to catch, 'null' for ANY. */
175   public ObjectType getCatchType() { return catch_type; }
176
177   /** @return start of handled region (inclusive)
178    */

179   public InstructionHandle getStartPC() { return start_pc; }
180
181   /** @return end of handled region (inclusive)
182    */

183   public InstructionHandle getEndPC() { return end_pc; }
184
185   /** @return start of handler
186    */

187   public InstructionHandle getHandlerPC() { return handler_pc; }
188
189   public String JavaDoc toString() {
190     return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
191   }
192
193   public Object JavaDoc clone() {
194     try {
195       return super.clone();
196     } catch(CloneNotSupportedException JavaDoc e) {
197       System.err.println(e);
198       return null;
199     }
200   }
201 }
202
Popular Tags