Skip to content

Commit

Permalink
[DEX] Simplify KeyList usages and rework RenameTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Dec 7, 2024
1 parent 55780ad commit 46cc49e
Show file tree
Hide file tree
Showing 37 changed files with 528 additions and 585 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/reandroid/dex/dalvik/DalvikInnerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ public void setAccessFlags(int flags) {
writeValue(Key.DALVIK_accessFlags, PrimitiveKey.of(flags));
}
public String getName() {
Key key = getKey().getValue(Key.DALVIK_name);
Key key = readValue(Key.DALVIK_name);
if (key instanceof StringKey) {
return ((StringKey) key).getString();
}
return null;
}
public boolean hasName() {
return readValue(Key.DALVIK_name) instanceof StringKey;
}
public void setName(String name) {
Key key = name == null? NullValueKey.INSTANCE : StringKey.create(name);
writeValue(Key.DALVIK_name, key);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/dalvik/DalvikMemberClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public void addSimpleName(TypeKey enclosing, String simpleName) {
public ArrayValueKey getArray() {
ArrayValueKey arrayValueKey = (ArrayValueKey) readValue(Key.DALVIK_value);
if (arrayValueKey == null) {
arrayValueKey = ArrayValueKey.EMPTY;
arrayValueKey = ArrayValueKey.empty();
}
return arrayValueKey;
}
public void setArray(ArrayValueKey array) {
if (array == null) {
array = ArrayValueKey.EMPTY;
array = ArrayValueKey.empty();
}
writeValue(Key.DALVIK_value, array);
}
Expand All @@ -116,7 +116,7 @@ public static DalvikMemberClass getOrCreate(AnnotatedProgram annotatedProgram) {
annotatedProgram.addAnnotation(AnnotationItemKey.create(
AnnotationVisibility.SYSTEM,
TypeKey.DALVIK_MemberClass,
AnnotationElementKey.create(Key.DALVIK_value, ArrayValueKey.EMPTY)
AnnotationElementKey.create(Key.DALVIK_value, ArrayValueKey.empty())
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/reandroid/dex/dalvik/DalvikSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Iterator<String> values() {
public ArrayValueKey getArrayKey() {
ArrayValueKey key = (ArrayValueKey) getKey().getValue(Key.DALVIK_value);
if (key == null) {
key = ArrayValueKey.EMPTY;
key = ArrayValueKey.empty();
}
return key;
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public static DalvikSignature getOrCreate(AnnotatedProgram annotatedProgram) {
annotatedProgram.addAnnotation(AnnotationItemKey.create(
AnnotationVisibility.SYSTEM,
TypeKey.DALVIK_Signature,
AnnotationElementKey.create(Key.DALVIK_value, ArrayValueKey.EMPTY)
AnnotationElementKey.create(Key.DALVIK_value, ArrayValueKey.empty())
)
);
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/reandroid/dex/data/AnnotationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ public void setType(TypeKey typeKey){
typeId.setKey(typeKey);
}

public void replaceKeys(Key search, Key replace){
for(AnnotationElement element : this){
element.replaceKeys(search, replace);
public void replaceKeys(Key search, Key replace) {
AnnotationItemKey itemKey = getKey();
AnnotationItemKey update = itemKey.replaceKey(search, replace);
if (itemKey != update) {
setKey(update);
}
}
public Iterator<IdItem> usedIds(){
Expand Down Expand Up @@ -373,9 +375,9 @@ private AnnotationItemKey getItemKey() {
public AnnotationSetKey getAnnotation() {
AnnotationItemKey key = getItemKey();
if (key != null) {
return AnnotationSetKey.create(new AnnotationItemKey[]{key});
return AnnotationSetKey.of(key);
}
return AnnotationSetKey.EMPTY;
return AnnotationSetKey.empty();
}
@Override
public void setAnnotation(AnnotationSetKey annotationSet) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/data/AnnotationSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean isBlank() {
public AnnotationSetKey getKey() {
AnnotationItemKey[] elements = new AnnotationItemKey[size()];
getItemKeys(elements);
return checkKey(AnnotationSetKey.create(elements));
return checkKey(AnnotationSetKey.of(elements));
}
@Override
public void setKey(Key key) {
Expand Down Expand Up @@ -166,10 +166,10 @@ public void replaceKeys(Key search, Key replace){
}
}
@Override
public void refreshFull(){
public void refreshFull() {
sort();
}
public boolean sort(){
public boolean sort() {
return super.sort(CollectionUtil.getComparator());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public AnnotationSet getOrCreateClassAnnotations(){
public AnnotationSetKey getClassAnnotation() {
AnnotationSetKey key = (AnnotationSetKey) header.classAnnotation.getKey();
if (key == null) {
key = AnnotationSetKey.EMPTY;
key = AnnotationSetKey.empty();
}
return key;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/data/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setAnnotation(AnnotationSetKey annotationSet) {
}
@Override
public void clearAnnotations() {
writeAnnotation(AnnotationSetKey.EMPTY);
writeAnnotation(AnnotationSetKey.empty());
}
private boolean hasAnnotationSetBlocks() {
return getAnnotationItemBlocks().hasNext();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/reandroid/dex/data/EncodedArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.dex.base.Ule128Item;
import com.reandroid.dex.id.IdItem;
import com.reandroid.dex.key.ArrayKey;
import com.reandroid.dex.key.ArrayValueKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.KeyReference;
Expand Down Expand Up @@ -57,11 +58,11 @@ public ArrayValueKey getKey() {
for (int i = 0; i < size; i++) {
keys[i] = get(i).getKey();
}
return checkKey(ArrayValueKey.create(keys));
return checkKey(ArrayValueKey.of(keys));
}
@Override
public void setKey(Key key) {
ArrayValueKey arrayKey = (ArrayValueKey) key;
ArrayKey<?> arrayKey = (ArrayKey<?>) key;
clear();
int size = arrayKey.size();
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public boolean removeIf(Predicate<? super T> filter) {
return referenceList.removeIf(reference -> filter.test(reference.getItem()));
}
void removeNulls() {
removeIf(item -> item == null);
referenceList.removeIf(reference -> reference.getItem() == null);
}
@Override
public Iterator<T> iterator() {
Expand All @@ -157,6 +157,7 @@ public boolean isEmpty() {
return !iterator().hasNext();
}
public boolean sort(Comparator<? super T> comparator) {
removeNulls();
return referenceList.sort((ref1, ref2) -> comparator.compare(ref1.getItem(), ref2.getItem()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/data/MethodParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setAnnotation(AnnotationSetKey annotationSet) {
}
@Override
public void clearAnnotations() {
writeAnnotation(AnnotationSetKey.EMPTY);
writeAnnotation(AnnotationSetKey.empty());
}

public void onRemoved() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private ArrayValueKey buildCachedKey() {
elements[i] = key;
fieldDef.cachedStaticValue(null);
}
return ArrayValueKey.create(elements);
return ArrayValueKey.of(elements);
}
private int lastCachedKeyIndex() {
int result = -1;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/id/CallSiteId.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ public void setProto(ProtoKey protoKey){
setKey(getKey().changeProto(protoKey));
}
}
public ArrayKey getArguments() {
public ArrayValueKey getArguments() {
int size = getArgumentsSize();
Key[] results = new Key[size];
for (int i = 0; i < size; i++) {
results[i] = getArgument(i);
}
return ArrayKey.create(results);
return ArrayValueKey.of(results);
}
public Iterator<DexValueBlock<?>> getArgumentValues() {
EncodedArray encodedArray = getEncodedArray();
Expand Down Expand Up @@ -138,7 +138,7 @@ public int getArgumentsSize() {
}
return 0;
}
public void setArguments(ArrayValueKey key) {
public void setArguments(ArrayKey<?> key) {
if (!key.equals(getArguments())) {
setKey(getKey().changeArguments(key));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/id/ClassId.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public AnnotationSetKey getAnnotation() {
if (directory != null) {
return directory.getClassAnnotation();
}
return AnnotationSetKey.EMPTY;
return AnnotationSetKey.empty();
}
@Override
public void setAnnotation(AnnotationSetKey annotationSet) {
Expand All @@ -110,7 +110,7 @@ public void setAnnotation(AnnotationSetKey annotationSet) {
}
@Override
public void clearAnnotations() {
writeAnnotation(AnnotationSetKey.EMPTY);
writeAnnotation(AnnotationSetKey.empty());
}
@Override
public boolean hasAnnotations() {
Expand Down Expand Up @@ -189,7 +189,7 @@ public Iterator<TypeKey> getInstanceKeys(){
public TypeListKey getInterfacesKey() {
TypeListKey typeListKey = interfaces.getKey();
if (typeListKey == null) {
typeListKey = TypeListKey.EMPTY;
typeListKey = TypeListKey.empty();
}
return typeListKey;
}
Expand Down
82 changes: 16 additions & 66 deletions src/main/java/com/reandroid/dex/key/AnnotationItemKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import com.reandroid.utils.collection.*;

import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.function.Predicate;

public class AnnotationItemKey extends KeyList<AnnotationElementKey> implements Key, Iterable<AnnotationElementKey> {

private static final AnnotationElementKey[] EMPTY_ARRAY = new AnnotationElementKey[0];

private final AnnotationVisibility visibility;
private final TypeKey type;

private AnnotationItemKey(AnnotationVisibility visibility, TypeKey type, AnnotationElementKey[] elements) {
super(elements);
private AnnotationItemKey(AnnotationVisibility visibility, TypeKey type, Key[] elements) {
super(elements, true);
this.visibility = visibility;
this.type = type;
}
Expand All @@ -53,7 +52,7 @@ public AnnotationItemKey changeType(TypeKey typeKey) {
if (typeKey.equals(getType())) {
return this;
}
return create(getVisibility(), typeKey, getElements());
return createKey(getVisibility(), typeKey, getElements());
}
public AnnotationItemKey remove(String name) {
return removeIf(elementKey -> ObjectsUtil.equals(elementKey.getName(), name));
Expand Down Expand Up @@ -87,7 +86,7 @@ public AnnotationItemKey changeVisibility(AnnotationVisibility visibility) {
if (ObjectsUtil.equals(getVisibility(), visibility)) {
return this;
}
return create(visibility, getType(), getElements());
return createKey(visibility, getType(), getElements());
}
public AnnotationElementKey get(String name) {
int size = size();
Expand Down Expand Up @@ -152,39 +151,18 @@ public AnnotationItemKey set(int i, AnnotationElementKey item) {
public AnnotationItemKey sorted() {
return (AnnotationItemKey) super.sorted();
}

@Override
AnnotationItemKey newInstance(AnnotationElementKey[] elements) {
return create(getVisibility(), getType(), elements);
public AnnotationItemKey clearDuplicates() {
return (AnnotationItemKey) super.clearDuplicates();
}
@Override
AnnotationElementKey[] newArray(int length) {
if (length == 0) {
return EMPTY_ARRAY;
}
return new AnnotationElementKey[length];
public AnnotationItemKey clearDuplicates(Comparator<? super AnnotationElementKey> comparator) {
return (AnnotationItemKey) super.clearDuplicates(comparator);
}

@Override
AnnotationElementKey[] initializeSortedElements(AnnotationElementKey[] elements) {
if (elements == null || elements.length < 2) {
return null;
}
boolean needsSort = false;
int length = elements.length;
AnnotationElementKey previous = elements[0];
for (int i = 1; i < length; i ++) {
AnnotationElementKey next = elements[i];
if (CompareUtil.compare(previous, next) > 0) {
needsSort = true;
break;
}
}
if (!needsSort) {
return null;
}
elements = elements.clone();
ArraySort.sort(elements, CompareUtil.getComparableComparator());
return elements;
AnnotationItemKey newInstance(Key[] elements) {
return createKey(getVisibility(), getType(), elements);
}

public SmaliDirective getSmaliDirective() {
Expand Down Expand Up @@ -265,11 +243,13 @@ public int hashCode() {
}

public static AnnotationItemKey create(AnnotationVisibility visibility, TypeKey typeKey, AnnotationElementKey ... elements) {
return createKey(visibility, typeKey, elements);
}
public static AnnotationItemKey createKey(AnnotationVisibility visibility, TypeKey typeKey, Key[] elements) {
if (typeKey == null) {
return null;
}
elements = removeNulls(elements);
return new AnnotationItemKey(visibility, typeKey, elements);
return new AnnotationItemKey(visibility, typeKey, removeNulls(elements));
}

public static AnnotationItemKey read(SmaliReader reader) throws IOException {
Expand All @@ -280,34 +260,4 @@ public static AnnotationItemKey parse(String text) {
//FIXME
throw new RuntimeException("AnnotationItemKey.parse not implemented");
}

private static AnnotationElementKey[] removeNulls(AnnotationElementKey[] elements) {
if (elements == null || elements.length == 0) {
return EMPTY_ARRAY;
}
int length = elements.length;
int size = 0;
for (int i = 0; i < length; i ++) {
AnnotationElementKey key = elements[i];
if (key != null) {
size ++;
}
}
if (size == length) {
return elements;
}
if (size == 0) {
return EMPTY_ARRAY;
}
AnnotationElementKey[] results = new AnnotationElementKey[size];
int j = 0;
for (int i = 0; i < length; i ++) {
AnnotationElementKey key = elements[i];
if (key != null) {
results[j] = key;
j ++;
}
}
return results;
}
}
Loading

0 comments on commit 46cc49e

Please sign in to comment.