ClassDefinition.java revision 05c9404ecde123f9c965d4d99967850b74f943f2
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2009 Ben Gruver 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.baksmali.Adaptors; 30 31import org.jf.dexlib.EncodedValue.EncodedValue; 32import org.jf.dexlib.*; 33import org.jf.dexlib.Util.AccessFlags; 34import org.jf.dexlib.Util.SparseArray; 35import org.antlr.stringtemplate.StringTemplate; 36import org.antlr.stringtemplate.StringTemplateGroup; 37 38import java.util.*; 39 40public class ClassDefinition { 41 private StringTemplateGroup stg; 42 private ClassDefItem classDefItem; 43 private ClassDataItem classDataItem; 44 45 private SparseArray<AnnotationSetItem> methodAnnotationsMap; 46 private SparseArray<AnnotationSetItem> fieldAnnotationsMap; 47 private SparseArray<AnnotationSetRefList> parameterAnnotationsMap; 48 49 public ClassDefinition(StringTemplateGroup stg, ClassDefItem classDefItem) { 50 this.stg = stg; 51 this.classDefItem = classDefItem; 52 this.classDataItem = classDefItem.getClassData(); 53 buildAnnotationMaps(); 54 } 55 56 public StringTemplate makeTemplate() { 57 StringTemplate template = stg.getInstanceOf("smaliFile"); 58 59 template.setAttribute("AccessFlags", getAccessFlags()); 60 template.setAttribute("ClassType", classDefItem.getClassType().getTypeDescriptor()); 61 template.setAttribute("SuperType", getSuperType()); 62 template.setAttribute("SourceFile", getSourceFile()); 63 template.setAttribute("Interfaces", getInterfaces()); 64 template.setAttribute("Annotations", getAnnotations()); 65 template.setAttribute("StaticFields", getStaticFields()); 66 template.setAttribute("InstanceFields", getInstanceFields()); 67 template.setAttribute("DirectMethods", getDirectMethods()); 68 template.setAttribute("VirtualMethods", getVirtualMethods()); 69 70 return template; 71 } 72 73 private void buildAnnotationMaps() { 74 AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); 75 if (annotationDirectory == null) { 76 methodAnnotationsMap = new SparseArray<AnnotationSetItem>(0); 77 fieldAnnotationsMap = new SparseArray<AnnotationSetItem>(0); 78 parameterAnnotationsMap = new SparseArray<AnnotationSetRefList>(0); 79 return; 80 } 81 82 methodAnnotationsMap = new SparseArray<AnnotationSetItem>(annotationDirectory.getMethodAnnotationCount()); 83 annotationDirectory.iterateMethodAnnotations(new AnnotationDirectoryItem.MethodAnnotationIteratorDelegate() { 84 public void processMethodAnnotations(MethodIdItem method, AnnotationSetItem methodAnnotations) { 85 methodAnnotationsMap.put(method.getIndex(), methodAnnotations); 86 } 87 }); 88 89 fieldAnnotationsMap = new SparseArray<AnnotationSetItem>(annotationDirectory.getFieldAnnotationCount()); 90 annotationDirectory.iterateFieldAnnotations(new AnnotationDirectoryItem.FieldAnnotationIteratorDelegate() { 91 public void processFieldAnnotations(FieldIdItem field, AnnotationSetItem fieldAnnotations) { 92 fieldAnnotationsMap.put(field.getIndex(), fieldAnnotations); 93 } 94 }); 95 96 parameterAnnotationsMap = new SparseArray<AnnotationSetRefList>( 97 annotationDirectory.getParameterAnnotationCount()); 98 annotationDirectory.iterateParameterAnnotations( 99 new AnnotationDirectoryItem.ParameterAnnotationIteratorDelegate() { 100 public void processParameterAnnotations(MethodIdItem method, AnnotationSetRefList parameterAnnotations) { 101 parameterAnnotationsMap.put(method.getIndex(), parameterAnnotations); 102 } 103 }); 104 } 105 106 private List<String> getAccessFlags() { 107 List<String> accessFlags = new ArrayList<String>(); 108 109 for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) { 110 accessFlags.add(accessFlag.toString()); 111 } 112 113 return accessFlags; 114 } 115 116 117 private String getSuperType() { 118 TypeIdItem superClass = classDefItem.getSuperclass(); 119 if (superClass != null) { 120 return superClass.getTypeDescriptor(); 121 } 122 return null; 123 } 124 125 private String getSourceFile() { 126 StringIdItem sourceFile = classDefItem.getSourceFile(); 127 128 if (sourceFile == null) { 129 return null; 130 } 131 return classDefItem.getSourceFile().getStringValue(); 132 } 133 134 private List<String> getInterfaces() { 135 List<String> interfaces = new ArrayList<String>(); 136 137 TypeListItem interfaceList = classDefItem.getInterfaces(); 138 139 if (interfaceList != null) { 140 for (TypeIdItem typeIdItem: interfaceList.getTypes()) { 141 interfaces.add(typeIdItem.getTypeDescriptor()); 142 } 143 } 144 145 return interfaces; 146 } 147 148 private List<StringTemplate> getAnnotations() { 149 AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); 150 if (annotationDirectory == null) { 151 return null; 152 } 153 154 AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations(); 155 if (annotationSet == null) { 156 return null; 157 } 158 159 List<StringTemplate> annotations = new ArrayList<StringTemplate>(); 160 161 for (AnnotationItem annotationItem: annotationSet.getAnnotations()) { 162 annotations.add(AnnotationAdaptor.makeTemplate(stg, annotationItem)); 163 } 164 return annotations; 165 } 166 167 private List<StringTemplate> getStaticFields() { 168 List<StringTemplate> staticFields = new ArrayList<StringTemplate>(); 169 170 if (classDataItem != null) { 171 EncodedArrayItem encodedStaticInitializers = classDefItem.getStaticFieldInitializers(); 172 173 EncodedValue[] staticInitializers; 174 if (encodedStaticInitializers != null) { 175 staticInitializers = encodedStaticInitializers.getEncodedArray().values; 176 } else { 177 staticInitializers = new EncodedValue[0]; 178 } 179 180 int i=0; 181 for (ClassDataItem.EncodedField field: classDataItem.getStaticFields()) { 182 EncodedValue encodedValue = null; 183 if (i < staticInitializers.length) { 184 encodedValue = staticInitializers[i]; 185 } 186 AnnotationSetItem annotationSet = fieldAnnotationsMap.get(field.field.getIndex()); 187 188 staticFields.add(FieldDefinition.createTemplate(stg, field, encodedValue, annotationSet)); 189 i++; 190 } 191 } 192 return staticFields; 193 } 194 195 private List<StringTemplate> getInstanceFields() { 196 List<StringTemplate> instanceFields = new ArrayList<StringTemplate>(); 197 198 if (classDataItem != null) { 199 for (ClassDataItem.EncodedField field: classDataItem.getInstanceFields()) { 200 AnnotationSetItem annotationSet = fieldAnnotationsMap.get(field.field.getIndex()); 201 instanceFields.add(FieldDefinition.createTemplate(stg, field, annotationSet)); 202 } 203 } 204 205 return instanceFields; 206 } 207 208 private List<StringTemplate> getDirectMethods() { 209 List<StringTemplate> directMethods = new ArrayList<StringTemplate>(); 210 211 if (classDataItem != null) { 212 for (ClassDataItem.EncodedMethod method: classDataItem.getDirectMethods()) { 213 AnnotationSetItem annotationSet = methodAnnotationsMap.get(method.method.getIndex()); 214 AnnotationSetRefList parameterAnnotationList = parameterAnnotationsMap.get(method.method.getIndex()); 215 directMethods.add(MethodDefinition.makeTemplate(stg, method, annotationSet, parameterAnnotationList)); 216 } 217 } 218 219 return directMethods; 220 } 221 222 private List<StringTemplate> getVirtualMethods() { 223 List<StringTemplate> virtualMethods = new ArrayList<StringTemplate>(); 224 225 if (classDataItem != null) { 226 for (ClassDataItem.EncodedMethod method: classDataItem.getVirtualMethods()) { 227 AnnotationSetItem annotationSet = methodAnnotationsMap.get(method.method.getIndex()); 228 AnnotationSetRefList parameterAnnotationList = parameterAnnotationsMap.get(method.method.getIndex()); 229 virtualMethods.add(MethodDefinition.makeTemplate(stg, method, annotationSet, parameterAnnotationList)); 230 } 231 } 232 233 return virtualMethods; 234 } 235} 236