diff --git a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java b/dexlib/src/main/java/org/jf/dexlib/CodeItem.java index c9d9bc2e..28173848 100644 --- a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/CodeItem.java @@ -211,12 +211,10 @@ public class CodeItem extends OffsettedItem { public void copyTo(DexFile dexFile, CachedIntegerValueField _copy) { EncodedCatchHandlerReference copy = (EncodedCatchHandlerReference)_copy; - EncodedCatchHandler copiedItem = copy.getEncodedCatchHandlerList().getByOffset( - encodedCatchHandler.getOffsetInList()); + EncodedCatchHandler copiedItem = copy.getEncodedCatchHandlerList().intern(encodedCatchHandler); copy.setReference(copiedItem); } - public void writeTo(AnnotatedOutput out) { cacheValue(encodedCatchHandler.getOffsetInList()); @@ -237,9 +235,12 @@ public class CodeItem extends OffsettedItem { public class EncodedCatchHandlerList extends CompositeField { private boolean fieldPresent = false; + //this field is only valid when reading a dex file in protected HashMap itemsByOffset = new HashMap(); + protected HashMap uniqueItems = null; + private final DexFile dexFile; public EncodedCatchHandler getByOffset(int offset) { @@ -249,8 +250,27 @@ public class CodeItem extends OffsettedItem { itemsByOffset.put(offset, encodedCatchHandler); } return encodedCatchHandler; + } + public EncodedCatchHandler intern(EncodedCatchHandler item) { + if (uniqueItems == null) { + buildInternedItemMap(); + } + EncodedCatchHandler encodedCatchHandler = uniqueItems.get(item); + if (encodedCatchHandler == null) { + encodedCatchHandler = new EncodedCatchHandler(dexFile, -1); + catchHandlerList.add(encodedCatchHandler); + item.copyTo(dexFile, encodedCatchHandler); + uniqueItems.put(encodedCatchHandler, encodedCatchHandler); + } + return encodedCatchHandler; + } + private void buildInternedItemMap() { + uniqueItems = new HashMap(); + for (EncodedCatchHandler item: catchHandlerList) { + uniqueItems.put(item, item); + } } public EncodedCatchHandlerList(final DexFile dexFile) { @@ -318,6 +338,7 @@ public class CodeItem extends OffsettedItem { super.copyTo(dexFile, copy); copy.fieldPresent = fieldPresent; copy.itemsByOffset.clear(); + int offset = 0; for (EncodedCatchHandler encodedCatchHandler: copy.listField.list) { copy.itemsByOffset.put(encodedCatchHandler.offset, encodedCatchHandler); }