-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEX] Abstracting smali and dex classes with similar structure
- Loading branch information
Showing
54 changed files
with
1,435 additions
and
604 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/reandroid/dex/dalvik/DalvikAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.dalvik; | ||
|
||
import com.reandroid.dex.program.ProgramElement; | ||
import com.reandroid.dex.key.AnnotationItemKey; | ||
import com.reandroid.dex.key.Key; | ||
import com.reandroid.dex.key.TypeKey; | ||
|
||
public class DalvikAnnotation { | ||
|
||
private final ProgramElement programElement; | ||
private final TypeKey annotationType; | ||
|
||
protected DalvikAnnotation(ProgramElement programElement, TypeKey annotationType) { | ||
this.programElement = programElement; | ||
this.annotationType = annotationType; | ||
} | ||
|
||
public AnnotationItemKey getKey() { | ||
return getProgramElement().getAnnotation(getAnnotationType()); | ||
} | ||
public void setKey(AnnotationItemKey key) { | ||
if (!getAnnotationType().equals(key.getType())) { | ||
throw new IllegalArgumentException("Different annotation type: " | ||
+ getAnnotationType() + ", " + key.getType()); | ||
} | ||
getProgramElement().addAnnotation(key); | ||
} | ||
public TypeKey getAnnotationType() { | ||
return annotationType; | ||
} | ||
public ProgramElement getProgramElement() { | ||
return programElement; | ||
} | ||
|
||
Key readValue(String name) { | ||
return getKey().getValue(name); | ||
} | ||
void writeValue(String name, Key value) { | ||
setKey(getKey().add(name, value)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getKey().toString(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/reandroid/dex/dalvik/DalvikEnclosing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.dalvik; | ||
|
||
import com.reandroid.dex.key.Key; | ||
import com.reandroid.dex.key.NullValueKey; | ||
import com.reandroid.dex.key.TypeKey; | ||
import com.reandroid.dex.program.ProgramElement; | ||
import com.reandroid.utils.ObjectsUtil; | ||
|
||
public abstract class DalvikEnclosing<T extends Key> extends DalvikAnnotation { | ||
|
||
public DalvikEnclosing(ProgramElement programElement, TypeKey annotationType) { | ||
super(programElement, annotationType); | ||
} | ||
|
||
public T getEnclosing() { | ||
Key value = readValue(Key.DALVIK_value); | ||
if (value == null || (value instanceof NullValueKey)) { | ||
return null; | ||
} | ||
return ObjectsUtil.cast(value); | ||
} | ||
public void setEnclosing(T enclosing) { | ||
Key value = enclosing == null ? NullValueKey.INSTANCE : enclosing; | ||
writeValue(Key.DALVIK_value, value); | ||
} | ||
public TypeKey getEnclosingClass() { | ||
Key key = getEnclosing(); | ||
if (key != null) { | ||
return key.getDeclaring(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(getEnclosing()); | ||
} | ||
|
||
public static DalvikEnclosing<?> of(ProgramElement programElement) { | ||
DalvikEnclosing<?> enclosing = DalvikEnclosingClass.of(programElement); | ||
if (enclosing == null) { | ||
enclosing = DalvikEnclosingMethod.of(programElement); | ||
} | ||
return enclosing; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/reandroid/dex/dalvik/DalvikEnclosingClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.dalvik; | ||
|
||
import com.reandroid.dex.common.AnnotationVisibility; | ||
import com.reandroid.dex.key.*; | ||
import com.reandroid.dex.program.ProgramElement; | ||
|
||
public class DalvikEnclosingClass extends DalvikEnclosing<TypeKey> { | ||
|
||
private DalvikEnclosingClass(ProgramElement programElement) { | ||
super(programElement, TypeKey.DALVIK_EnclosingClass); | ||
} | ||
|
||
public static DalvikEnclosingClass of(ProgramElement programElement) { | ||
if (programElement.hasAnnotation(TypeKey.DALVIK_EnclosingClass)) { | ||
return new DalvikEnclosingClass(programElement); | ||
} | ||
return null; | ||
} | ||
public static DalvikEnclosingClass getOrCreate(ProgramElement programElement) { | ||
if (!programElement.hasAnnotation(TypeKey.DALVIK_EnclosingClass)) { | ||
TypeKey typeKey = (TypeKey) programElement.getKey(); | ||
typeKey = typeKey.getEnclosingClass(); | ||
programElement.addAnnotation(AnnotationItemKey.create( | ||
AnnotationVisibility.SYSTEM, | ||
TypeKey.DALVIK_EnclosingClass, | ||
AnnotationElementKey.create(Key.DALVIK_value, typeKey) | ||
) | ||
); | ||
} | ||
return of(programElement); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/reandroid/dex/dalvik/DalvikEnclosingMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.dalvik; | ||
|
||
import com.reandroid.dex.common.AnnotationVisibility; | ||
import com.reandroid.dex.key.*; | ||
import com.reandroid.dex.program.ProgramElement; | ||
|
||
public class DalvikEnclosingMethod extends DalvikEnclosing<MethodKey> { | ||
|
||
private DalvikEnclosingMethod(ProgramElement programElement) { | ||
super(programElement, TypeKey.DALVIK_EnclosingMethod); | ||
} | ||
|
||
public static DalvikEnclosingMethod of(ProgramElement programElement) { | ||
if (programElement.hasAnnotation(TypeKey.DALVIK_EnclosingMethod)) { | ||
return new DalvikEnclosingMethod(programElement); | ||
} | ||
return null; | ||
} | ||
public static DalvikEnclosingMethod getOrCreate(ProgramElement programElement) { | ||
if (!programElement.hasAnnotation(TypeKey.DALVIK_EnclosingMethod)) { | ||
programElement.addAnnotation(AnnotationItemKey.create( | ||
AnnotationVisibility.SYSTEM, | ||
TypeKey.DALVIK_EnclosingMethod, | ||
AnnotationElementKey.create(Key.DALVIK_value, NullValueKey.INSTANCE) | ||
) | ||
); | ||
} | ||
return of(programElement); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/reandroid/dex/dalvik/DalvikInnerClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.dalvik; | ||
|
||
import com.reandroid.dex.common.AccessFlag; | ||
import com.reandroid.dex.common.AnnotationVisibility; | ||
import com.reandroid.dex.key.*; | ||
import com.reandroid.dex.program.ProgramElement; | ||
|
||
import java.util.Iterator; | ||
|
||
public class DalvikInnerClass extends DalvikAnnotation { | ||
|
||
private DalvikInnerClass(ProgramElement programElement) { | ||
super(programElement, TypeKey.DALVIK_InnerClass); | ||
} | ||
|
||
public Iterator<AccessFlag> getAccessFlags() { | ||
return AccessFlag.valuesOfClass(getAccessFlagsValue()); | ||
} | ||
public int getAccessFlagsValue() { | ||
PrimitiveKey key = (PrimitiveKey) readValue(Key.DALVIK_accessFlags); | ||
return (int) key.getValueAsLong(); | ||
} | ||
public void setAccessFlags(int flags) { | ||
writeValue(Key.DALVIK_accessFlags, PrimitiveKey.of(flags)); | ||
} | ||
public String getName() { | ||
Key key = getKey().getValue(Key.DALVIK_name); | ||
if (key instanceof StringKey) { | ||
return ((StringKey) key).getString(); | ||
} | ||
return null; | ||
} | ||
public void setName(String name) { | ||
Key key = name == null? NullValueKey.INSTANCE : StringKey.create(name); | ||
writeValue(Key.DALVIK_name, key); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return AccessFlag.toString(getAccessFlags()) + getName(); | ||
} | ||
|
||
public static DalvikInnerClass of(ProgramElement programElement) { | ||
if (programElement.hasAnnotation(TypeKey.DALVIK_InnerClass)) { | ||
return new DalvikInnerClass(programElement); | ||
} | ||
return null; | ||
} | ||
public static DalvikInnerClass getOrCreate(ProgramElement programElement) { | ||
if (!programElement.hasAnnotation(TypeKey.DALVIK_InnerClass)) { | ||
programElement.addAnnotation(AnnotationItemKey.create( | ||
AnnotationVisibility.SYSTEM, | ||
TypeKey.DALVIK_InnerClass, | ||
AnnotationElementKey.create(Key.DALVIK_accessFlags, PrimitiveKey.of(0)), | ||
AnnotationElementKey.create(Key.DALVIK_name, NullValueKey.INSTANCE) | ||
) | ||
); | ||
} | ||
return of(programElement); | ||
} | ||
} |
Oops, something went wrong.