ClassDefinition.java revision 630a384c6bb90358cc64c7fcbec6884fdac8f50d
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; 34 35import java.util.*; 36 37public class ClassDefinition { 38 private ClassDefItem classDefItem; 39 private ClassDataItem classDataItem; 40 41 private HashMap<Integer, AnnotationSetItem> methodAnnotationsMap = new HashMap<Integer, AnnotationSetItem>(); 42 private HashMap<Integer, AnnotationSetItem> fieldAnnotationsMap = new HashMap<Integer, AnnotationSetItem>(); 43 private HashMap<Integer, AnnotationSetRefList> parameterAnnotationsMap = new HashMap<Integer, AnnotationSetRefList>(); 44 45 public ClassDefinition(ClassDefItem classDefItem) { 46 this.classDefItem = classDefItem; 47 this.classDataItem = classDefItem.getClassData(); 48 buildAnnotationMaps(); 49 } 50 51 private void buildAnnotationMaps() { 52 AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); 53 if (annotationDirectory == null) { 54 return; 55 } 56 57 annotationDirectory.iterateMethodAnnotations(new AnnotationDirectoryItem.MethodAnnotationIteratorDelegate() { 58 public void processMethodAnnotations(MethodIdItem method, AnnotationSetItem methodAnnotations) { 59 methodAnnotationsMap.put(method.getIndex(), methodAnnotations); 60 } 61 }); 62 63 annotationDirectory.iterateFieldAnnotations(new AnnotationDirectoryItem.FieldAnnotationIteratorDelegate() { 64 public void processFieldAnnotations(FieldIdItem field, AnnotationSetItem fieldAnnotations) { 65 fieldAnnotationsMap.put(field.getIndex(), fieldAnnotations); 66 } 67 }); 68 69 annotationDirectory.iteratParameterAnnotations( 70 new AnnotationDirectoryItem.ParameterAnnotationIteratorDelegate() { 71 public void processParameterAnnotations(MethodIdItem method, AnnotationSetRefList parameterAnnotations) { 72 parameterAnnotationsMap.put(method.getIndex(), parameterAnnotations); 73 } 74 }); 75 } 76 77 public List<String> getAccessFlags() { 78 List<String> accessFlags = new ArrayList<String>(); 79 80 for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) { 81 accessFlags.add(accessFlag.toString()); 82 } 83 84 return accessFlags; 85 } 86 87 public String getClassType() { 88 return classDefItem.getClassType().getTypeDescriptor(); 89 } 90 91 public String getSuperType() { 92 TypeIdItem superClass = classDefItem.getSuperclass(); 93 if (superClass != null) { 94 return superClass.getTypeDescriptor(); 95 } 96 return null; 97 } 98 99 public String getSourceFile() { 100 StringIdItem sourceFile = classDefItem.getSourceFile(); 101 102 if (sourceFile == null) { 103 return null; 104 } 105 return classDefItem.getSourceFile().getStringValue(); 106 } 107 108 public List<String> getInterfaces() { 109 List<String> interfaces = new ArrayList<String>(); 110 111 TypeListItem interfaceList = classDefItem.getInterfaces(); 112 113 if (interfaceList != null) { 114 for (TypeIdItem typeIdItem: interfaceList.getTypes()) { 115 interfaces.add(typeIdItem.getTypeDescriptor()); 116 } 117 } 118 119 return interfaces; 120 } 121 122 public List<FieldDefinition> getStaticFields() { 123 List<FieldDefinition> staticFields = new ArrayList<FieldDefinition>(); 124 125 if (classDataItem != null) { 126 127 EncodedArrayItem encodedStaticInitializers = classDefItem.getStaticFieldInitializers(); 128 129 EncodedValue[] staticInitializers; 130 if (encodedStaticInitializers != null) { 131 staticInitializers = encodedStaticInitializers.getEncodedArray().values; 132 } else { 133 staticInitializers = new EncodedValue[0]; 134 } 135 136 int i=0; 137 for (ClassDataItem.EncodedField field: classDataItem.getStaticFields()) { 138 EncodedValue encodedValue = null; 139 if (i < staticInitializers.length) { 140 encodedValue = staticInitializers[i]; 141 } 142 AnnotationSetItem annotationSet = fieldAnnotationsMap.get(field.field.getIndex()); 143 staticFields.add(new FieldDefinition(field, encodedValue, annotationSet)); 144 i++; 145 } 146 } 147 return staticFields; 148 } 149 150 public List<FieldDefinition> getInstanceFields() { 151 List<FieldDefinition> instanceFields = new ArrayList<FieldDefinition>(); 152 153 if (classDataItem != null) { 154 for (ClassDataItem.EncodedField field: classDataItem.getInstanceFields()) { 155 AnnotationSetItem annotationSet = fieldAnnotationsMap.get(field.field.getIndex()); 156 instanceFields.add(new FieldDefinition(field, annotationSet)); 157 } 158 } 159 160 return instanceFields; 161 } 162 163 public List<MethodDefinition> getDirectMethods() { 164 List<MethodDefinition> directMethods = new ArrayList<MethodDefinition>(); 165 166 if (classDataItem != null) { 167 for (ClassDataItem.EncodedMethod method: classDataItem.getDirectMethods()) { 168 AnnotationSetItem annotationSet = methodAnnotationsMap.get(method.method.getIndex()); 169 AnnotationSetRefList parameterAnnotationList = parameterAnnotationsMap.get(method.method.getIndex()); 170 directMethods.add(new MethodDefinition(method, annotationSet, parameterAnnotationList)); 171 } 172 } 173 174 return directMethods; 175 } 176 177 public List<MethodDefinition> getVirtualMethods() { 178 List<MethodDefinition> virtualMethods = new ArrayList<MethodDefinition>(); 179 180 if (classDataItem != null) { 181 for (ClassDataItem.EncodedMethod method: classDataItem.getVirtualMethods()) { 182 AnnotationSetItem annotationSet = methodAnnotationsMap.get(method.method.getIndex()); 183 AnnotationSetRefList parameterAnnotationList = parameterAnnotationsMap.get(method.method.getIndex()); 184 virtualMethods.add(new MethodDefinition(method, annotationSet, parameterAnnotationList)); 185 } 186 } 187 188 return virtualMethods; 189 } 190 191 public List<AnnotationAdaptor> getAnnotations() { 192 AnnotationDirectoryItem annotationDirectory = classDefItem.getAnnotations(); 193 if (annotationDirectory == null) { 194 return null; 195 } 196 197 AnnotationSetItem annotationSet = annotationDirectory.getClassAnnotations(); 198 if (annotationSet == null) { 199 return null; 200 } 201 202 List<AnnotationAdaptor> annotationAdaptors = new ArrayList<AnnotationAdaptor>(); 203 204 for (AnnotationItem annotationItem: annotationSet.getAnnotations()) { 205 annotationAdaptors.add(new AnnotationAdaptor(annotationItem)); 206 } 207 return annotationAdaptors; 208 } 209} 210