Use a SparseArray instead of a HashMap, and add get*Count methods to AnnotationDirectoryItem so we can initialize the SparseArrays with the correct capacity

git-svn-id: https://smali.googlecode.com/svn/trunk@478 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2009-09-19 03:18:35 +00:00
parent cd5d4c0385
commit 29f49465ca
2 changed files with 32 additions and 9 deletions

View File

@ -31,6 +31,7 @@ package org.jf.baksmali.Adaptors;
import org.jf.dexlib.EncodedValue.EncodedValue;
import org.jf.dexlib.*;
import org.jf.dexlib.Util.AccessFlags;
import org.jf.dexlib.Util.SparseArray;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
@ -41,9 +42,9 @@ public class ClassDefinition {
private ClassDefItem classDefItem;
private ClassDataItem classDataItem;
private HashMap<Integer, AnnotationSetItem> methodAnnotationsMap = new HashMap<Integer, AnnotationSetItem>();
private HashMap<Integer, AnnotationSetItem> fieldAnnotationsMap = new HashMap<Integer, AnnotationSetItem>();
private HashMap<Integer, AnnotationSetRefList> parameterAnnotationsMap = new HashMap<Integer, AnnotationSetRefList>();
private SparseArray<AnnotationSetItem> methodAnnotationsMap;
private SparseArray<AnnotationSetItem> fieldAnnotationsMap;
private SparseArray<AnnotationSetRefList> parameterAnnotationsMap;
public ClassDefinition(StringTemplateGroup stg, ClassDefItem classDefItem) {
this.stg = stg;
@ -75,19 +76,23 @@ public class ClassDefinition {
return;
}
methodAnnotationsMap = new SparseArray<AnnotationSetItem>(annotationDirectory.getMethodAnnotationCount());
annotationDirectory.iterateMethodAnnotations(new AnnotationDirectoryItem.MethodAnnotationIteratorDelegate() {
public void processMethodAnnotations(MethodIdItem method, AnnotationSetItem methodAnnotations) {
methodAnnotationsMap.put(method.getIndex(), methodAnnotations);
}
});
fieldAnnotationsMap = new SparseArray<AnnotationSetItem>(annotationDirectory.getFieldAnnotationCount());
annotationDirectory.iterateFieldAnnotations(new AnnotationDirectoryItem.FieldAnnotationIteratorDelegate() {
public void processFieldAnnotations(FieldIdItem field, AnnotationSetItem fieldAnnotations) {
fieldAnnotationsMap.put(field.getIndex(), fieldAnnotations);
}
});
annotationDirectory.iteratParameterAnnotations(
parameterAnnotationsMap = new SparseArray<AnnotationSetRefList>(
annotationDirectory.getParameterAnnotationCount());
annotationDirectory.iterateParameterAnnotations(
new AnnotationDirectoryItem.ParameterAnnotationIteratorDelegate() {
public void processParameterAnnotations(MethodIdItem method, AnnotationSetRefList parameterAnnotations) {
parameterAnnotationsMap.put(method.getIndex(), parameterAnnotations);

View File

@ -28,12 +28,9 @@
package org.jf.dexlib;
import org.jf.dexlib.EncodedValue.AnnotationEncodedSubValue;
import org.jf.dexlib.Util.ArrayUtils;
import org.jf.dexlib.Util.Input;
import org.jf.dexlib.Util.AnnotatedOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -338,6 +335,13 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
void processFieldAnnotations(FieldIdItem field, AnnotationSetItem fieldAnnotations);
}
/**
* @return the number of field annotations in this <code>AnnotationDirectoryItem</code>
*/
public int getFieldAnnotationCount() {
return fieldAnnotationFields.length;
}
/**
* Iterates over the method annotations, calling delegate.processMethodAnnotations for each
* @param delegate the delegate to call
@ -352,11 +356,18 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
void processMethodAnnotations(MethodIdItem method, AnnotationSetItem methodAnnotations);
}
/**
* @return the number of method annotations in this <code>AnnotationDirectoryItem</code>
*/
public int getMethodAnnotationCount() {
return methodAnnotationMethods.length;
}
/**
* Iterates over the parameter annotations, calling delegate.processParameterAnnotations for each
* @param delegate the delegate to call
*/
public void iteratParameterAnnotations(ParameterAnnotationIteratorDelegate delegate) {
public void iterateParameterAnnotations(ParameterAnnotationIteratorDelegate delegate) {
for (int i=0; i<parameterAnnotationMethods.length; i++) {
delegate.processParameterAnnotations(parameterAnnotationMethods[i], parameterAnnotations[i]);
}
@ -366,6 +377,13 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
void processParameterAnnotations(MethodIdItem method, AnnotationSetRefList parameterAnnotations);
}
/**
* @return the number of parameter annotations in this <code>AnnotationDirectoryItem</code>
*/
public int getParameterAnnotationCount() {
return parameterAnnotationMethods.length;
}
/**
* @return true if this <code>AnnotationDirectoryItem</code> is internable. It is only internable if it has
* only class annotations, but no field, method or parameter annotations