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  package org.apache.bcel.generic;
18  
19  import org.apache.bcel.classfile.CodeException;
20  
21  /**
22   * This class represents an exception handler, i.e., specifies the region where a handler is active and an instruction
23   * where the actual handling is done. pool as parameters. Opposed to the JVM specification the end of the handled region
24   * is set to be inclusive, i.e. all instructions between start and end are protected including the start and end
25   * instructions (handles) themselves. The end of the region is automatically mapped to be exclusive when calling
26   * getCodeException(), i.e., there is no difference semantically.
27   *
28   * @see MethodGen
29   * @see CodeException
30   * @see InstructionHandle
31   */
32  public final class CodeExceptionGen implements InstructionTargeter, Cloneable {
33  
34      static final CodeExceptionGen[] EMPTY_ARRAY = {};
35  
36      private InstructionHandle startPc;
37      private InstructionHandle endPc;
38      private InstructionHandle handlerPc;
39      private ObjectType catchType;
40  
41      /**
42       * Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling
43       * is done.
44       *
45       * @param startPc Start of handled region (inclusive)
46       * @param endPc End of handled region (inclusive)
47       * @param handlerPc Where handling is done
48       * @param catchType which exception is handled, null for ANY
49       */
50      public CodeExceptionGen(final InstructionHandle startPc, final InstructionHandle endPc, final InstructionHandle handlerPc, final ObjectType catchType) {
51          setStartPC(startPc);
52          setEndPC(endPc);
53          setHandlerPC(handlerPc);
54          this.catchType = catchType;
55      }
56  
57      @Override
58      public Object clone() {
59          try {
60              return super.clone();
61          } catch (final CloneNotSupportedException e) {
62              throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
63          }
64      }
65  
66      /**
67       * @return true, if ih is target of this handler
68       */
69      @Override
70      public boolean containsTarget(final InstructionHandle ih) {
71          return startPc == ih || endPc == ih || handlerPc == ih;
72      }
73  
74      /** Gets the type of the Exception to catch, 'null' for ANY. */
75      public ObjectType getCatchType() {
76          return catchType;
77      }
78  
79      /**
80       * Gets CodeException object.<BR>
81       *
82       * This relies on that the instruction list has already been dumped to byte code or that the 'setPositions' methods
83       * has been called for the instruction list.
84       *
85       * @param cp constant pool
86       */
87      public CodeException getCodeException(final ConstantPoolGen cp) {
88          return new CodeException(startPc.getPosition(), endPc.getPosition() + endPc.getInstruction().getLength(), handlerPc.getPosition(),
89              catchType == null ? 0 : cp.addClass(catchType));
90      }
91  
92      /**
93       * @return end of handled region (inclusive)
94       */
95      public InstructionHandle getEndPC() {
96          return endPc;
97      }
98  
99      /**
100      * @return start of handler
101      */
102     public InstructionHandle getHandlerPC() {
103         return handlerPc;
104     }
105 
106     /**
107      * @return start of handled region (inclusive)
108      */
109     public InstructionHandle getStartPC() {
110         return startPc;
111     }
112 
113     /** Sets the type of the Exception to catch. Set 'null' for ANY. */
114     public void setCatchType(final ObjectType catchType) {
115         this.catchType = catchType;
116     }
117 
118     /*
119      * Sets end of handler
120      *
121      * @param endPc End of handled region (inclusive)
122      */
123     public void setEndPC(final InstructionHandle endPc) { // TODO could be package-protected?
124         BranchInstruction.notifyTarget(this.endPc, endPc, this);
125         this.endPc = endPc;
126     }
127 
128     /*
129      * Sets handler code
130      *
131      * @param handlerPc Start of handler
132      */
133     public void setHandlerPC(final InstructionHandle handlerPc) { // TODO could be package-protected?
134         BranchInstruction.notifyTarget(this.handlerPc, handlerPc, this);
135         this.handlerPc = handlerPc;
136     }
137 
138     /*
139      * Sets start of handler
140      *
141      * @param startPc Start of handled region (inclusive)
142      */
143     public void setStartPC(final InstructionHandle startPc) { // TODO could be package-protected?
144         BranchInstruction.notifyTarget(this.startPc, startPc, this);
145         this.startPc = startPc;
146     }
147 
148     @Override
149     public String toString() {
150         return "CodeExceptionGen(" + startPc + ", " + endPc + ", " + handlerPc + ")";
151     }
152 
153     /**
154      * @param oldIh old target, either start or end
155      * @param newIh new target
156      */
157     @Override
158     public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
159         boolean targeted = false;
160         if (startPc == oldIh) {
161             targeted = true;
162             setStartPC(newIh);
163         }
164         if (endPc == oldIh) {
165             targeted = true;
166             setEndPC(newIh);
167         }
168         if (handlerPc == oldIh) {
169             targeted = true;
170             setHandlerPC(newIh);
171         }
172         if (!targeted) {
173             throw new ClassGenException("Not targeting " + oldIh + ", but {" + startPc + ", " + endPc + ", " + handlerPc + "}");
174         }
175     }
176 }