From 7885a819a0dc52568fedd60f7d8d0f0a9ca352e9 Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Fri, 7 Aug 2009 06:48:37 +0000 Subject: [PATCH] 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 --- .../jf/baksmali/Adaptors/ClassDefinition.java | 108 +++++++----------- .../main/java/org/jf/baksmali/baksmali.java | 8 +- .../templates/templates/baksmali.stg | 37 +++--- 3 files changed, 64 insertions(+), 89 deletions(-) diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java index e7c241cc..eb59178c 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java @@ -52,6 +52,23 @@ public class ClassDefinition { 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() { AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); if (annotationDirectory == null) { @@ -78,7 +95,7 @@ public class ClassDefinition { }); } - public List getAccessFlags() { + private List getAccessFlags() { List accessFlags = new ArrayList(); for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) { @@ -88,11 +105,8 @@ public class ClassDefinition { return accessFlags; } - public String getClassType() { - return classDefItem.getClassType().getTypeDescriptor(); - } - public String getSuperType() { + private String getSuperType() { TypeIdItem superClass = classDefItem.getSuperclass(); if (superClass != null) { return superClass.getTypeDescriptor(); @@ -100,7 +114,7 @@ public class ClassDefinition { return null; } - public String getSourceFile() { + private String getSourceFile() { StringIdItem sourceFile = classDefItem.getSourceFile(); if (sourceFile == null) { @@ -109,7 +123,7 @@ public class ClassDefinition { return classDefItem.getSourceFile().getStringValue(); } - public List getInterfaces() { + private List getInterfaces() { List interfaces = new ArrayList(); TypeListItem interfaceList = classDefItem.getInterfaces(); @@ -123,17 +137,26 @@ public class ClassDefinition { return interfaces; } - public String getHasStaticFields() { - if (classDataItem != null) { - ClassDataItem.EncodedField fields[] = classDataItem.getStaticFields(); - if (fields != null && fields.length > 0) { - return "true"; - } + private List getAnnotations() { + AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); + if (annotationDirectory == null) { + return null; } - return null; + + AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations(); + if (annotationSet == null) { + return null; + } + + List annotations = new ArrayList(); + + for (AnnotationItem annotationItem: annotationSet.getAnnotations()) { + annotations.add(AnnotationAdaptor.makeTemplate(stg, annotationItem)); + } + return annotations; } - public List getStaticFields() { + private List getStaticFields() { List staticFields = new ArrayList(); if (classDataItem != null) { @@ -161,17 +184,7 @@ public class ClassDefinition { return staticFields; } - public String getHasInstanceFields() { - if (classDataItem != null) { - ClassDataItem.EncodedField fields[] = classDataItem.getInstanceFields(); - if (fields != null && fields.length > 0) { - return "true"; - } - } - return null; - } - - public List getInstanceFields() { + private List getInstanceFields() { List instanceFields = new ArrayList(); if (classDataItem != null) { @@ -184,17 +197,7 @@ public class ClassDefinition { return instanceFields; } - public String getHasDirectMethods() { - if (classDataItem != null) { - ClassDataItem.EncodedMethod[] methods = classDataItem.getDirectMethods(); - if (methods != null && methods.length > 0) { - return "true"; - } - } - return null; - } - - public List getDirectMethods() { + private List getDirectMethods() { List directMethods = new ArrayList(); if (classDataItem != null) { @@ -208,17 +211,7 @@ public class ClassDefinition { return directMethods; } - public String getHasVirtualMethods() { - if (classDataItem != null) { - ClassDataItem.EncodedMethod[] methods = classDataItem.getVirtualMethods(); - if (methods != null && methods.length > 0) { - return "true"; - } - } - return null; - } - - public List getVirtualMethods() { + private List getVirtualMethods() { List virtualMethods = new ArrayList(); if (classDataItem != null) { @@ -231,23 +224,4 @@ public class ClassDefinition { return virtualMethods; } - - public List getAnnotations() { - AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); - if (annotationDirectory == null) { - return null; - } - - AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations(); - if (annotationSet == null) { - return null; - } - - List annotations = new ArrayList(); - - for (AnnotationItem annotationItem: annotationSet.getAnnotations()) { - annotations.add(AnnotationAdaptor.makeTemplate(stg, annotationItem)); - } - return annotations; - } } diff --git a/baksmali/src/main/java/org/jf/baksmali/baksmali.java b/baksmali/src/main/java/org/jf/baksmali/baksmali.java index e3c2df31..6f66ee13 100644 --- a/baksmali/src/main/java/org/jf/baksmali/baksmali.java +++ b/baksmali/src/main/java/org/jf/baksmali/baksmali.java @@ -96,8 +96,9 @@ public class baksmali { File smaliFile = new File(smaliPath.toString()); //create and initialize the top level string template - StringTemplate smaliFileST = templates.getInstanceOf("smaliFile"); - smaliFileST.setAttribute("classDef", new ClassDefinition(templates, classDefItem)); + ClassDefinition classDefinition = new ClassDefinition(templates, classDefItem); + + StringTemplate smaliFileST = classDefinition.makeTemplate(); //generate the disassembly String output = smaliFileST.toString(); @@ -126,7 +127,6 @@ public class baksmali { } catch (Throwable ex) { System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); ex.printStackTrace(); - continue; } finally { @@ -136,7 +136,7 @@ public class baksmali { } catch (Throwable ex) { System.err.println("\n\nError occured while closing file " + smaliFile.toString()); ex.printStackTrace(); - }; + } } } } diff --git a/baksmali/src/main/resources/templates/templates/baksmali.stg b/baksmali/src/main/resources/templates/templates/baksmali.stg index 8d98d72c..b26a4aa8 100644 --- a/baksmali/src/main/resources/templates/templates/baksmali.stg +++ b/baksmali/src/main/resources/templates/templates/baksmali.stg @@ -1,56 +1,57 @@ group baksmali; -smaliFile(classDef) ::= +smaliFile(AccessFlags, ClassType, SuperType, SourceFile, Interfaces, Annotations, StaticFields, + InstanceFields, DirectMethods, VirtualMethods) ::= << -.class }> - -.super +.class }> + +.super - + -.source "" +.source "" - + # interfaces - + - + # annotations - + - + # static fields - + - + # instance fields - + - + # direct methods - + - + # virtual methods - + >>