remove the "disable interning" functionality in dexlib

- rename the getInterned* methods to intern*
- add a lookup* method to a few item types that performs the same function, except that
it returns null if the item isn't found, instead of interning it

git-svn-id: https://smali.googlecode.com/svn/trunk@632 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com
2010-02-22 00:55:50 +00:00
parent f5defb97c0
commit 928790f293
22 changed files with 218 additions and 97 deletions

View File

@ -108,7 +108,7 @@ public class AnnotationDirectoryItem extends Item<AnnotationDirectoryItem> {
* @return an <code>AnnotationItem</code> for the given values, and that has been interned into the given
* <code>DexFile</code>
*/
public static AnnotationDirectoryItem getInternedAnnotationDirectoryItem(DexFile dexFile,
public static AnnotationDirectoryItem internAnnotationDirectoryItem(DexFile dexFile,
AnnotationSetItem classAnnotations,
List<FieldAnnotation> fieldAnnotations,
List<MethodAnnotation> methodAnnotations,

View File

@ -68,7 +68,7 @@ public class AnnotationItem extends Item<AnnotationItem> {
* @return an <code>AnnotationItem</code> for the given values, and that has been interned into the given
* <code>DexFile</code>
*/
public static AnnotationItem getInternedAnnotationItem(DexFile dexFile, AnnotationVisibility visibility,
public static AnnotationItem internAnnotationItem(DexFile dexFile, AnnotationVisibility visibility,
AnnotationEncodedSubValue annotationValue) {
AnnotationItem annotationItem = new AnnotationItem(dexFile, visibility, annotationValue);
return dexFile.AnnotationsSection.intern(annotationItem);

View File

@ -63,7 +63,7 @@ public class AnnotationSetItem extends Item<AnnotationSetItem> {
* @param annotations The annotations for this <code>AnnotationSetItem</code>
* @return an <code>AnnotationSetItem</code> for the given annotations
*/
public static AnnotationSetItem getInternedAnnotationSetItem(DexFile dexFile, List<AnnotationItem> annotations) {
public static AnnotationSetItem internAnnotationSetItem(DexFile dexFile, List<AnnotationItem> annotations) {
AnnotationItem[] annotationsArray = new AnnotationItem[annotations.size()];
annotations.toArray(annotationsArray);
AnnotationSetItem annotationSetItem = new AnnotationSetItem(dexFile, annotationsArray);

View File

@ -63,7 +63,7 @@ public class AnnotationSetRefList extends Item<AnnotationSetRefList> {
* @param annotationSets The annotation sets for this <code>AnnotationSetRefList</code>
* @return an <code>AnnotationSetItem</code> for the given annotations
*/
public static AnnotationSetRefList getInternedAnnotationSetRefList(DexFile dexFile,
public static AnnotationSetRefList internAnnotationSetRefList(DexFile dexFile,
List<AnnotationSetItem> annotationSets) {
AnnotationSetItem[] annotationSetsArray = new AnnotationSetItem[annotationSets.size()];
annotationSets.toArray(annotationSetsArray);

View File

@ -75,7 +75,7 @@ public class ClassDataItem extends Item<ClassDataItem> {
* @param virtualMethods The virtual methods for this class
* @return a new <code>ClassDataItem</code> with the given values
*/
public static ClassDataItem getInternedClassDataItem(DexFile dexFile, List<EncodedField> staticFields,
public static ClassDataItem internClassDataItem(DexFile dexFile, List<EncodedField> staticFields,
List<EncodedField> instanceFields,
List<EncodedMethod> directMethods,
List<EncodedMethod> virtualMethods) {

View File

@ -112,7 +112,7 @@ public class ClassDefItem extends Item<ClassDefItem> {
* @return a <code>ClassDefItem</code> for the given values, and that has been interned into the given
* <code>DexFile</code>
*/
public static ClassDefItem getInternedClassDefItem(DexFile dexFile, TypeIdItem classType, int accessFlags,
public static ClassDefItem internClassDefItem(DexFile dexFile, TypeIdItem classType, int accessFlags,
TypeIdItem superType, TypeListItem implementedInterfaces, StringIdItem sourceFile,
AnnotationDirectoryItem annotations, ClassDataItem classData,
List<StaticFieldInitializer> staticFieldInitializers) {
@ -128,6 +128,40 @@ public class ClassDefItem extends Item<ClassDefItem> {
return dexFile.ClassDefsSection.intern(classDefItem);
}
/**
* Looks up a <code>ClassDefItem</code> from the given <code>DexFile</code> for the given
* values
* @param dexFile The <code>DexFile</code> that the <code>ClassDefItem</code> belongs to
* @param classType The type of the class
* @param accessFlags The access flags of the class
* @param superType The superclass of the class, or null if none (only valid for java.lang.Object)
* @param implementedInterfaces A list of the interfaces that the class implements, or null if none
* @param sourceFile The main source file that the class is defined in, or null if not available
* @param annotations The annotations for the class and its fields, methods and method parameters, or null if none
* @param classData The <code>ClassDataItem</code> containing the method and field definitions for the class
* @param staticFieldInitializers The initial values for the class's static fields, or null if none. If it is not
* null, it must contain the same number of items as the number of static fields in the class. The value in the
* <code>StaticFieldInitializer</code> for any field that doesn't have an explicit initial value can either be null
* or be the type-appropriate null/0 value.
* @return a <code>ClassDefItem</code> from the given <code>DexFile</code> for the given
* values, or null if it doesn't exist
*/
public static ClassDefItem lookupClassDefItem(DexFile dexFile, TypeIdItem classType, int accessFlags,
TypeIdItem superType, TypeListItem implementedInterfaces, StringIdItem sourceFile,
AnnotationDirectoryItem annotations, ClassDataItem classData,
List<StaticFieldInitializer> staticFieldInitializers) {
EncodedArrayItem encodedArrayItem = null;
if(!dexFile.getInplace() && staticFieldInitializers != null && staticFieldInitializers.size() > 0) {
assert classData != null;
assert staticFieldInitializers.size() == classData.getStaticFields().length;
encodedArrayItem = makeStaticFieldInitializersItem(dexFile, staticFieldInitializers);
}
ClassDefItem classDefItem = new ClassDefItem(dexFile, classType, accessFlags, superType, implementedInterfaces,
sourceFile, annotations, classData, encodedArrayItem);
return dexFile.ClassDefsSection.getInternedItem(classDefItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
classType = dexFile.TypeIdsSection.getItemByIndex(in.readInt());
@ -373,6 +407,6 @@ public class ClassDefItem extends Item<ClassDefItem> {
}
ArrayEncodedSubValue encodedArrayValue = new ArrayEncodedSubValue(values);
return EncodedArrayItem.getInternedEncodedArrayItem(dexFile, encodedArrayValue);
return EncodedArrayItem.internEncodedArrayItem(dexFile, encodedArrayValue);
}
}

View File

@ -105,7 +105,7 @@ public class CodeItem extends Item<CodeItem> {
* @param encodedCatchHandlers a list of the exception handlers defined for this code/method or null if none
* @return a new <code>CodeItem</code> with the given values.
*/
public static CodeItem getInternedCodeItem(DexFile dexFile,
public static CodeItem internCodeItem(DexFile dexFile,
int registerCount,
int inWords,
int outWords,

View File

@ -87,7 +87,7 @@ public class DebugInfoItem extends Item<DebugInfoItem> {
* debug info
* @return a new <code>DebugInfoItem</code> with the given values
*/
public static DebugInfoItem getInternedDebugInfoItem(DexFile dexFile,
public static DebugInfoItem internDebugInfoItem(DexFile dexFile,
int lineStart,
StringIdItem[] parameterNames,
byte[] encodedDebugInfo,

View File

@ -30,7 +30,7 @@ package org.jf.dexlib;
import org.jf.dexlib.Util.*;
import org.jf.dexlib.*;
import org.jf.dexlib.Item;
import org.jf.dexlib.Item;
import org.jf.dexlib.StringDataItem;
import java.io.*;
@ -185,9 +185,6 @@ public class DexFile
private int dataSize;
private int fileSize;
private boolean disableInterning = false;
/**
* A private constructor containing common code to initialize the section maps and lists
* @param preserveSignedRegisters If true, keep track of any registers in the debug information
@ -506,23 +503,6 @@ public class DexFile
this.sortAllItems = value;
}
/**
* Disables adding new items to this dex file. The various getInterned*() type
* methods on individual items will return null if there isn't an existing item
* that matches
*/
public void disableInterning() {
this.disableInterning = true;
}
/**
* @return a boolean value indicating whether interning new items has been disabled
* for this dex file
*/
public boolean getInterningDisabled() {
return disableInterning;
}
/**
* @return a boolean value indicating whether this dex file was created by reading in an odex file
*/

View File

@ -62,7 +62,7 @@ public class EncodedArrayItem extends Item<EncodedArrayItem> {
* @param encodedArray The encoded array value
* @return an <code>EncodedArrayItem</code> for the given values, and that has been interned into the given
*/
public static EncodedArrayItem getInternedEncodedArrayItem(DexFile dexFile, ArrayEncodedSubValue encodedArray) {
public static EncodedArrayItem internEncodedArrayItem(DexFile dexFile, ArrayEncodedSubValue encodedArray) {
EncodedArrayItem encodedArrayItem = new EncodedArrayItem(dexFile, encodedArray);
return dexFile.EncodedArraysSection.intern(encodedArrayItem);
}

View File

@ -75,12 +75,28 @@ public class FieldIdItem extends Item<FieldIdItem> {
* @return a <code>FieldIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static FieldIdItem getInternedFieldIdItem(DexFile dexFile, TypeIdItem classType, TypeIdItem fieldType,
public static FieldIdItem internFieldIdItem(DexFile dexFile, TypeIdItem classType, TypeIdItem fieldType,
StringIdItem fieldName) {
FieldIdItem fieldIdItem = new FieldIdItem(dexFile, classType, fieldType, fieldName);
return dexFile.FieldIdsSection.intern(fieldIdItem);
}
/**
* Looks up a <code>FieldIdItem</code> from the given <code>DexFile</code> for the given
* values
* @param dexFile The <code>DexFile</code> that this item belongs to
* @param classType the class that the field is a member of
* @param fieldType the type of the field
* @param fieldName the name of the field
* @return a <code>FieldIdItem</code> from the given <code>DexFile</code> for the given
* values, or null if it doesn't exist
*/
public static FieldIdItem lookupFieldIdItem(DexFile dexFile, TypeIdItem classType, TypeIdItem fieldType,
StringIdItem fieldName) {
FieldIdItem fieldIdItem = new FieldIdItem(dexFile, classType, fieldType, fieldName);
return dexFile.FieldIdsSection.getInternedItem(fieldIdItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
classType = dexFile.TypeIdsSection.getItemByIndex(in.readShort());

View File

@ -70,12 +70,28 @@ public class MethodIdItem extends Item<MethodIdItem> {
* @return a <code>MethodIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static MethodIdItem getInternedMethodIdItem(DexFile dexFile, TypeIdItem classType,
public static MethodIdItem internMethodIdItem(DexFile dexFile, TypeIdItem classType,
ProtoIdItem methodPrototype, StringIdItem methodName) {
MethodIdItem methodIdItem = new MethodIdItem(dexFile, classType, methodPrototype, methodName);
return dexFile.MethodIdsSection.intern(methodIdItem);
}
/**
* Looks up a <code>MethodIdItem</code> from the given <code>DexFile</code> for the given
* values
* @param dexFile The <code>DexFile</code> that this item belongs to
* @param classType the class that the method is a member of
* @param methodPrototype the type of the method
* @param methodName the name of the method
* @return a <code>MethodIdItem</code> from the given <code>DexFile</code> for the given
* values, or null if it doesn't exist
*/
public static MethodIdItem lookupMethodIdItem(DexFile dexFile, TypeIdItem classType,
ProtoIdItem methodPrototype, StringIdItem methodName) {
MethodIdItem methodIdItem = new MethodIdItem(dexFile, classType, methodPrototype, methodName);
return dexFile.MethodIdsSection.getInternedItem(methodIdItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
classType = dexFile.TypeIdsSection.getItemByIndex(in.readShort());

View File

@ -59,7 +59,7 @@ public class ProtoIdItem extends Item<ProtoIdItem> {
if (parameters != null) {
shortyString += parameters.getShortyString();
}
this.shortyDescriptor = StringIdItem.getInternedStringIdItem(dexFile, shortyString);
this.shortyDescriptor = StringIdItem.internStringIdItem(dexFile, shortyString);
this.returnType = returnType;
this.parameters = parameters;
}
@ -73,11 +73,25 @@ public class ProtoIdItem extends Item<ProtoIdItem> {
* @return a <code>ProtoIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static ProtoIdItem getInternedProtoIdItem(DexFile dexFile, TypeIdItem returnType, TypeListItem parameters) {
public static ProtoIdItem internProtoIdItem(DexFile dexFile, TypeIdItem returnType, TypeListItem parameters) {
ProtoIdItem protoIdItem = new ProtoIdItem(dexFile, returnType, parameters);
return dexFile.ProtoIdsSection.intern(protoIdItem);
}
/**
* Looks up the <code>ProtoIdItem</code> from the given <code>DexFile</code> for the given
* values
* @param dexFile the <code>Dexfile</code> to find the type in
* @param returnType the return type
* @param parameters a <code>TypeListItem</code> containing a list of the parameter types
* @return a <code>ProtoIdItem</code> from the given <code>DexFile</code> for the given
* values, or null if it doesn't exist
*/
public static ProtoIdItem lookupProtoIdItem(DexFile dexFile, TypeIdItem returnType, TypeListItem parameters) {
ProtoIdItem protoIdItem = new ProtoIdItem(dexFile, returnType, parameters);
return dexFile.ProtoIdsSection.getInternedItem(protoIdItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
shortyDescriptor = dexFile.StringIdsSection.getItemByIndex(in.readInt());

View File

@ -178,7 +178,7 @@ public abstract class Section<T extends Item> {
return null;
}
T internedItem = getInternedItem(item);
if (internedItem == null && !item.dexFile.getInterningDisabled()) {
if (internedItem == null) {
uniqueItems.put(item, item);
items.add(item);
return item;

View File

@ -62,11 +62,24 @@ public class StringDataItem extends Item<StringDataItem> {
* @return a <code>StringDataItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static StringDataItem getInternedStringDataItem(DexFile dexFile, String value) {
public static StringDataItem internStringDataItem(DexFile dexFile, String value) {
StringDataItem StringDataItem = new StringDataItem(dexFile, value);
return dexFile.StringDataSection.intern(StringDataItem);
}
/**
* Looks up the <code>StringDataItem</code> from the given <code>DexFile</code> for the given
* string value
* @param dexFile the <code>Dexfile</code> to find the string value in
* @param value The string value to look up
* @return a <code>StringDataItem</code> from the given <code>DexFile</code> for the given
* string value, or null if it doesn't exist
**/
public static StringDataItem lookupStringDataItem(DexFile dexFile, String value) {
StringDataItem StringDataItem = new StringDataItem(dexFile, value);
return dexFile.StringDataSection.getInternedItem(StringDataItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
in.readUnsignedLeb128(); //string length

View File

@ -61,8 +61,8 @@ public class StringIdItem extends Item<StringIdItem> {
* @return a <code>StringIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static StringIdItem getInternedStringIdItem(DexFile dexFile, String stringValue) {
StringDataItem stringDataItem = StringDataItem.getInternedStringDataItem(dexFile, stringValue);
public static StringIdItem internStringIdItem(DexFile dexFile, String stringValue) {
StringDataItem stringDataItem = StringDataItem.internStringDataItem(dexFile, stringValue);
if (stringDataItem == null) {
return null;
}
@ -70,6 +70,23 @@ public class StringIdItem extends Item<StringIdItem> {
return dexFile.StringIdsSection.intern(stringIdItem);
}
/**
* Looks up the <code>StringIdItem</code> from the given <code>DexFile</code> for the given
* string value
* @param dexFile the <code>Dexfile</code> to find the string value in
* @param stringValue The string value to look up
* @return a <code>StringIdItem</code> from the given <code>DexFile</code> for the given
* string value, or null if it doesn't exist
*/
public static StringIdItem lookupStringIdItem(DexFile dexFile, String stringValue) {
StringDataItem stringDataItem = StringDataItem.lookupStringDataItem(dexFile, stringValue);
if (stringDataItem == null) {
return null;
}
StringIdItem stringIdItem = new StringIdItem(dexFile, stringDataItem);
return dexFile.StringIdsSection.getInternedItem(stringIdItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
int stringDataOffset = in.readInt();

View File

@ -62,7 +62,7 @@ public class TypeIdItem extends Item<TypeIdItem> {
* @return a <code>TypeIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static TypeIdItem getInternedTypeIdItem(DexFile dexFile, StringIdItem typeDescriptor) {
public static TypeIdItem internTypeIdItem(DexFile dexFile, StringIdItem typeDescriptor) {
TypeIdItem typeIdItem = new TypeIdItem(dexFile, typeDescriptor);
return dexFile.TypeIdsSection.intern(typeIdItem);
}
@ -76,8 +76,8 @@ public class TypeIdItem extends Item<TypeIdItem> {
* @return a <code>TypeIdItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static TypeIdItem getInternedTypeIdItem(DexFile dexFile, String typeDescriptor) {
StringIdItem stringIdItem = StringIdItem.getInternedStringIdItem(dexFile, typeDescriptor);
public static TypeIdItem internTypeIdItem(DexFile dexFile, String typeDescriptor) {
StringIdItem stringIdItem = StringIdItem.internStringIdItem(dexFile, typeDescriptor);
if (stringIdItem == null) {
return null;
}
@ -85,6 +85,23 @@ public class TypeIdItem extends Item<TypeIdItem> {
return dexFile.TypeIdsSection.intern(typeIdItem);
}
/**
* Looks up the <code>TypeIdItem</code> from the given <code>DexFile</code> for the given
* type descriptor
* @param dexFile the <code>Dexfile</code> to find the type in
* @param typeDescriptor The string containing the type descriptor to look up
* @return a <code>TypeIdItem</code> from the given <code>DexFile</code> for the given
* type descriptor, or null if it doesn't exist
*/
public static TypeIdItem lookupTypeIdItem(DexFile dexFile, String typeDescriptor) {
StringIdItem stringIdItem = StringIdItem.lookupStringIdItem(dexFile, typeDescriptor);
if (stringIdItem == null) {
return null;
}
TypeIdItem typeIdItem = new TypeIdItem(dexFile, stringIdItem);
return dexFile.TypeIdsSection.getInternedItem(typeIdItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
int stringIdIndex = in.readInt();

View File

@ -66,13 +66,28 @@ public class TypeListItem extends Item<TypeListItem> {
* @return a <code>TypeListItem</code> for the given values, and that has been interned into
* the given <code>DexFile</code>
*/
public static TypeListItem getInternedTypeListItem(DexFile dexFile, List<TypeIdItem> typeList) {
public static TypeListItem internTypeListItem(DexFile dexFile, List<TypeIdItem> typeList) {
TypeIdItem[] typeArray = new TypeIdItem[typeList.size()];
typeList.toArray(typeArray);
TypeListItem typeListItem = new TypeListItem(dexFile, typeArray);
return dexFile.TypeListsSection.intern(typeListItem);
}
/**
* Looks up the <code>TypeListItem</code> from the given <code>DexFile</code> for the given
* list of types
* @param dexFile the <code>Dexfile</code> to find the type in
* @param typeList A list of the types that the <code>TypeListItem</code> represents
* @return a <code>TypeListItem</code> from the given <code>DexFile</code> for the given
* list of types, or null if it doesn't exist
*/
public static TypeListItem lookupTypeListItem(DexFile dexFile, List<TypeIdItem> typeList) {
TypeIdItem[] typeArray = new TypeIdItem[typeList.size()];
typeList.toArray(typeArray);
TypeListItem typeListItem = new TypeListItem(dexFile, typeArray);
return dexFile.TypeListsSection.getInternedItem(typeListItem);
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
int size = in.readInt();

View File

@ -170,13 +170,13 @@ public class DebugInfoBuilder
if (parameterName == null) {
parameterNamesArray[index++] = null;
} else {
parameterNamesArray[index++] = StringIdItem.getInternedStringIdItem(dexFile, parameterName);
parameterNamesArray[index++] = StringIdItem.internStringIdItem(dexFile, parameterName);
}
}
Item[] referencedItemsArray = new Item[referencedItems.size()];
referencedItems.toArray(referencedItemsArray);
return DebugInfoItem.getInternedDebugInfoItem(dexFile, lineStart, parameterNamesArray, out.toByteArray(),
return DebugInfoItem.internDebugInfoItem(dexFile, lineStart, parameterNamesArray, out.toByteArray(),
referencedItemsArray);
}
@ -307,9 +307,9 @@ public class DebugInfoBuilder
public void emit(DexFile dexFile, Output out, List<Item> referencedItems) {
emitAdvancePC(out, address);
emitStartLocal(out, registerNum);
referencedItems.add(localName==null?null:StringIdItem.getInternedStringIdItem(dexFile, localName));
referencedItems.add(localType==null?null:TypeIdItem.getInternedTypeIdItem(dexFile,
StringIdItem.getInternedStringIdItem(dexFile, localType)));
referencedItems.add(localName==null?null:StringIdItem.internStringIdItem(dexFile, localName));
referencedItems.add(localType==null?null:TypeIdItem.internTypeIdItem(dexFile,
StringIdItem.internStringIdItem(dexFile, localType)));
}
}
@ -338,14 +338,14 @@ public class DebugInfoBuilder
emitAdvancePC(out, address);
emitStartLocalExtended(out, registerNum);
if (localName != null) {
referencedItems.add(StringIdItem.getInternedStringIdItem(dexFile, localName));
referencedItems.add(StringIdItem.internStringIdItem(dexFile, localName));
}
if (localType != null) {
referencedItems.add(TypeIdItem.getInternedTypeIdItem(dexFile,
StringIdItem.getInternedStringIdItem(dexFile, localType)));
referencedItems.add(TypeIdItem.internTypeIdItem(dexFile,
StringIdItem.internStringIdItem(dexFile, localType)));
}
if (signature != null) {
referencedItems.add(StringIdItem.getInternedStringIdItem(dexFile, signature));
referencedItems.add(StringIdItem.internStringIdItem(dexFile, signature));
}
}
}
@ -444,7 +444,7 @@ public class DebugInfoBuilder
emitAdvancePC(out, address);
emitSetFile(out);
if (fileName != null) {
referencedItems.add(StringIdItem.getInternedStringIdItem(dexFile, fileName));
referencedItems.add(StringIdItem.internStringIdItem(dexFile, fileName));
}
}
}