Skip to content

Commit

Permalink
[DEX] Rework and simplify annotation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Dec 1, 2024
1 parent b893c29 commit cc90c22
Show file tree
Hide file tree
Showing 24 changed files with 661 additions and 649 deletions.
31 changes: 12 additions & 19 deletions src/main/java/com/reandroid/dex/common/AnnotatedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.TypeKey;

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

public interface AnnotatedItem {
Expand All @@ -31,7 +30,6 @@ public interface AnnotatedItem {

default void addAnnotation(AnnotationItemKey annotation) {
AnnotationSetKey key = getAnnotation()
.remove(annotation.getType())
.add(annotation);
setAnnotation(key);
}
Expand All @@ -45,30 +43,25 @@ default boolean removeAnnotationIf(Predicate<? super AnnotationItemKey> predicat
return true;
}

default Iterator<AnnotationItemKey> getAnnotations() {
return getAnnotation().iterator();
}
default boolean hasAnnotations() {
return !getAnnotation().isEmpty();
}
default boolean hasAnnotation(TypeKey typeKey) {
return getAnnotation(typeKey) != null;
}
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;
return getAnnotation().get(typeKey);
}
default Key getAnnotationValue(TypeKey typeKey, String name) {
AnnotationItemKey annotationItemKey = getAnnotation(typeKey);
if (annotationItemKey != null) {
return annotationItemKey.get(name);
}
return null;
return getAnnotation().getAnnotationValue(typeKey, name);
}
default boolean removeAnnotation(TypeKey typeKey) {
return removeAnnotationIf(key -> key.equalsType(typeKey));
AnnotationSetKey annotation = getAnnotation();
AnnotationSetKey update = annotation.remove(typeKey);
if (annotation.equals(update)) {
return false;
}
setAnnotation(update);
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/common/IdDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.util.Iterator;

public interface IdDefinition<T extends IdItem> {
public interface IdDefinition<T extends IdItem> extends AnnotatedItem {
T getId();
int getAccessFlagsValue();
Iterator<? extends Modifier> getAccessFlags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AnnotationElement() {

@Override
public AnnotationElementKey getKey(){
return new AnnotationElementKey(getName(), getValue());
return AnnotationElementKey.create(getName(), getValue());
}
@Override
public void setKey(Key key) {
Expand Down
2 changes: 1 addition & 1 deletion 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(), getType(), getElements()));
return checkKey(AnnotationItemKey.create(getVisibility(), getType(), getElements()));
}
@Override
public void setKey(Key key){
Expand Down
66 changes: 22 additions & 44 deletions src/main/java/com/reandroid/dex/data/AnnotationsDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ public void setClassAnnotations(AnnotationSetKey setKey) {
public AnnotationSet getOrCreateClassAnnotations(){
return header.classAnnotation.getOrCreate();
}
public AnnotationSet getClassAnnotations(){
public AnnotationSetKey getClassAnnotation() {
AnnotationSetKey key = (AnnotationSetKey) header.classAnnotation.getKey();
if (key == null) {
key = AnnotationSetKey.EMPTY;
}
return key;
}
public boolean hasClassAnnotation() {
AnnotationSet annotationSet = header.classAnnotation.getItem();
if (annotationSet != null) {
return !annotationSet.isEmpty();
}
return false;
}
public AnnotationSet getClassAnnotationBlock() {
return header.classAnnotation.getItem();
}
public void setClassAnnotations(AnnotationSet annotationSet){
Expand All @@ -99,20 +113,12 @@ public void setClassAnnotations(AnnotationSet annotationSet){
public boolean isBlank() {
return isEmpty();
}
public boolean isEmpty(){
return getClassAnnotations() == null &&
public boolean isEmpty() {
return !hasClassAnnotation() &&
fieldsAnnotationMap.isEmpty() &&
methodsAnnotationMap.isEmpty() &&
parametersAnnotationMap.isEmpty();
}
public int countField(){
return fieldsAnnotationMap.getCount() ;
}
public int countMethod(){
return methodsAnnotationMap.getCount() +
parametersAnnotationMap.getCount();
}


public void sortFields() {
fieldsAnnotationMap.sort();
Expand Down Expand Up @@ -187,21 +193,12 @@ public Iterator<AnnotationSet> getAnnotations(Def<?> def){
public Iterator<AnnotationSet> getFieldsAnnotation(FieldDef fieldDef){
return fieldsAnnotationMap.getValues(fieldDef);
}
public Iterator<AnnotationSet> getFieldsAnnotation(int index){
return fieldsAnnotationMap.getValues(index);
}
public Iterator<AnnotationSet> getMethodAnnotation(int index){
return methodsAnnotationMap.getValues(index);
}
public Iterator<AnnotationSet> getMethodAnnotation(MethodDef methodDef){
return methodsAnnotationMap.getValues(methodDef);
}
public Iterator<AnnotationGroup> getParameterAnnotation(MethodDef methodDef){
return parametersAnnotationMap.getValues(methodDef);
}
public Iterator<DirectoryEntry<MethodDef, AnnotationGroup>> getParameterEntries(MethodDef methodDef){
return parametersAnnotationMap.getEntries(methodDef);
}
public Iterator<AnnotationGroup> getParameterAnnotation(int methodIndex){
return parametersAnnotationMap.getValues(methodIndex);
}
Expand All @@ -213,28 +210,9 @@ public Iterator<AnnotationSet> getParameterAnnotation(int methodIndex, int param
return ComputeIterator.of(getParameterAnnotation(methodIndex),
annotationGroup -> annotationGroup.getItem(parameterIndex));
}
public AnnotationSet getOrCreateParameterAnnotation(MethodDef methodDef, int parameterIndex){
AnnotationGroup annotationGroup = getOrCreateParameterAnnotationGroup(methodDef);
return annotationGroup.getOrCreateAt(parameterIndex);
}
private AnnotationGroup getOrCreateParameterAnnotationGroup(MethodDef methodDef){
AnnotationGroup annotationGroup;
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
if(iterator.hasNext()){
annotationGroup = iterator.next();
}else {
annotationGroup = getOrCreateSection(SectionType.ANNOTATION_GROUP).createItem();
this.parametersAnnotationMap.add(methodDef, annotationGroup);
}
return annotationGroup;
}
public AnnotationSet createNewParameterAnnotation(MethodDef methodDef, int parameterIndex){
AnnotationGroup annotationGroup = getEmptyParameterAnnotationGroup(methodDef, parameterIndex);
return annotationGroup.getOrCreateAt(parameterIndex);
}
public AnnotationSet setParameterAnnotation(MethodDef methodDef, int parameterIndex, AnnotationSetKey key){
public void setParameterAnnotation(MethodDef methodDef, int parameterIndex, AnnotationSetKey key) {
AnnotationGroup annotationGroup = getEmptyParameterAnnotationGroup(methodDef, parameterIndex);
return annotationGroup.setItemKeyAt(parameterIndex, key);
annotationGroup.setItemKeyAt(parameterIndex, key);
}
public void removeParameterAnnotation(MethodDef methodDef, int parameterIndex) {
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
Expand All @@ -245,7 +223,7 @@ public void removeParameterAnnotation(MethodDef methodDef, int parameterIndex) {
}
}
}
private AnnotationGroup getEmptyParameterAnnotationGroup(MethodDef methodDef, int parameterIndex){
private AnnotationGroup getEmptyParameterAnnotationGroup(MethodDef methodDef, int parameterIndex) {
Iterator<AnnotationGroup> iterator = parametersAnnotationMap.getValues(methodDef);
while (iterator.hasNext()){
AnnotationGroup group = iterator.next();
Expand All @@ -259,7 +237,7 @@ private AnnotationGroup getEmptyParameterAnnotationGroup(MethodDef methodDef, in
}

public void replaceKeys(Key search, Key replace){
AnnotationSet set = getClassAnnotations();
AnnotationSet set = getClassAnnotationBlock();
if(set != null){
set.replaceKeys(search, replace);
}
Expand All @@ -285,7 +263,7 @@ public void editInternal(Block user) {
}

public Iterator<IdItem> usedIds(){
AnnotationSet classAnnotation = getClassAnnotations();
AnnotationSet classAnnotation = getClassAnnotationBlock();
Iterator<IdItem> iterator1;
if(classAnnotation == null){
iterator1 = EmptyIterator.of();
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/com/reandroid/dex/data/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,6 @@ public void setKey(Key key){
setItem(key);
}

public AnnotationSet getOrCreateAnnotationSet(){
AnnotationSet annotationSet = CollectionUtil.getFirst(getAnnotationSets());
if(annotationSet != null){
return annotationSet;
}
AnnotationsDirectory directory = getOrCreateUniqueAnnotationsDirectory();
annotationSet = directory.createSectionItem(SectionType.ANNOTATION_SET);
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);
}
void addAnnotationSet(AnnotationsDirectory directory, AnnotationSet annotationSet){
directory.addAnnotation(this, annotationSet);
}
public Iterator<AnnotationItemKey> getAnnotationKeys() {
Iterator<AnnotationSetKey> iterator = getAnnotationSetKeys();
if (!iterator.hasNext()) {
Expand All @@ -217,9 +196,6 @@ public Iterator<AnnotationItemKey> iterator(AnnotationSetKey element) {
public Iterator<AnnotationSetKey> getAnnotationSetKeys() {
return ComputeIterator.of(getAnnotationSets(true), AnnotationSet::getKey);
}
public Iterator<AnnotationSet> getAnnotationSets() {
return getAnnotationSets(false);
}
public Iterator<AnnotationSet> getAnnotationSets(boolean skipEmpty){
AnnotationsDirectory directory = getAnnotationsDirectory();
if(directory == null) {
Expand All @@ -245,13 +221,6 @@ public AnnotationsDirectory getOrCreateUniqueAnnotationsDirectory(){
}
return null;
}
public AnnotationsDirectory getUniqueAnnotationsDirectory(){
ClassId classId = getClassId();
if(classId != null){
return classId.getUniqueAnnotationsDirectory();
}
return null;
}
public ClassId getClassId() {
DefArray<Def<T>> array = getParentArray();
if(array != null){
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/com/reandroid/dex/data/MethodParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private boolean hasAnnotationSetBlocks() {
return false;
}

public Iterator<AnnotationItem> getAnnotationItemBlocks() {
private Iterator<AnnotationItem> getAnnotationItemBlocks() {
MethodDef methodDef = getMethodDef();
AnnotationsDirectory directory = methodDef.getAnnotationsDirectory();
if (directory != null) {
Expand All @@ -84,31 +84,18 @@ public Iterator<AnnotationItem> getAnnotationItemBlocks() {
return EmptyIterator.of();
}

public AnnotationItem addAnnotationItemBlock(TypeKey typeKey) {
return getOrCreateAnnotationSet().addNewItem(typeKey);
}

public AnnotationItem getOrCreateAnnotationItemBlock(TypeKey typeKey) {
return getOrCreateAnnotationSet().getOrCreate(typeKey);
}


public AnnotationSet getOrCreateAnnotationSet() {
MethodDef methodDef = getMethodDef();
AnnotationsDirectory directory = methodDef.getOrCreateUniqueAnnotationsDirectory();
return directory.getOrCreateParameterAnnotation(methodDef, getDefinitionIndex());
}

private void writeAnnotation(AnnotationSetKey key) {
MethodDef methodDef = getMethodDef();
int index = getDefinitionIndex();
if (key == null || key.isEmpty()) {
if (hasAnnotationSetBlocks()) {
AnnotationsDirectory directory = methodDef.getOrCreateUniqueAnnotationsDirectory();
directory.removeParameterAnnotation(methodDef, getDefinitionIndex());
directory.removeParameterAnnotation(methodDef, index);
}
} else {
AnnotationsDirectory directory = methodDef.getOrCreateUniqueAnnotationsDirectory();
directory.setParameterAnnotation(methodDef, getDefinitionIndex(), key);
directory.removeParameterAnnotation(methodDef, index);
directory.setParameterAnnotation(methodDef, index, key);
}
}

Expand Down
Loading

0 comments on commit cc90c22

Please sign in to comment.