diff --git a/jadx-core/src/main/java/jadx/core/dex/attributes/AttrNode.java b/jadx-core/src/main/java/jadx/core/dex/attributes/AttrNode.java index 5666c8b8c..bbc996965 100644 --- a/jadx-core/src/main/java/jadx/core/dex/attributes/AttrNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/attributes/AttrNode.java @@ -136,8 +136,7 @@ public abstract class AttrNode implements IAttributeNode { @Override public void clearAttributes() { - storage.clear(); - unloadIfEmpty(); + storage = EMPTY_ATTR_STORAGE; } /** diff --git a/jadx-core/src/main/java/jadx/core/dex/attributes/AttributeStorage.java b/jadx-core/src/main/java/jadx/core/dex/attributes/AttributeStorage.java index 6076b76d9..2b476ecf3 100644 --- a/jadx-core/src/main/java/jadx/core/dex/attributes/AttributeStorage.java +++ b/jadx-core/src/main/java/jadx/core/dex/attributes/AttributeStorage.java @@ -7,6 +7,7 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import jadx.api.plugins.input.data.annotations.IAnnotation; import jadx.api.plugins.input.data.attributes.IJadxAttrType; @@ -48,14 +49,11 @@ public class AttributeStorage { } public void add(IJadxAttribute attr) { - writeAttributes().put(attr.getAttrType(), attr); + writeAttributes(map -> map.put(attr.getAttrType(), attr)); } public void add(List list) { - Map, IJadxAttribute> map = writeAttributes(); - for (IJadxAttribute attr : list) { - map.put(attr.getAttrType(), attr); - } + writeAttributes(map -> list.forEach(attr -> map.put(attr.getAttrType(), attr))); } public void add(IJadxAttrType> type, T obj) { @@ -69,7 +67,9 @@ public class AttributeStorage { public void addAll(AttributeStorage otherList) { flags.addAll(otherList.flags); - writeAttributes().putAll(otherList.attributes); + if (!otherList.attributes.isEmpty()) { + writeAttributes(m -> m.putAll(otherList.attributes)); + } } public boolean contains(AFlag flag) { @@ -104,43 +104,42 @@ public class AttributeStorage { public void remove(IJadxAttrType type) { if (!attributes.isEmpty()) { - attributes.remove(type); + writeAttributes(map -> map.remove(type)); } } public void remove(IJadxAttribute attr) { if (!attributes.isEmpty()) { - IJadxAttrType type = attr.getAttrType(); - IJadxAttribute a = attributes.get(type); - if (a == attr) { - attributes.remove(type); - } + writeAttributes(map -> { + IJadxAttrType type = attr.getAttrType(); + IJadxAttribute a = map.get(type); + if (a == attr) { + map.remove(type); + } + }); } } - private Map, IJadxAttribute> writeAttributes() { + private void writeAttributes(Consumer, IJadxAttribute>> mapConsumer) { if (attributes.isEmpty()) { attributes = new IdentityHashMap<>(5); } - return attributes; - } - - public void clear() { - flags.clear(); - if (!attributes.isEmpty()) { - attributes.clear(); + synchronized (this) { + mapConsumer.accept(attributes); } } - public synchronized void unloadAttributes() { + public void unloadAttributes() { if (attributes.isEmpty()) { return; } - attributes.entrySet().removeIf(entry -> !entry.getValue().keepLoaded()); + synchronized (this) { + attributes.entrySet().removeIf(entry -> !entry.getValue().keepLoaded()); + } } public List getAttributeStrings() { - int size = flags.size() + attributes.size() + attributes.size(); + int size = flags.size() + attributes.size(); if (size == 0) { return Collections.emptyList(); } diff --git a/jadx-core/src/main/java/jadx/core/dex/attributes/EmptyAttrStorage.java b/jadx-core/src/main/java/jadx/core/dex/attributes/EmptyAttrStorage.java index 54676aa61..ec854541d 100644 --- a/jadx-core/src/main/java/jadx/core/dex/attributes/EmptyAttrStorage.java +++ b/jadx-core/src/main/java/jadx/core/dex/attributes/EmptyAttrStorage.java @@ -34,11 +34,6 @@ public final class EmptyAttrStorage extends AttributeStorage { return Collections.emptyList(); } - @Override - public void clear() { - // ignore - } - @Override public void remove(AFlag flag) { // ignore diff --git a/jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java b/jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java index e97f6fdb2..501372bf8 100644 --- a/jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java @@ -351,8 +351,8 @@ public class ClassNode extends NotificationAttrNode // manually added class return; } - unload(); clearAttributes(); + unload(); root().getConstValues().removeForClass(this); load(clsData, true); diff --git a/jadx-core/src/test/java/jadx/tests/functional/AttributeStorageTest.java b/jadx-core/src/test/java/jadx/tests/functional/AttributeStorageTest.java index a44d9d1ee..88382b246 100644 --- a/jadx-core/src/test/java/jadx/tests/functional/AttributeStorageTest.java +++ b/jadx-core/src/test/java/jadx/tests/functional/AttributeStorageTest.java @@ -70,15 +70,4 @@ public class AttributeStorageTest { assertThat(storage.contains(TEST), is(true)); assertThat(storage.get(TEST), is(attr)); } - - @Test - public void clear() { - storage.add(SYNTHETIC); - storage.add(new TestAttr()); - storage.clear(); - - assertThat(storage.contains(SYNTHETIC), is(false)); - assertThat(storage.contains(TEST), is(false)); - assertThat(storage.get(TEST), nullValue()); - } }