1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import org.apache.bcel.classfile.CodeException;
22
23
24
25
26
27
28
29
30
31
32
33
34 public final class CodeExceptionGen implements InstructionTargeter, Cloneable {
35
36 static final CodeExceptionGen[] EMPTY_ARRAY = {};
37
38 private InstructionHandle startPc;
39 private InstructionHandle endPc;
40 private InstructionHandle handlerPc;
41 private ObjectType catchType;
42
43
44
45
46
47
48
49
50
51
52 public CodeExceptionGen(final InstructionHandle startPc, final InstructionHandle endPc, final InstructionHandle handlerPc, final ObjectType catchType) {
53 setStartPC(startPc);
54 setEndPC(endPc);
55 setHandlerPC(handlerPc);
56 this.catchType = catchType;
57 }
58
59 @Override
60 public Object clone() {
61 try {
62 return super.clone();
63 } catch (final CloneNotSupportedException e) {
64 throw new UnsupportedOperationException("Clone Not Supported", e);
65 }
66 }
67
68
69
70
71 @Override
72 public boolean containsTarget(final InstructionHandle ih) {
73 return startPc == ih || endPc == ih || handlerPc == ih;
74 }
75
76
77 public ObjectType getCatchType() {
78 return catchType;
79 }
80
81
82
83
84
85
86
87
88
89 public CodeException getCodeException(final ConstantPoolGen cp) {
90 return new CodeException(startPc.getPosition(), endPc.getPosition() + endPc.getInstruction().getLength(), handlerPc.getPosition(),
91 catchType == null ? 0 : cp.addClass(catchType));
92 }
93
94
95
96
97 public InstructionHandle getEndPC() {
98 return endPc;
99 }
100
101
102
103
104 public InstructionHandle getHandlerPC() {
105 return handlerPc;
106 }
107
108
109
110
111 public InstructionHandle getStartPC() {
112 return startPc;
113 }
114
115
116 public void setCatchType(final ObjectType catchType) {
117 this.catchType = catchType;
118 }
119
120
121
122
123
124
125 public void setEndPC(final InstructionHandle endPc) {
126 BranchInstruction.notifyTarget(this.endPc, endPc, this);
127 this.endPc = endPc;
128 }
129
130
131
132
133
134
135 public void setHandlerPC(final InstructionHandle handlerPc) {
136 BranchInstruction.notifyTarget(this.handlerPc, handlerPc, this);
137 this.handlerPc = handlerPc;
138 }
139
140
141
142
143
144
145 public void setStartPC(final InstructionHandle startPc) {
146 BranchInstruction.notifyTarget(this.startPc, startPc, this);
147 this.startPc = startPc;
148 }
149
150 @Override
151 public String toString() {
152 return "CodeExceptionGen(" + startPc + ", " + endPc + ", " + handlerPc + ")";
153 }
154
155
156
157
158
159 @Override
160 public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
161 boolean targeted = false;
162 if (startPc == oldIh) {
163 targeted = true;
164 setStartPC(newIh);
165 }
166 if (endPc == oldIh) {
167 targeted = true;
168 setEndPC(newIh);
169 }
170 if (handlerPc == oldIh) {
171 targeted = true;
172 setHandlerPC(newIh);
173 }
174 if (!targeted) {
175 throw new ClassGenException("Not targeting " + oldIh + ", but {" + startPc + ", " + endPc + ", " + handlerPc + "}");
176 }
177 }
178 }