Skip to content

Commit

Permalink
[DEX] Simplify & clean unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 30, 2024
1 parent 49351e7 commit a797b6d
Show file tree
Hide file tree
Showing 62 changed files with 1,199 additions and 624 deletions.
74 changes: 74 additions & 0 deletions src/main/java/com/reandroid/dex/common/AnnotatedItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.dex.common;

import com.reandroid.dex.key.AnnotationItemKey;
import com.reandroid.dex.key.AnnotationSetKey;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.TypeKey;

import java.util.Iterator;
import java.util.function.Predicate;

public interface AnnotatedItem {

AnnotationSetKey getAnnotation();
void setAnnotation(AnnotationSetKey annotationSet);
void clearAnnotations();

default void addAnnotation(AnnotationItemKey annotation) {
AnnotationSetKey key = getAnnotation()
.remove(annotation.getType())
.add(annotation);
setAnnotation(key);
}
default boolean removeAnnotationIf(Predicate<? super AnnotationItemKey> predicate) {
AnnotationSetKey key = getAnnotation();
AnnotationSetKey update = key.removeIf(predicate);
if (key.equals(update)) {
return false;
}
setAnnotation(update);
return true;
}

default Iterator<AnnotationItemKey> getAnnotations() {
return getAnnotation().iterator();
}
default boolean hasAnnotations() {
return !getAnnotation().isEmpty();
}
default AnnotationItemKey getAnnotation(TypeKey typeKey) {
Iterator<AnnotationItemKey> iterator = getAnnotations();
while (iterator.hasNext()) {
AnnotationItemKey key = iterator.next();
if (typeKey.equals(key.getType())) {
return key;
}
}
return null;
}
default Key getAnnotationValue(TypeKey typeKey, String name) {
AnnotationItemKey annotationItemKey = getAnnotation(typeKey);
if (annotationItemKey != null) {
return annotationItemKey.get(name);
}
return null;
}
default boolean removeAnnotation(TypeKey typeKey) {
return removeAnnotationIf(key -> key.equalsType(typeKey));
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/reandroid/dex/common/MethodHandleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ public static MethodHandleType valueOf(String name) {
return nameMap.get(name);
}

public static boolean startsWithHandleType(SmaliReader reader) {
return get(reader) != null;
}
public static MethodHandleType get(SmaliReader reader) {
if (reader.available() < 10) {
return null;
}
int position = reader.position();
MethodHandleType type = read(reader);
reader.position(position);
return type;
}

public static MethodHandleType read(SmaliReader reader) {
reader.skipWhitespaces();
int position = reader.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public TypeKey getDataTypeKey(){
public TypeKey getParentType(){
AnnotationItem parent = getParentInstance(AnnotationItem.class);
if(parent != null){
return parent.getTypeKey();
return parent.getType();
}
return null;
}
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/com/reandroid/dex/data/AnnotationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String[] getNames(){
}
@Override
public AnnotationItemKey getKey(){
return checkKey(new AnnotationItemKey(getVisibility(), getTypeKey(), getElements()));
return checkKey(new AnnotationItemKey(getVisibility(), getType(), getElements()));
}
@Override
public void setKey(Key key){
Expand Down Expand Up @@ -213,14 +213,7 @@ public int getVisibilityValue(){
}
return this.visibility.get();
}
public String getTypeName(){
TypeId typeId = getTypeId();
if(typeId != null){
return typeId.getName();
}
return null;
}
public TypeKey getTypeKey(){
public TypeKey getType() {
return (TypeKey) typeId.getKey();
}
public TypeId getTypeId(){
Expand All @@ -239,7 +232,7 @@ public void replaceKeys(Key search, Key replace){
}
}
public Iterator<IdItem> usedIds(){
TypeKey typeKey = getTypeKey();
TypeKey typeKey = getType();
if(typeKey.getTypeName().startsWith("Ldalvik/annotation/")){
return EmptyIterator.of();
}
Expand All @@ -263,7 +256,7 @@ public void merge(AnnotationItem annotationItem){
return;
}
setVisibility(annotationItem.getVisibilityValue());
setType(annotationItem.getTypeKey());
setType(annotationItem.getType());
annotationElements.ensureCapacity(annotationItem.getElementsCount());
for(AnnotationElement coming : annotationItem){
createNewElement().merge(coming);
Expand Down Expand Up @@ -320,7 +313,7 @@ public boolean equals(Object obj) {
return false;
}
AnnotationItem item = (AnnotationItem) obj;
if(!ObjectsUtil.equals(this.getTypeName(), item.getTypeName())){
if(!ObjectsUtil.equals(this.getType(), item.getType())){
return false;
}
if(this.getVisibilityValue() != item.getVisibilityValue()){
Expand All @@ -333,15 +326,15 @@ public boolean equals(Object obj) {
public int hashCode() {
return ObjectsUtil.hash(
getVisibility(),
getTypeName(),
getType(),
annotationElements);
}

@Override
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append('@');
builder.append(getTypeName());
builder.append(getType());
boolean appendOnce = false;
for(AnnotationElement element : this){
if(appendOnce){
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/com/reandroid/dex/data/AnnotationSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import com.reandroid.dex.smali.model.SmaliAnnotationItem;
import com.reandroid.dex.smali.model.SmaliAnnotationSet;
import com.reandroid.dex.value.DexValueBlock;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.FilterIterator;
import com.reandroid.utils.collection.IterableIterator;

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

public class AnnotationSet extends IntegerDataItemList<AnnotationItem>
implements KeyReference, SmaliFormat, PositionAlignedItem, FullRefresh {
Expand All @@ -42,6 +44,15 @@ public AnnotationSet(){
super(SectionType.ANNOTATION_ITEM, UsageMarker.USAGE_ANNOTATION, new DexPositionAlign());
}

public boolean remove(TypeKey typeKey) {
return removeIf(item -> ObjectsUtil.equals(typeKey, item.getType()));
}
public boolean remove(AnnotationItemKey itemKey) {
return removeIf(item -> ObjectsUtil.equals(itemKey, item.getKey()));
}
public boolean removeAnnotationIf(Predicate<? super AnnotationItemKey> predicate) {
return removeIf(item -> predicate.test(item.getKey()));
}
@Override
public boolean isBlank() {
return isEmpty();
Expand All @@ -51,7 +62,7 @@ public boolean isBlank() {
public AnnotationSetKey getKey() {
AnnotationItemKey[] elements = new AnnotationItemKey[size()];
getItemKeys(elements);
return checkKey(new AnnotationSetKey(elements));
return checkKey(AnnotationSetKey.create(elements));
}
@Override
public void setKey(Key key) {
Expand Down Expand Up @@ -81,26 +92,18 @@ public AnnotationElement getElement(TypeKey typeKey, String name){
}
public AnnotationItem get(TypeKey typeKey) {
for(AnnotationItem item : this){
if(typeKey.equals(item.getTypeKey())){
if(typeKey.equals(item.getType())){
return item;
}
}
return null;
}
public Iterator<AnnotationItem> getAll(TypeKey typeKey) {
return FilterIterator.of(iterator(), item -> typeKey.equals(item.getTypeKey()));
}
public boolean contains(String typeName) {
for(AnnotationItem item : this){
if(typeName.equals(item.getTypeName())){
return true;
}
}
return false;
return FilterIterator.of(iterator(), item -> typeKey.equals(item.getType()));
}
public boolean contains(TypeKey typeKey) {
for(AnnotationItem item : this){
if(typeKey.equals(item.getTypeKey())){
if(typeKey.equals(item.getType())){
return true;
}
}
Expand Down Expand Up @@ -133,7 +136,7 @@ public AnnotationItem addNew(TypeKey type, String name){
}
public AnnotationItem get(TypeKey type, String name){
for (AnnotationItem item : this) {
if (type.equals(item.getTypeKey())
if (type.equals(item.getType())
&& item.containsName(name)) {
return item;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/reandroid/dex/data/AnnotationsDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public void removeMethod(MethodDef def) {
methodsAnnotationMap.remove(def);
parametersAnnotationMap.remove(def);
}
public void addAnnotation(Def<?> def, AnnotationSetKey key){
addAnnotation(def, getOrCreateSectionItem(
SectionType.ANNOTATION_SET, key));
}
public void addAnnotation(Def<?> def, AnnotationSet annotationSet){
if(def instanceof FieldDef){
addFieldAnnotation((FieldDef) def, annotationSet);
Expand Down Expand Up @@ -232,6 +236,15 @@ public AnnotationSet setParameterAnnotation(MethodDef methodDef, int parameterIn
AnnotationGroup annotationGroup = getEmptyParameterAnnotationGroup(methodDef, parameterIndex);
return annotationGroup.setItemKeyAt(parameterIndex, key);
}
public void removeParameterAnnotation(MethodDef methodDef, int parameterIndex) {
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
while (iterator.hasNext()) {
AnnotationGroup group = iterator.next();
if (group != null) {
group.clearAt(parameterIndex);
}
}
}
private AnnotationGroup getEmptyParameterAnnotationGroup(MethodDef methodDef, int parameterIndex){
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
while (iterator.hasNext()){
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/com/reandroid/dex/data/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import com.reandroid.dex.smali.SmaliRegion;
import com.reandroid.dex.smali.model.Smali;
import com.reandroid.dex.smali.model.SmaliDef;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.*;

import java.io.IOException;
import java.util.Iterator;

public abstract class Def<T extends IdItem> extends FixedDexContainerWithTool implements
IdDefinition<T>, EditableItem, Comparable<Def<T>>, SmaliRegion, DefIndex, IdUsageIterator {
IdDefinition<T>, EditableItem, Comparable<Def<T>>, AnnotatedItem, SmaliRegion,
DefIndex, IdUsageIterator {

private final SectionType<T> sectionType;
private final Ule128Item relativeId;
Expand All @@ -56,6 +58,42 @@ public Def(int childesCount, SectionType<T> sectionType) {
addChild(0, relativeId);
addChild(1, accessFlags);
}
@Override
public AnnotationSetKey getAnnotation() {
return AnnotationSetKey.create(
ComputeIterator.of(getAnnotationItemBlocks(), AnnotationItem::getKey));
}
@Override
public void setAnnotation(AnnotationSetKey annotationSet) {
if (!ObjectsUtil.equals(getAnnotation(), annotationSet)) {
clearAnnotations();
writeAnnotation(annotationSet);
}
}
@Override
public void clearAnnotations() {
writeAnnotation(AnnotationSetKey.EMPTY);
}
private boolean hasAnnotationSetBlocks() {
return getAnnotationItemBlocks().hasNext();
}
private Iterator<AnnotationItem> getAnnotationItemBlocks() {
AnnotationsDirectory directory = getAnnotationsDirectory();
if (directory != null) {
return ExpandIterator.of(directory
.getAnnotations(this));
}
return EmptyIterator.of();
}
private void writeAnnotation(AnnotationSetKey key) {
if (key == null || key.isEmpty()) {
if (hasAnnotationSetBlocks()) {
getOrCreateUniqueAnnotationsDirectory().remove(this);
}
} else {
getOrCreateUniqueAnnotationsDirectory().addAnnotation(this, key);
}
}

@Override
public Iterator<? extends Modifier> getModifiers() {
Expand Down Expand Up @@ -195,7 +233,7 @@ public Iterator<AnnotationSet> getAnnotationSets(boolean skipEmpty){
}
public AnnotationsDirectory getAnnotationsDirectory(){
ClassId classId = getClassId();
if(classId != null){
if (classId != null) {
return classId.getAnnotationsDirectory();
}
return null;
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/reandroid/dex/data/EncodedArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +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;
import com.reandroid.dex.sections.SectionType;
Expand Down Expand Up @@ -51,20 +51,21 @@ public EncodedArray() {
}

@Override
public ArrayKey getKey() {
public ArrayValueKey getKey() {
int size = size();
Key[] keys = new Key[size];
for (int i = 0; i < size; i++) {
keys[i] = get(i).getKey();
}
return checkKey(new ArrayKey(keys));
return checkKey(ArrayValueKey.create(keys));
}

@Override
public void setKey(Key key) {
ArrayKey arrayKey = (ArrayKey) key;
ArrayValueKey arrayKey = (ArrayValueKey) key;
clear();
for (Key k : arrayKey) {
add(k);
int size = arrayKey.size();
for (int i = 0; i < size; i++) {
add(arrayKey.get(i));
}
}

Expand Down
Loading

0 comments on commit a797b6d

Please sign in to comment.