1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 import org.apache.bcel.classfile.CodeException;
20
21
22
23
24
25
26
27
28
29
30
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
43
44
45
46
47
48
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);
63 }
64 }
65
66
67
68
69 @Override
70 public boolean containsTarget(final InstructionHandle ih) {
71 return startPc == ih || endPc == ih || handlerPc == ih;
72 }
73
74
75 public ObjectType getCatchType() {
76 return catchType;
77 }
78
79
80
81
82
83
84
85
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
94
95 public InstructionHandle getEndPC() {
96 return endPc;
97 }
98
99
100
101
102 public InstructionHandle getHandlerPC() {
103 return handlerPc;
104 }
105
106
107
108
109 public InstructionHandle getStartPC() {
110 return startPc;
111 }
112
113
114 public void setCatchType(final ObjectType catchType) {
115 this.catchType = catchType;
116 }
117
118
119
120
121
122
123 public void setEndPC(final InstructionHandle endPc) {
124 BranchInstruction.notifyTarget(this.endPc, endPc, this);
125 this.endPc = endPc;
126 }
127
128
129
130
131
132
133 public void setHandlerPC(final InstructionHandle handlerPc) {
134 BranchInstruction.notifyTarget(this.handlerPc, handlerPc, this);
135 this.handlerPc = handlerPc;
136 }
137
138
139
140
141
142
143 public void setStartPC(final InstructionHandle startPc) {
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
155
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 }