Skip to content

Commit

Permalink
[DEX] Simplify annotation accessing
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Apr 25, 2024
1 parent 830fa96 commit f8c8c98
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/data/AnnotationElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public void setValue(DexValueBlock<?> dexValue){
public boolean is(DexValueType<?> valueType){
return getValueType() == valueType;
}
public boolean is(MethodKey methodKey) {
return methodKey != null &&
methodKey.equalsIgnoreReturnType(getMethodKey());
}
public DexValueType<?> getValueType(){
DexValueBlock<?> value = getValue();
if(value != null){
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/reandroid/dex/data/AnnotationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public AnnotationItem(){
public SectionType<AnnotationItem> getSectionType() {
return SectionType.ANNOTATION_ITEM;
}
public void remove(Predicate<AnnotationElement> filter){
annotationElements.remove(filter);
public int remove(Predicate<AnnotationElement> filter){
return annotationElements.remove(filter);
}
public void remove(AnnotationElement element){
annotationElements.remove(element);
Expand Down Expand Up @@ -158,6 +158,9 @@ public Iterator<AnnotationElement> clonedIterator(){
public int getElementsCount(){
return annotationElements.size();
}
public boolean isEmpty() {
return annotationElements.size() == 0;
}
public AnnotationElement getElement(int index){
return annotationElements.get(index);
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/reandroid/dex/model/DexClassRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.common.SectionItem;
import com.reandroid.dex.data.AnnotationElement;
import com.reandroid.dex.data.AnnotationItem;
import com.reandroid.dex.key.FieldKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.MethodKey;
import com.reandroid.dex.key.TypeKey;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.utils.collection.FilterIterator;
import com.reandroid.utils.collection.IterableIterator;
import com.reandroid.utils.collection.UniqueIterator;

Expand All @@ -38,7 +41,11 @@ public interface DexClassRepository {
<T extends SectionItem> Iterator<T> getClonedItems(SectionType<T> sectionType);
<T1 extends SectionItem> Iterator<T1> getItems(SectionType<T1> sectionType, Key key);
<T1 extends SectionItem> T1 getItem(SectionType<T1> sectionType, Key key);
<T1 extends SectionItem> int removeEntries(SectionType<T1> sectionType, Predicate<T1> filter);

default <T extends SectionItem> Iterator<T> getClonedItems(SectionType<T> sectionType, Predicate<? super T> filter) {
return FilterIterator.of(getClonedItems(sectionType), filter);
}

default Iterator<DexClass> findUserClasses(Key key){
return new UniqueIterator<>(getDexClasses(),
Expand Down Expand Up @@ -101,4 +108,30 @@ public Iterator<DexField> iterator(DexClass dexClass) {
default Iterator<IntegerReference> visitIntegers(){
return new DexIntegerVisitor(this);
}

default int removeAnnotations(TypeKey typeKey) {
return removeEntries(SectionType.ANNOTATION_ITEM,
annotationItem -> typeKey.equals(annotationItem.getTypeKey()));
}
default int removeAnnotationElements(MethodKey methodKey) {
int removeCount = 0;

TypeKey typeKey = methodKey.getDeclaring();
Predicate<AnnotationElement> elementFilter = element -> element.is(methodKey);

Iterator<AnnotationItem> iterator = getClonedItems(SectionType.ANNOTATION_ITEM);
while (iterator.hasNext()) {
AnnotationItem annotationItem = iterator.next();
if(typeKey.equals(methodKey.getDeclaring())) {
int count = annotationItem.remove(elementFilter);
if(count != 0){
if(annotationItem.isEmpty()) {
annotationItem.removeSelf();
}
removeCount += count;
}
}
}
return removeCount;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/reandroid/dex/model/DexDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public boolean removeDexClass(TypeKey typeKey){
}
return false;
}
@Override
public <T1 extends SectionItem> int removeEntries(SectionType<T1> sectionType, Predicate<T1> filter){
Iterator<DexFile> iterator = clonedIterator();
int result = 0;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/reandroid/dex/model/DexFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public int removeClasses(Predicate<DexClass> filter){
Predicate<ClassId> classIdFilter = classId -> filter.test(DexFile.this.create(classId));
return getDexLayout().removeEntries(SectionType.CLASS_ID, classIdFilter);
}
@Override
public <T1 extends SectionItem> int removeEntries(SectionType<T1> sectionType, Predicate<T1> filter){
return getDexLayout().removeEntries(sectionType, filter);
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/reandroid/dex/model/DexValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,23 @@ public void setFloat(float f) {
}
public Number getNumber() {
DexValueBlock<?> value = getDexValueBlock();
if(value instanceof PrimitiveValue){
return ((PrimitiveValue)value).getNumber();
if(value instanceof PrimitiveValueBlock){
return ((PrimitiveValueBlock)value).getData();
}
return null;
}
public void setNumber(Number number) {
DexValueBlock<?> value = getDexValueBlock();
if(value instanceof PrimitiveValue){
((PrimitiveValue)value).setNumber(number);
if(value instanceof PrimitiveValueBlock){
((PrimitiveValueBlock)value).setData(number);
}
}
public Object getData() {
return getDexValueBlock().getData();
}
public void setData(Object data) {
getDexValueBlock().setData(data);
}
public DexAnnotation getAnnotation(){
DexValueBlock<?> value = getDexValueBlock();
if(value instanceof AnnotationValue){
Expand All @@ -188,8 +194,8 @@ public int getAsInteger() {
}
public int getAsInteger(int def) {
DexValueBlock<?> value = getDexValueBlock();
if(value instanceof PrimitiveValue){
return (int) ((PrimitiveValue)value).getNumberValue();
if(value instanceof PrimitiveValueBlock){
return (int) ((PrimitiveValueBlock)value).getNumberValue();
}
return def;
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/reandroid/dex/model/RField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import com.reandroid.arsc.item.TypeString;
import com.reandroid.arsc.model.ResourceEntry;
import com.reandroid.arsc.value.Entry;
import com.reandroid.dex.common.AccessFlag;
import com.reandroid.dex.data.FieldDef;
import com.reandroid.dex.value.DexValueBlock;
import com.reandroid.dex.value.DexValueType;
import com.reandroid.dex.value.IntValue;
import com.reandroid.dex.value.PrimitiveValue;
import com.reandroid.dex.value.PrimitiveValueBlock;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.collection.EmptyIterator;
import org.xmlpull.v1.XmlSerializer;
Expand Down Expand Up @@ -119,8 +118,8 @@ public String toString() {
}

static boolean isResourceIdValue(DexValueBlock<?> dexValueBlock) {
if(dexValueBlock instanceof PrimitiveValue){
long value = ((PrimitiveValue)dexValueBlock).getNumberValue();
if(dexValueBlock instanceof PrimitiveValueBlock){
long value = ((PrimitiveValueBlock)dexValueBlock).getNumberValue();
if((value & 0xffffffff00000000L) != 0){
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/reandroid/dex/value/ArrayValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public TypeKey getDataTypeKey() {
}
return TypeKey.OBJECT.setArrayDimension(1);
}
@Override
public Object[] getData() {
int size = size();
Object[] result = new Object[size];
for(int i = 0; i < size; i++) {
result[i] = get(i).getData();
}
return result;
}

@Override
public void append(SmaliWriter writer) throws IOException {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/reandroid/dex/value/BooleanValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public String getAsString() {
public TypeKey getDataTypeKey() {
return TypeKey.TYPE_Z;
}
@Override
public Boolean getData() {
if(get()) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
@Override
public void setData(Object data) {
set((Boolean) data);
}

@Override
public int hashCode() {
int hash = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/ByteValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
import com.reandroid.dex.smali.model.SmaliValueByte;
import com.reandroid.utils.HexUtil;

public class ByteValue extends PrimitiveValue {
public class ByteValue extends PrimitiveValueBlock {

public ByteValue() {
super(DexValueType.BYTE);
}

@Override
public Byte getNumber() {
public Byte getData() {
return get();
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
this.set((Byte) number);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/CharValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

import java.io.IOException;

public class CharValue extends PrimitiveValue {
public class CharValue extends PrimitiveValueBlock {

public CharValue(){
super(DexValueType.CHAR);
}

@Override
public Number getNumber() {
public Number getData() {
int i = (int) getNumberValue();
if((i & 0xff) == i){
return (byte) i;
Expand All @@ -42,7 +42,7 @@ public Number getNumber() {
return i;
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
if(number == null){
throw new NullPointerException();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/reandroid/dex/value/DexValueBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public boolean is(DexValueType<?> dexValueType){
public TypeKey getDataTypeKey(){
return TypeKey.OBJECT;
}
public Object getData() {
return null;
}
public void setData(Object data) {
throw new RuntimeException("Method not implemented");
}

@Override
public int hashCode() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/DoubleValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

import java.io.IOException;

public class DoubleValue extends PrimitiveValue {
public class DoubleValue extends PrimitiveValueBlock {

public DoubleValue() {
super(DexValueType.DOUBLE);
}

@Override
public Double getNumber() {
public Double getData() {
return get();
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
this.set((Double) number);
}
public double get(){
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/FloatValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

import java.io.IOException;

public class FloatValue extends PrimitiveValue {
public class FloatValue extends PrimitiveValueBlock {

public FloatValue() {
super(DexValueType.FLOAT);
}

@Override
public Float getNumber() {
public Float getData() {
return get();
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
this.set((Float) number);
}
public float get(){
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/IntValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@

import java.io.IOException;

public class IntValue extends PrimitiveValue implements IntegerReference {
public class IntValue extends PrimitiveValueBlock implements IntegerReference {

public IntValue() {
super(DexValueType.INT);
}

@Override
public Integer getNumber() {
public Integer getData() {
return get();
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
this.set((Integer) number);
}
@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/reandroid/dex/value/LongValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

import java.io.IOException;

public class LongValue extends PrimitiveValue {
public class LongValue extends PrimitiveValueBlock {

public LongValue() {
super(DexValueType.LONG);
}

@Override
public Long getNumber() {
public Long getData() {
return get();
}
@Override
public void setNumber(Number number) {
public void setData(Number number) {
this.set((Long) number);
}
public long get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@

import java.io.IOException;

public abstract class PrimitiveValue extends DexValueBlock<NumberValue> {
public abstract class PrimitiveValueBlock extends DexValueBlock<NumberValue> {

public PrimitiveValue(DexValueType<?> type) {
public PrimitiveValueBlock(DexValueType<?> type) {
super(new NumberValue(), type);
}

public abstract Number getNumber();
public abstract void setNumber(Number number);
@Override
public abstract Number getData();
@Override
public void setData(Object number) {
setData((Number) number);
}
public abstract void setData(Number number);

public long getNumberValue(){
return getValueContainer().getNumberValue();
Expand Down Expand Up @@ -60,7 +65,7 @@ public void onReadBytes(BlockReader reader) throws IOException {
@Override
public void merge(DexValueBlock<?> valueBlock){
super.merge(valueBlock);
PrimitiveValue coming = (PrimitiveValue) valueBlock;
PrimitiveValueBlock coming = (PrimitiveValueBlock) valueBlock;
getValueContainer().merge(coming.getValueContainer());
}
@Override
Expand Down
Loading

0 comments on commit f8c8c98

Please sign in to comment.