Skip to content

Commit

Permalink
[DEX] Full support of dex version 041 multi-layout structure
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 7, 2024
1 parent 47015d6 commit 67501ee
Show file tree
Hide file tree
Showing 43 changed files with 2,738 additions and 1,235 deletions.
33 changes: 13 additions & 20 deletions src/main/java/com/reandroid/dex/data/AnnotationGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.reandroid.dex.base.UsageMarker;
import com.reandroid.dex.id.IdItem;
import com.reandroid.dex.key.DataKey;
import com.reandroid.dex.key.ArrayKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.KeyReference;
import com.reandroid.dex.sections.SectionType;
Expand All @@ -27,22 +27,17 @@

public class AnnotationGroup extends IntegerDataItemList<AnnotationSet> implements KeyReference {

private final DataKey<AnnotationGroup> mKey;

public AnnotationGroup() {
super(SectionType.ANNOTATION_SET, UsageMarker.USAGE_ANNOTATION, null);
this.mKey = new DataKey<>(this);
}

@Override
public DataKey<AnnotationGroup> getKey() {
return mKey;
public ArrayKey getKey() {
return (ArrayKey) checkKey(super.getKey());
}
@SuppressWarnings("unchecked")
@Override
public void setKey(Key key){
DataKey<AnnotationGroup> dataKey = (DataKey<AnnotationGroup>) key;
merge(dataKey.getItem());
super.setKey(key);
}
@Override
public SectionType<AnnotationGroup> getSectionType() {
Expand All @@ -65,17 +60,15 @@ public Iterator<IdItem> iterator(AnnotationSet element) {
};
}

public void merge(AnnotationGroup annotationGroup){
AnnotationSet[] comingSets = annotationGroup.getItems();
if(comingSets == null){
return;
}
for(AnnotationSet comingSet : comingSets){
if(comingSet == null){
addNull();
continue;
}
addNew(comingSet.getKey());
public void merge(AnnotationGroup annotationGroup) {
int size = annotationGroup.size();
for (int i = 0; i < size; i++) {
addNewItem(annotationGroup.getItemKey(i));
}
}

@Override
public String toString() {
return getKey().toString("\n");
}
}
44 changes: 29 additions & 15 deletions src/main/java/com/reandroid/dex/data/AnnotationSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@
public class AnnotationSet extends IntegerDataItemList<AnnotationItem>
implements KeyReference, SmaliFormat, PositionAlignedItem, FullRefresh {

private final DataKey<AnnotationSet> mKey;

public AnnotationSet(){
super(SectionType.ANNOTATION_ITEM, UsageMarker.USAGE_ANNOTATION, new DexPositionAlign());
this.mKey = new DataKey<>(this);
}

@Override
public DataKey<AnnotationSet> getKey() {
return mKey;
public AnnotationSetKey getKey() {
AnnotationItemKey[] elements = new AnnotationItemKey[size()];
getItemKeys(elements);
return checkKey(new AnnotationSetKey(elements));
}
@SuppressWarnings("unchecked")
@Override
public void setKey(Key key){
DataKey<AnnotationSet> dataKey = (DataKey<AnnotationSet>) key;
merge(dataKey.getItem());
public void setKey(Key key) {
super.setKey(key);
}
@Override
public SectionType<AnnotationSet> getSectionType() {
return SectionType.ANNOTATION_SET;
}

public AnnotationItemKey getItemKey(int i) {
return (AnnotationItemKey) super.getItemKey(i);
}
public DexValueBlock<?> getValue(TypeKey typeKey, String name){
AnnotationElement element = getElement(typeKey, name);
if(element != null){
Expand Down Expand Up @@ -116,12 +116,12 @@ public AnnotationItem getOrCreate(TypeKey type, String name){
return addNew(type, name);
}
public AnnotationItem addNewItem(TypeKey typeKey){
AnnotationItem item = addNew();
AnnotationItem item = addNewItem();
item.setType(typeKey);
return item;
}
public AnnotationItem addNew(TypeKey type, String name){
AnnotationItem item = addNew();
AnnotationItem item = addNewItem();
item.setType(type);
item.getOrCreateElement(name);
return item;
Expand All @@ -135,6 +135,21 @@ public AnnotationItem get(TypeKey type, String name){
}
return null;
}
public AnnotationItem getOrCreate(AnnotationItemKey annotationItemKey) {
AnnotationItem item = get(annotationItemKey);
if (item == null) {
item = addNewItem(annotationItemKey);
}
return item;
}
public AnnotationItem get(AnnotationItemKey annotationItemKey){
for (AnnotationItem item : this) {
if (annotationItemKey.equals(item.getKey())) {
return item;
}
}
return null;
}

public void replaceKeys(Key search, Key replace){
for(AnnotationItem annotationItem : this){
Expand Down Expand Up @@ -162,15 +177,14 @@ public void merge(AnnotationSet annotationSet){
return;
}
for(AnnotationItem coming : annotationSet){
addNew(coming.getKey());
addNewItem(coming.getKey());
}
}
public void fromSmali(SmaliAnnotationSet smaliAnnotationSet){
Iterator<SmaliAnnotationItem> iterator = smaliAnnotationSet.iterator();
while (iterator.hasNext()){
while (iterator.hasNext()) {
SmaliAnnotationItem smaliAnnotationItem = iterator.next();
AnnotationItem annotationItem = addNewItem(smaliAnnotationItem.getType());
annotationItem.fromSmali(smaliAnnotationItem);
getOrCreate(smaliAnnotationItem.getKey());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.reandroid.dex.common.SectionItem;
import com.reandroid.dex.base.UsageMarker;
import com.reandroid.dex.id.IdItem;
import com.reandroid.dex.key.AnnotationSetKey;
import com.reandroid.dex.key.DataKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.KeyReference;
Expand Down Expand Up @@ -81,6 +82,9 @@ public SectionType<AnnotationsDirectory> getSectionType() {
return SectionType.ANNOTATION_DIRECTORY;
}

public void setClassAnnotations(AnnotationSetKey setKey) {
header.classAnnotation.setItem(setKey);
}
public AnnotationSet getOrCreateClassAnnotations(){
return header.classAnnotation.getOrCreate();
}
Expand Down Expand Up @@ -220,6 +224,10 @@ public AnnotationSet createNewParameterAnnotation(MethodDef methodDef, int param
AnnotationGroup annotationGroup = getEmptyParameterAnnotationGroup(methodDef, parameterIndex);
return annotationGroup.getOrCreateAt(parameterIndex);
}
public AnnotationSet setParameterAnnotation(MethodDef methodDef, int parameterIndex, AnnotationSetKey key){
AnnotationGroup annotationGroup = getEmptyParameterAnnotationGroup(methodDef, parameterIndex);
return annotationGroup.setItemKeyAt(parameterIndex, key);
}
private AnnotationGroup getEmptyParameterAnnotationGroup(MethodDef methodDef, int parameterIndex){
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
while (iterator.hasNext()){
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/reandroid/dex/data/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.reandroid.dex.common.*;
import com.reandroid.dex.id.ClassId;
import com.reandroid.dex.id.IdItem;
import com.reandroid.dex.key.AnnotationSetKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.TypeKey;
import com.reandroid.dex.pool.DexSectionPool;
Expand Down Expand Up @@ -149,6 +150,10 @@ public AnnotationSet getOrCreateAnnotationSet(){
directory.addAnnotation(this, annotationSet);
return annotationSet;
}
public void addAnnotationSet(AnnotationSetKey key) {
AnnotationSet annotationSet = getOrCreateSectionItem(SectionType.ANNOTATION_SET, key);
addAnnotationSet(annotationSet);
}
public void addAnnotationSet(AnnotationSet annotationSet){
AnnotationsDirectory directory = getOrCreateUniqueAnnotationsDirectory();
addAnnotationSet(directory, annotationSet);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/reandroid/dex/data/FieldDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public void fromSmali(SmaliField smaliField){
setAccessFlagsValue(smaliField.getAccessFlagsValue());
addHiddenApiFlags(smaliField.getHiddenApiFlags());
if(smaliField.hasAnnotation()){
AnnotationSet annotationSet = getOrCreateSection(SectionType.ANNOTATION_SET).createItem();
annotationSet.fromSmali(smaliField.getAnnotation());
addAnnotationSet(annotationSet);
addAnnotationSet(smaliField.getAnnotationSetKey());
}
SmaliValue smaliValue = smaliField.getValue();
if(smaliValue != null) {
Expand Down
Loading

0 comments on commit 67501ee

Please sign in to comment.