mirror of
https://github.com/revanced/smali.git
synced 2025-05-04 16:44:25 +02:00
refactor ClassDefinition to use template attributes instead of object parameters
git-svn-id: https://smali.googlecode.com/svn/trunk@371 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
parent
fa7e91bff2
commit
7885a819a0
@ -52,6 +52,23 @@ public class ClassDefinition {
|
|||||||
buildAnnotationMaps();
|
buildAnnotationMaps();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringTemplate makeTemplate() {
|
||||||
|
StringTemplate template = stg.getInstanceOf("smaliFile");
|
||||||
|
|
||||||
|
template.setAttribute("AccessFlags", getAccessFlags());
|
||||||
|
template.setAttribute("ClassType", classDefItem.getClassType().getTypeDescriptor());
|
||||||
|
template.setAttribute("SuperType", getSuperType());
|
||||||
|
template.setAttribute("SourceFile", getSourceFile());
|
||||||
|
template.setAttribute("Interfaces", getInterfaces());
|
||||||
|
template.setAttribute("Annotations", getAnnotations());
|
||||||
|
template.setAttribute("StaticFields", getStaticFields());
|
||||||
|
template.setAttribute("InstanceFields", getInstanceFields());
|
||||||
|
template.setAttribute("DirectMethods", getDirectMethods());
|
||||||
|
template.setAttribute("VirtualMethods", getVirtualMethods());
|
||||||
|
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
private void buildAnnotationMaps() {
|
private void buildAnnotationMaps() {
|
||||||
AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations();
|
AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations();
|
||||||
if (annotationDirectory == null) {
|
if (annotationDirectory == null) {
|
||||||
@ -78,7 +95,7 @@ public class ClassDefinition {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getAccessFlags() {
|
private List<String> getAccessFlags() {
|
||||||
List<String> accessFlags = new ArrayList<String>();
|
List<String> accessFlags = new ArrayList<String>();
|
||||||
|
|
||||||
for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) {
|
for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) {
|
||||||
@ -88,11 +105,8 @@ public class ClassDefinition {
|
|||||||
return accessFlags;
|
return accessFlags;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClassType() {
|
|
||||||
return classDefItem.getClassType().getTypeDescriptor();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSuperType() {
|
private String getSuperType() {
|
||||||
TypeIdItem superClass = classDefItem.getSuperclass();
|
TypeIdItem superClass = classDefItem.getSuperclass();
|
||||||
if (superClass != null) {
|
if (superClass != null) {
|
||||||
return superClass.getTypeDescriptor();
|
return superClass.getTypeDescriptor();
|
||||||
@ -100,7 +114,7 @@ public class ClassDefinition {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSourceFile() {
|
private String getSourceFile() {
|
||||||
StringIdItem sourceFile = classDefItem.getSourceFile();
|
StringIdItem sourceFile = classDefItem.getSourceFile();
|
||||||
|
|
||||||
if (sourceFile == null) {
|
if (sourceFile == null) {
|
||||||
@ -109,7 +123,7 @@ public class ClassDefinition {
|
|||||||
return classDefItem.getSourceFile().getStringValue();
|
return classDefItem.getSourceFile().getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getInterfaces() {
|
private List<String> getInterfaces() {
|
||||||
List<String> interfaces = new ArrayList<String>();
|
List<String> interfaces = new ArrayList<String>();
|
||||||
|
|
||||||
TypeListItem interfaceList = classDefItem.getInterfaces();
|
TypeListItem interfaceList = classDefItem.getInterfaces();
|
||||||
@ -123,17 +137,26 @@ public class ClassDefinition {
|
|||||||
return interfaces;
|
return interfaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHasStaticFields() {
|
private List<StringTemplate> getAnnotations() {
|
||||||
if (classDataItem != null) {
|
AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations();
|
||||||
ClassDataItem.EncodedField fields[] = classDataItem.getStaticFields();
|
if (annotationDirectory == null) {
|
||||||
if (fields != null && fields.length > 0) {
|
return null;
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations();
|
||||||
|
if (annotationSet == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<StringTemplate> annotations = new ArrayList<StringTemplate>();
|
||||||
|
|
||||||
|
for (AnnotationItem annotationItem: annotationSet.getAnnotations()) {
|
||||||
|
annotations.add(AnnotationAdaptor.makeTemplate(stg, annotationItem));
|
||||||
|
}
|
||||||
|
return annotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<StringTemplate> getStaticFields() {
|
private List<StringTemplate> getStaticFields() {
|
||||||
List<StringTemplate> staticFields = new ArrayList<StringTemplate>();
|
List<StringTemplate> staticFields = new ArrayList<StringTemplate>();
|
||||||
|
|
||||||
if (classDataItem != null) {
|
if (classDataItem != null) {
|
||||||
@ -161,17 +184,7 @@ public class ClassDefinition {
|
|||||||
return staticFields;
|
return staticFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHasInstanceFields() {
|
private List<StringTemplate> getInstanceFields() {
|
||||||
if (classDataItem != null) {
|
|
||||||
ClassDataItem.EncodedField fields[] = classDataItem.getInstanceFields();
|
|
||||||
if (fields != null && fields.length > 0) {
|
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<StringTemplate> getInstanceFields() {
|
|
||||||
List<StringTemplate> instanceFields = new ArrayList<StringTemplate>();
|
List<StringTemplate> instanceFields = new ArrayList<StringTemplate>();
|
||||||
|
|
||||||
if (classDataItem != null) {
|
if (classDataItem != null) {
|
||||||
@ -184,17 +197,7 @@ public class ClassDefinition {
|
|||||||
return instanceFields;
|
return instanceFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHasDirectMethods() {
|
private List<StringTemplate> getDirectMethods() {
|
||||||
if (classDataItem != null) {
|
|
||||||
ClassDataItem.EncodedMethod[] methods = classDataItem.getDirectMethods();
|
|
||||||
if (methods != null && methods.length > 0) {
|
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<StringTemplate> getDirectMethods() {
|
|
||||||
List<StringTemplate> directMethods = new ArrayList<StringTemplate>();
|
List<StringTemplate> directMethods = new ArrayList<StringTemplate>();
|
||||||
|
|
||||||
if (classDataItem != null) {
|
if (classDataItem != null) {
|
||||||
@ -208,17 +211,7 @@ public class ClassDefinition {
|
|||||||
return directMethods;
|
return directMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHasVirtualMethods() {
|
private List<StringTemplate> getVirtualMethods() {
|
||||||
if (classDataItem != null) {
|
|
||||||
ClassDataItem.EncodedMethod[] methods = classDataItem.getVirtualMethods();
|
|
||||||
if (methods != null && methods.length > 0) {
|
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<StringTemplate> getVirtualMethods() {
|
|
||||||
List<StringTemplate> virtualMethods = new ArrayList<StringTemplate>();
|
List<StringTemplate> virtualMethods = new ArrayList<StringTemplate>();
|
||||||
|
|
||||||
if (classDataItem != null) {
|
if (classDataItem != null) {
|
||||||
@ -231,23 +224,4 @@ public class ClassDefinition {
|
|||||||
|
|
||||||
return virtualMethods;
|
return virtualMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<StringTemplate> getAnnotations() {
|
|
||||||
AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations();
|
|
||||||
if (annotationDirectory == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations();
|
|
||||||
if (annotationSet == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<StringTemplate> annotations = new ArrayList<StringTemplate>();
|
|
||||||
|
|
||||||
for (AnnotationItem annotationItem: annotationSet.getAnnotations()) {
|
|
||||||
annotations.add(AnnotationAdaptor.makeTemplate(stg, annotationItem));
|
|
||||||
}
|
|
||||||
return annotations;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -96,8 +96,9 @@ public class baksmali {
|
|||||||
File smaliFile = new File(smaliPath.toString());
|
File smaliFile = new File(smaliPath.toString());
|
||||||
|
|
||||||
//create and initialize the top level string template
|
//create and initialize the top level string template
|
||||||
StringTemplate smaliFileST = templates.getInstanceOf("smaliFile");
|
ClassDefinition classDefinition = new ClassDefinition(templates, classDefItem);
|
||||||
smaliFileST.setAttribute("classDef", new ClassDefinition(templates, classDefItem));
|
|
||||||
|
StringTemplate smaliFileST = classDefinition.makeTemplate();
|
||||||
|
|
||||||
//generate the disassembly
|
//generate the disassembly
|
||||||
String output = smaliFileST.toString();
|
String output = smaliFileST.toString();
|
||||||
@ -126,7 +127,6 @@ public class baksmali {
|
|||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
|
System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -136,7 +136,7 @@ public class baksmali {
|
|||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
System.err.println("\n\nError occured while closing file " + smaliFile.toString());
|
System.err.println("\n\nError occured while closing file " + smaliFile.toString());
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,56 +1,57 @@
|
|||||||
group baksmali;
|
group baksmali;
|
||||||
|
|
||||||
smaliFile(classDef) ::=
|
smaliFile(AccessFlags, ClassType, SuperType, SourceFile, Interfaces, Annotations, StaticFields,
|
||||||
|
InstanceFields, DirectMethods, VirtualMethods) ::=
|
||||||
<<
|
<<
|
||||||
.class <classDef.AccessFlags: {<it> }><classDef.ClassType>
|
.class <AccessFlags: {<it> }><ClassType>
|
||||||
<if(classDef.SuperType)>
|
<if(SuperType)>
|
||||||
.super <classDef.SuperType>
|
.super <SuperType>
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.SourceFile)>
|
<if(SourceFile)>
|
||||||
|
|
||||||
.source "<classDef.SourceFile>"
|
.source "<SourceFile>"
|
||||||
|
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.Interfaces)>
|
<if(Interfaces)>
|
||||||
|
|
||||||
# interfaces
|
# interfaces
|
||||||
<classDef.Interfaces: implement(it); separator="\n">
|
<Interfaces: implement(it); separator="\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.Annotations)>
|
<if(Annotations)>
|
||||||
|
|
||||||
|
|
||||||
# annotations
|
# annotations
|
||||||
<classDef.Annotations; separator="\n\n">
|
<Annotations; separator="\n\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.HasStaticFields)>
|
<if(StaticFields)>
|
||||||
|
|
||||||
|
|
||||||
# static fields
|
# static fields
|
||||||
<classDef.StaticFields; separator="\n">
|
<StaticFields; separator="\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.HasInstanceFields)>
|
<if(InstanceFields)>
|
||||||
|
|
||||||
|
|
||||||
# instance fields
|
# instance fields
|
||||||
<classDef.InstanceFields; separator="\n">
|
<InstanceFields; separator="\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.HasDirectMethods)>
|
<if(DirectMethods)>
|
||||||
|
|
||||||
|
|
||||||
# direct methods
|
# direct methods
|
||||||
<classDef.DirectMethods; separator="\n\n">
|
<DirectMethods; separator="\n\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
<if(classDef.HasVirtualMethods)>
|
<if(VirtualMethods)>
|
||||||
|
|
||||||
|
|
||||||
# virtual methods
|
# virtual methods
|
||||||
<classDef.VirtualMethods; separator="\n\n">
|
<VirtualMethods; separator="\n\n">
|
||||||
|
|
||||||
<endif>
|
<endif>
|
||||||
>>
|
>>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user