From af8bc1d9cd308e3d4f52330f740725c5c7847f1d Mon Sep 17 00:00:00 2001 From: Assaf Date: Sun, 8 Jul 2018 16:01:34 +0300 Subject: [PATCH] Refactor LocatedItems::mergeItemsIntoNext Add test for mergeInto to make sure the order is kept. --- .../jf/dexlib2/builder/LocatedDebugItems.java | 2 +- .../org/jf/dexlib2/builder/LocatedItems.java | 51 +++++++++++-------- .../org/jf/dexlib2/builder/LocatedLabels.java | 2 +- .../jf/dexlib2/builder/MethodLocation.java | 7 ++- .../jf/dexlib2/builder/LocatedItemsTest.java | 46 +++++++++++++++++ 5 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 dexlib2/src/test/java/org/jf/dexlib2/builder/LocatedItemsTest.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedDebugItems.java b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedDebugItems.java index 61403565..77291ac8 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedDebugItems.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedDebugItems.java @@ -3,7 +3,7 @@ package org.jf.dexlib2.builder; public class LocatedDebugItems extends LocatedItems { @Override - protected String addLocatedItemError() { + protected String getAddLocatedItemError() { return "Cannot add a debug item that has already been added to a method." + "You must remove it from its current location first."; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedItems.java b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedItems.java index c4abf9e3..261742f9 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedItems.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedItems.java @@ -13,16 +13,14 @@ public abstract class LocatedItems { @Nullable private List items = null; - @Nonnull - private List getMutableItems() { + private void initItemsIfNull() { if (items == null) { items = new ArrayList<>(1); } - return items; } @Nonnull - private List getImmutableItems() { + private List getItems() { if (items == null) { return ImmutableList.of(); } @@ -32,23 +30,27 @@ public abstract class LocatedItems { public Set getModifiableItems(MethodLocation newItemsLocation) { return new AbstractSet() { @Nonnull - @Override public Iterator iterator() { - final Iterator it = getImmutableItems().iterator(); + @Override + public Iterator iterator() { + final Iterator it = getItems().iterator(); return new Iterator() { private @Nullable T currentItem = null; - @Override public boolean hasNext() { + @Override + public boolean hasNext() { return it.hasNext(); } - @Override public T next() { + @Override + public T next() { currentItem = it.next(); return currentItem; } - @Override public void remove() { + @Override + public void remove() { if (currentItem != null) { currentItem.setLocation(null); } @@ -57,30 +59,37 @@ public abstract class LocatedItems { }; } - @Override public int size() { - return getImmutableItems().size(); + @Override + public int size() { + return getItems().size(); } - @Override public boolean add(@Nonnull T item) { + @Override + public boolean add(@Nonnull T item) { if (item.isPlaced()) { - throw new IllegalArgumentException(addLocatedItemError()); + throw new IllegalArgumentException(getAddLocatedItemError()); } item.setLocation(newItemsLocation); - getMutableItems().add(item); + initItemsIfNull(); + items.add(item); return true; } }; } - protected abstract String addLocatedItemError(); + protected abstract String getAddLocatedItemError(); - public void mergeItemsInto(@Nonnull MethodLocation newLocation, LocatedItems otherLocatedItems) { - if (items != null || otherLocatedItems.items != null) { - List otherItems = otherLocatedItems.getMutableItems(); - for (T item: getImmutableItems()) { - item.setLocation(newLocation); - otherItems.add(item); + public void mergeItemsIntoNext(@Nonnull MethodLocation nextLocation, LocatedItems otherLocatedItems) { + if (otherLocatedItems == this) { + return; + } + if (items != null) { + for (T item : items) { + item.setLocation(nextLocation); } + List mergedItems = items; + mergedItems.addAll(otherLocatedItems.getItems()); + otherLocatedItems.items = mergedItems; items = null; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedLabels.java b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedLabels.java index 2b7823f3..d14ce7bc 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedLabels.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/builder/LocatedLabels.java @@ -2,7 +2,7 @@ package org.jf.dexlib2.builder; public class LocatedLabels extends LocatedItems