Skip to content

Commit def48e5

Browse files
committed
[DEX] parsing dalvik.annotation.AnnotationDefault
1 parent 39b363a commit def48e5

4 files changed

Lines changed: 160 additions & 1 deletion

File tree

src/main/java/com/reandroid/dex/dalvik/DalvikAnnotation.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,56 @@ public String toString() {
8585
}
8686
return key.toString();
8787
}
88+
89+
public static DalvikAnnotation of(TypeKey dalvikAnnotationType, AnnotatedProgram annotatedProgram) {
90+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_EnclosingClass)) {
91+
return DalvikEnclosingClass.of(annotatedProgram);
92+
}
93+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_EnclosingMethod)) {
94+
return DalvikEnclosingMethod.of(annotatedProgram);
95+
}
96+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_InnerClass)) {
97+
return DalvikInnerClass.of(annotatedProgram);
98+
}
99+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_MemberClass)) {
100+
return DalvikMemberClass.of(annotatedProgram);
101+
}
102+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_MethodParameters)) {
103+
return DalvikMethodParameters.of(annotatedProgram);
104+
}
105+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_Signature)) {
106+
return DalvikSignature.of(annotatedProgram);
107+
}
108+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_Throws)) {
109+
return DalvikThrows.of(annotatedProgram);
110+
}
111+
return null;
112+
}
113+
public static DalvikAnnotation getOrCreate(TypeKey dalvikAnnotationType, AnnotatedProgram annotatedProgram) {
114+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_AnnotationDefault)) {
115+
return DalvikAnnotationDefault.getOrCreate(annotatedProgram);
116+
}
117+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_EnclosingClass)) {
118+
return DalvikEnclosingClass.getOrCreate(annotatedProgram);
119+
}
120+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_EnclosingMethod)) {
121+
return DalvikEnclosingMethod.getOrCreate(annotatedProgram);
122+
}
123+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_InnerClass)) {
124+
return DalvikInnerClass.getOrCreate(annotatedProgram);
125+
}
126+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_MemberClass)) {
127+
return DalvikMemberClass.getOrCreate(annotatedProgram);
128+
}
129+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_MethodParameters)) {
130+
return DalvikMethodParameters.getOrCreate(annotatedProgram);
131+
}
132+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_Signature)) {
133+
return DalvikSignature.getOrCreate(annotatedProgram);
134+
}
135+
if (dalvikAnnotationType.equals(TypeKey.DALVIK_Throws)) {
136+
return DalvikThrows.getOrCreate(annotatedProgram);
137+
}
138+
return null;
139+
}
88140
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2022 github.com/REAndroid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.reandroid.dex.dalvik;
17+
18+
import com.reandroid.dex.common.AnnotationVisibility;
19+
import com.reandroid.dex.key.AnnotationElementKey;
20+
import com.reandroid.dex.key.AnnotationItemKey;
21+
import com.reandroid.dex.key.Key;
22+
import com.reandroid.dex.key.TypeKey;
23+
import com.reandroid.dex.program.AnnotatedProgram;
24+
import com.reandroid.dex.program.ClassProgram;
25+
26+
import java.util.Iterator;
27+
28+
public class DalvikAnnotationDefault extends DalvikAnnotation {
29+
30+
private DalvikAnnotationDefault(ClassProgram classProgram) {
31+
super(classProgram, TypeKey.DALVIK_AnnotationDefault);
32+
}
33+
34+
public Iterator<AnnotationElementKey> values() {
35+
return getSubAnnotation().iterator();
36+
}
37+
public Key getValue(String name) {
38+
return getSubAnnotation().getValue(name);
39+
}
40+
public Key getValue(int index) {
41+
AnnotationElementKey elementKey = getSubAnnotation().get(index);
42+
if (elementKey != null) {
43+
return elementKey.getValue();
44+
}
45+
return null;
46+
}
47+
public void setValue(String name, Key value) {
48+
writeValue(Key.DALVIK_value, getSubAnnotation().setValue(name, value));
49+
}
50+
public void remove(String name) {
51+
writeValue(Key.DALVIK_value, getSubAnnotation().remove(name));
52+
}
53+
public int size() {
54+
return getSubAnnotation().size();
55+
}
56+
public AnnotationItemKey getSubAnnotation() {
57+
Key key = readValue(Key.DALVIK_value);
58+
if (!(key instanceof AnnotationItemKey)) {
59+
key = AnnotationItemKey.create(null, getAnnotatedProgram().getKey());
60+
writeValue(Key.DALVIK_value, key);
61+
}
62+
return (AnnotationItemKey) key;
63+
}
64+
@Override
65+
public ClassProgram getAnnotatedProgram() {
66+
return (ClassProgram) super.getAnnotatedProgram();
67+
}
68+
69+
@Override
70+
public String toString() {
71+
Key key = readValue(Key.DALVIK_value);
72+
if (key == null) {
73+
return "NULL subannotation";
74+
}
75+
return key.toString();
76+
}
77+
78+
public static DalvikAnnotationDefault of(AnnotatedProgram annotatedProgram) {
79+
if (annotatedProgram instanceof ClassProgram) {
80+
if (annotatedProgram.hasAnnotation(TypeKey.DALVIK_AnnotationDefault)) {
81+
return new DalvikAnnotationDefault((ClassProgram) annotatedProgram);
82+
}
83+
}
84+
return null;
85+
}
86+
public static DalvikAnnotationDefault getOrCreate(AnnotatedProgram annotatedProgram) {
87+
if (!annotatedProgram.hasAnnotation(TypeKey.DALVIK_AnnotationDefault)) {
88+
if (!(annotatedProgram instanceof ClassProgram)) {
89+
return null;
90+
}
91+
ClassProgram classProgram = (ClassProgram) annotatedProgram;
92+
annotatedProgram.addAnnotation(AnnotationItemKey.create(
93+
AnnotationVisibility.SYSTEM,
94+
TypeKey.DALVIK_AnnotationDefault,
95+
AnnotationElementKey.create(
96+
Key.DALVIK_value, AnnotationItemKey.create(null, classProgram.getKey()))
97+
));
98+
}
99+
return of(annotatedProgram);
100+
}
101+
102+
}

src/main/java/com/reandroid/dex/key/TypeKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ public boolean isWide() {
836836
public static final TypeKey STRING = new TypeKey("Ljava/lang/String;");
837837
public static final TypeKey EXCEPTION = new TypeKey("Ljava/lang/Exception;");
838838

839+
public static final TypeKey DALVIK_AnnotationDefault = new TypeKey("Ldalvik/annotation/AnnotationDefault;");
839840
public static final TypeKey DALVIK_EnclosingClass = new TypeKey("Ldalvik/annotation/EnclosingClass;");
840841
public static final TypeKey DALVIK_EnclosingMethod = new TypeKey("Ldalvik/annotation/EnclosingMethod;");
841842
public static final TypeKey DALVIK_InnerClass = new TypeKey("Ldalvik/annotation/InnerClass;");

src/main/java/com/reandroid/dex/smali/formatters/ClassComment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ public void writeComment(SmaliWriter writer, TypeKey typeKey) throws IOException
7373
if (dexClass != null && dexClass.isInterface()) {
7474
SmaliComment.writeDeclarationComment(
7575
writer,
76-
"implemented-by:",
76+
"extended-by:",
7777
dexClass.getExtending());
78+
SmaliComment.writeDeclarationComment(
79+
writer,
80+
"imflemented-by:",
81+
dexClass.getImplementations());
7882
}
7983
}
8084
@Override

0 commit comments

Comments
 (0)