Skip to content

Commit e2b8333

Browse files
committed
[DEX] Methods for easy access of target and targeting instruction
1 parent e2ec1b8 commit e2b8333

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/main/java/com/reandroid/dex/model/DexInstruction.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@
4242
import com.reandroid.dex.smali.SmaliReader;
4343
import com.reandroid.dex.smali.SmaliWriter;
4444
import com.reandroid.dex.smali.model.SmaliInstruction;
45-
import com.reandroid.utils.collection.CollectionUtil;
46-
import com.reandroid.utils.collection.ComputeIterator;
47-
import com.reandroid.utils.collection.EmptyIterator;
48-
import com.reandroid.utils.collection.FilterIterator;
49-
import com.reandroid.utils.collection.IterableIterator;
45+
import com.reandroid.utils.collection.*;
5046

5147
import java.io.IOException;
5248
import java.util.Iterator;
@@ -399,12 +395,41 @@ public DexDeclaration findDeclaration() {
399395
}
400396
return null;
401397
}
398+
public DexInstruction getTargetInstruction() {
399+
return DexInstruction.create(getDexMethod(), getIns().getTargetIns());
400+
}
401+
public boolean hasTargetingInstructions() {
402+
return getIns().getForcedExtraLines(Ins.class).hasNext();
403+
}
404+
public boolean hasTargetingInstructionsIfOpcode(Predicate<Opcode<?>> predicate) {
405+
return FilterIterator.of(getIns().getForcedExtraLines(Ins.class),
406+
ins -> predicate.test(ins.getOpcode())).hasNext();
407+
}
408+
public Iterator<DexInstruction> getTargetingInstructions() {
409+
return DexInstruction.createAll(getDexMethod(),
410+
CollectionUtil.copyOf(getIns().getForcedExtraLines(Ins.class)));
411+
}
412+
public Iterator<DexInstruction> getTargetingInstructionsIfOpcode(Predicate<Opcode<?>> predicate) {
413+
Iterator<Ins> iterator = CollectionUtil.copyOf(getIns().getForcedExtraLines(Ins.class));
414+
if (!iterator.hasNext()) {
415+
return EmptyIterator.of();
416+
}
417+
iterator = FilterIterator.of(iterator, ins -> predicate.test(ins.getOpcode()));
418+
return DexInstruction.createAll(getDexMethod(), iterator);
419+
}
402420
public DexInstruction getNext() {
403421
return getDexMethod().getInstruction(getIndex() + 1);
404422
}
423+
public Iterator<DexInstruction> getNextInstructions() {
424+
return LinkedIterator.of(this, DexInstruction::getNext);
425+
}
405426
public DexInstruction getPrevious() {
406427
return getDexMethod().getInstruction(getIndex() - 1);
407428
}
429+
public Iterator<DexInstruction> getPreviousInstructions() {
430+
return LinkedIterator.of(this, DexInstruction::getPrevious);
431+
}
432+
408433
public DexInstruction getPreviousReader(int register) {
409434
return getPreviousReader(register, CollectionUtil.getAcceptAll());
410435
}
@@ -499,8 +524,8 @@ public String toString() {
499524
return getIns().toString();
500525
}
501526

502-
public static Iterator<DexInstruction> create(DexMethod dexMethod, Iterator<Ins> iterator) {
503-
if (dexMethod == null) {
527+
public static Iterator<DexInstruction> createAll(DexMethod dexMethod, Iterator<Ins> iterator) {
528+
if (dexMethod == null || !iterator.hasNext()) {
504529
return EmptyIterator.of();
505530
}
506531
return ComputeIterator.of(iterator, ins -> create(dexMethod, ins));

src/main/java/com/reandroid/dex/model/DexMethod.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ public Iterator<DexInstruction> getInstructions(Predicate<? super Ins> filter) {
265265
return getInstructionsIfIns(filter);
266266
}
267267
public Iterator<DexInstruction> getInstructionsIfIns(Predicate<? super Ins> filter) {
268-
Iterator<Ins> iterator = FilterIterator.of(getDefinition().getInstructions(), filter);
269-
return ComputeIterator.of(iterator, this::create);
268+
return DexInstruction.createAll(this,
269+
FilterIterator.of(getDefinition().getInstructions(), filter));
270270
}
271271
public Iterator<DexInstruction> getInstructions() {
272-
return DexInstruction.create(this, getDefinition().getInstructions());
272+
return DexInstruction.createAll(this, getDefinition().getInstructions());
273273
}
274274

275275
int getEditIndex() {

src/main/java/com/reandroid/dex/model/DexTry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Iterator<DexInstruction> getInstructions(){
7373
int address = ins.getAddress();
7474
return address >= getStartAddress() && address < getEndAddress();
7575
});
76-
return DexInstruction.create(getDexMethod(), iterator);
76+
return DexInstruction.createAll(getDexMethod(), iterator);
7777
}
7878
public int getStartAddress(){
7979
return getTryItem().getStartAddress();

0 commit comments

Comments
 (0)