Skip to content

Commit 8c3c495

Browse files
authored
Merge pull request #2052 from MarcMil/tableext
Add helper method to switch statements, which shows the target
2 parents 88acfee + e02916b commit 8c3c495

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/main/java/soot/jimple/SwitchStmt.java

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public interface SwitchStmt extends Stmt {
4343
public ValueBox getKeyBox();
4444

4545
public List<Unit> getTargets();
46+
47+
/**
48+
* Returns the target unit if a value with value
49+
* "value" is the key
50+
* @param value the value
51+
* @return the target
52+
*/
53+
public Unit getTargetForValue(int value);
4654

4755
public Unit getTarget(int index);
4856

src/main/java/soot/jimple/internal/JLookupSwitchStmt.java

+10
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,14 @@ public void convertToBaf(JimpleToBafContext context, List<Unit> out) {
164164
u.addAllTagsOf(this);
165165
out.add(u);
166166
}
167+
168+
@Override
169+
public Unit getTargetForValue(int value) {
170+
for (int i = 0; i < lookupValues.size(); i++) {
171+
if (lookupValues.get(i).value == value) {
172+
return getTarget(i);
173+
}
174+
}
175+
return getDefaultTarget();
176+
}
167177
}

src/main/java/soot/jimple/internal/JTableSwitchStmt.java

+19
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,23 @@ public void convertToBaf(JimpleToBafContext context, List<Unit> out) {
173173
u.addAllTagsOf(this);
174174
out.add(u);
175175
}
176+
177+
@Override
178+
public Unit getTargetForValue(int value) {
179+
180+
final int high = highIndex;
181+
int tgtIdx = 0;
182+
// In this for-loop, we cannot use "<=" since 'i' would wrap around.
183+
// The case for "i == highIndex" is handled separately after the loop.
184+
for (int i = lowIndex; i < high; i++) {
185+
if (value == i) {
186+
return getTarget(tgtIdx);
187+
}
188+
tgtIdx++;
189+
}
190+
if (high == value) {
191+
return getTarget(tgtIdx);
192+
}
193+
return getDefaultTarget();
194+
}
176195
}

0 commit comments

Comments
 (0)