diff --git a/baksmali/src/main/java/org/jf/baksmali/Deodex/DeodexUtil.java b/baksmali/src/main/java/org/jf/baksmali/Deodex/DeodexUtil.java index bb1242c2..c12de70f 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Deodex/DeodexUtil.java +++ b/baksmali/src/main/java/org/jf/baksmali/Deodex/DeodexUtil.java @@ -41,7 +41,6 @@ public class DeodexUtil { public DeodexUtil(Deodexerant deodexerant) { this.deodexerant = deodexerant; - deodexerant.dexFile.disableInterning(); } private List makeInsnList(final CodeItem codeItem) { diff --git a/baksmali/src/main/java/org/jf/baksmali/Deodex/Deodexerant.java b/baksmali/src/main/java/org/jf/baksmali/Deodex/Deodexerant.java index f44e3679..ef950e16 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Deodex/Deodexerant.java +++ b/baksmali/src/main/java/org/jf/baksmali/Deodex/Deodexerant.java @@ -112,7 +112,7 @@ public class Deodexerant { } private TypeIdItem resolveTypeOrSupertype(String type) { - TypeIdItem typeItem = TypeIdItem.getInternedTypeIdItem(dexFile, type); + TypeIdItem typeItem = TypeIdItem.internTypeIdItem(dexFile, type); while (typeItem == null) { type = lookupSuperclass(type); @@ -120,7 +120,7 @@ public class Deodexerant { throw new RuntimeException("Could not find the type or a supertype of " + type + " in the dex file"); } - typeItem = TypeIdItem.getInternedTypeIdItem(dexFile, type); + typeItem = TypeIdItem.internTypeIdItem(dexFile, type); } return typeItem; } @@ -249,7 +249,7 @@ public class Deodexerant { String methodRet) { TypeIdItem classTypeItem = resolveTypeOrSupertype(classType); - StringIdItem methodNameItem = StringIdItem.getInternedStringIdItem(dexFile, methodName); + StringIdItem methodNameItem = StringIdItem.internStringIdItem(dexFile, methodName); if (methodNameItem == null) { return null; } @@ -318,7 +318,7 @@ public class Deodexerant { TypeListItem paramListItem = null; if (paramList.size() > 0) { - paramListItem = TypeListItem.getInternedTypeListItem(dexFile, paramList); + paramListItem = TypeListItem.internTypeListItem(dexFile, paramList); if (paramListItem == null) { throw new RuntimeException("Could not find type list item in dex file"); } @@ -326,7 +326,7 @@ public class Deodexerant { TypeIdItem retType = getType(methodRet); - ProtoIdItem protoItem = ProtoIdItem.getInternedProtoIdItem(dexFile, retType, paramListItem); + ProtoIdItem protoItem = ProtoIdItem.internProtoIdItem(dexFile, retType, paramListItem); if (protoItem == null) { return null; } @@ -334,7 +334,7 @@ public class Deodexerant { MethodIdItem methodIdItem; do { - methodIdItem = MethodIdItem.getInternedMethodIdItem(dexFile, classTypeItem, protoItem, methodNameItem); + methodIdItem = MethodIdItem.internMethodIdItem(dexFile, classTypeItem, protoItem, methodNameItem); if (methodIdItem != null) { return methodIdItem; } @@ -343,11 +343,11 @@ public class Deodexerant { if (superclassDescriptor == null) { return null; } - classTypeItem = TypeIdItem.getInternedTypeIdItem(dexFile, superclassDescriptor); + classTypeItem = TypeIdItem.internTypeIdItem(dexFile, superclassDescriptor); while (classTypeItem == null && superclassDescriptor != null) { superclassDescriptor = lookupSuperclass(superclassDescriptor); - classTypeItem = TypeIdItem.getInternedTypeIdItem(dexFile, superclassDescriptor); + classTypeItem = TypeIdItem.internTypeIdItem(dexFile, superclassDescriptor); } } while (true); } @@ -371,12 +371,12 @@ public class Deodexerant { String fieldName = parts[0]; String fieldType = parts[1]; - StringIdItem fieldNameItem = StringIdItem.getInternedStringIdItem(dexFile, fieldName); + StringIdItem fieldNameItem = StringIdItem.internStringIdItem(dexFile, fieldName); if (fieldNameItem == null) { return null; } - TypeIdItem fieldTypeItem = TypeIdItem.getInternedTypeIdItem(dexFile, fieldType); + TypeIdItem fieldTypeItem = TypeIdItem.internTypeIdItem(dexFile, fieldType); if (fieldTypeItem == null) { return null; } @@ -384,17 +384,17 @@ public class Deodexerant { FieldIdItem fieldIdItem; do { - fieldIdItem = FieldIdItem.getInternedFieldIdItem(dexFile, classTypeItem, fieldTypeItem, fieldNameItem); + fieldIdItem = FieldIdItem.internFieldIdItem(dexFile, classTypeItem, fieldTypeItem, fieldNameItem); if (fieldIdItem != null) { return fieldIdItem; } String superclassDescriptor = lookupSuperclass(classTypeItem.getTypeDescriptor()); - classTypeItem = TypeIdItem.getInternedTypeIdItem(dexFile, superclassDescriptor); + classTypeItem = TypeIdItem.internTypeIdItem(dexFile, superclassDescriptor); while (classTypeItem == null && superclassDescriptor != null) { superclassDescriptor = lookupSuperclass(superclassDescriptor); - classTypeItem = TypeIdItem.getInternedTypeIdItem(dexFile, superclassDescriptor); + classTypeItem = TypeIdItem.internTypeIdItem(dexFile, superclassDescriptor); } } while (classTypeItem != null); throw new RuntimeException("Could not find field in dex file"); @@ -447,7 +447,7 @@ public class Deodexerant { } private TypeIdItem getType(String typeDescriptor) { - TypeIdItem type = TypeIdItem.getInternedTypeIdItem(dexFile, typeDescriptor); + TypeIdItem type = TypeIdItem.internTypeIdItem(dexFile, typeDescriptor); if (type == null) { throw new RuntimeException("Could not find type \"" + typeDescriptor + "\" in dex file"); } diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java index 14a1fed4..70439198 100644 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java @@ -108,7 +108,7 @@ public class AnnotationDirectoryItem extends Item { * @return an AnnotationItem for the given values, and that has been interned into the given * DexFile */ - public static AnnotationDirectoryItem getInternedAnnotationDirectoryItem(DexFile dexFile, + public static AnnotationDirectoryItem internAnnotationDirectoryItem(DexFile dexFile, AnnotationSetItem classAnnotations, List fieldAnnotations, List methodAnnotations, diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java index ec02004e..e31de243 100644 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java @@ -68,7 +68,7 @@ public class AnnotationItem extends Item { * @return an AnnotationItem for the given values, and that has been interned into the given * DexFile */ - 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); diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java index 945bd0cd..2fd81ae3 100644 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java @@ -63,7 +63,7 @@ public class AnnotationSetItem extends Item { * @param annotations The annotations for this AnnotationSetItem * @return an AnnotationSetItem for the given annotations */ - public static AnnotationSetItem getInternedAnnotationSetItem(DexFile dexFile, List annotations) { + public static AnnotationSetItem internAnnotationSetItem(DexFile dexFile, List annotations) { AnnotationItem[] annotationsArray = new AnnotationItem[annotations.size()]; annotations.toArray(annotationsArray); AnnotationSetItem annotationSetItem = new AnnotationSetItem(dexFile, annotationsArray); diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetRefList.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationSetRefList.java index 53d05a5f..a6296ac5 100644 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetRefList.java +++ b/dexlib/src/main/java/org/jf/dexlib/AnnotationSetRefList.java @@ -63,7 +63,7 @@ public class AnnotationSetRefList extends Item { * @param annotationSets The annotation sets for this AnnotationSetRefList * @return an AnnotationSetItem for the given annotations */ - public static AnnotationSetRefList getInternedAnnotationSetRefList(DexFile dexFile, + public static AnnotationSetRefList internAnnotationSetRefList(DexFile dexFile, List annotationSets) { AnnotationSetItem[] annotationSetsArray = new AnnotationSetItem[annotationSets.size()]; annotationSets.toArray(annotationSetsArray); diff --git a/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java b/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java index 31e4e9a6..9a4dd32e 100644 --- a/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java @@ -75,7 +75,7 @@ public class ClassDataItem extends Item { * @param virtualMethods The virtual methods for this class * @return a new ClassDataItem with the given values */ - public static ClassDataItem getInternedClassDataItem(DexFile dexFile, List staticFields, + public static ClassDataItem internClassDataItem(DexFile dexFile, List staticFields, List instanceFields, List directMethods, List virtualMethods) { diff --git a/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java b/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java index 0151fc86..c83e8642 100644 --- a/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java @@ -112,7 +112,7 @@ public class ClassDefItem extends Item { * @return a ClassDefItem for the given values, and that has been interned into the given * DexFile */ - 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 staticFieldInitializers) { @@ -128,6 +128,40 @@ public class ClassDefItem extends Item { return dexFile.ClassDefsSection.intern(classDefItem); } + /** + * Looks up a ClassDefItem from the given DexFile for the given + * values + * @param dexFile The DexFile that the ClassDefItem 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 ClassDataItem 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 + * StaticFieldInitializer 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 ClassDefItem from the given DexFile 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 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 { } ArrayEncodedSubValue encodedArrayValue = new ArrayEncodedSubValue(values); - return EncodedArrayItem.getInternedEncodedArrayItem(dexFile, encodedArrayValue); + return EncodedArrayItem.internEncodedArrayItem(dexFile, encodedArrayValue); } } diff --git a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java b/dexlib/src/main/java/org/jf/dexlib/CodeItem.java index 49d4c1f4..4f27d959 100644 --- a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/CodeItem.java @@ -105,7 +105,7 @@ public class CodeItem extends Item { * @param encodedCatchHandlers a list of the exception handlers defined for this code/method or null if none * @return a new CodeItem with the given values. */ - public static CodeItem getInternedCodeItem(DexFile dexFile, + public static CodeItem internCodeItem(DexFile dexFile, int registerCount, int inWords, int outWords, diff --git a/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java b/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java index a0fe02d9..6cc532ad 100644 --- a/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java @@ -87,7 +87,7 @@ public class DebugInfoItem extends Item { * debug info * @return a new DebugInfoItem with the given values */ - public static DebugInfoItem getInternedDebugInfoItem(DexFile dexFile, + public static DebugInfoItem internDebugInfoItem(DexFile dexFile, int lineStart, StringIdItem[] parameterNames, byte[] encodedDebugInfo, diff --git a/dexlib/src/main/java/org/jf/dexlib/DexFile.java b/dexlib/src/main/java/org/jf/dexlib/DexFile.java index b12ff4e8..db98eae0 100644 --- a/dexlib/src/main/java/org/jf/dexlib/DexFile.java +++ b/dexlib/src/main/java/org/jf/dexlib/DexFile.java @@ -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 */ diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java b/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java index 99ef6cc7..01a415db 100644 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java @@ -62,7 +62,7 @@ public class EncodedArrayItem extends Item { * @param encodedArray The encoded array value * @return an EncodedArrayItem 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); } diff --git a/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java b/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java index 058aeb19..854d8ddd 100644 --- a/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java @@ -75,12 +75,28 @@ public class FieldIdItem extends Item { * @return a FieldIdItem for the given values, and that has been interned into * the given DexFile */ - 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 FieldIdItem from the given DexFile for the given + * values + * @param dexFile The DexFile 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 FieldIdItem from the given DexFile 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()); diff --git a/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java b/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java index ecdc66ee..529b9041 100644 --- a/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java @@ -70,12 +70,28 @@ public class MethodIdItem extends Item { * @return a MethodIdItem for the given values, and that has been interned into * the given DexFile */ - 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 MethodIdItem from the given DexFile for the given + * values + * @param dexFile The DexFile 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 MethodIdItem from the given DexFile 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()); diff --git a/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java b/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java index 4e3ebf06..1589caff 100644 --- a/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java @@ -59,7 +59,7 @@ public class ProtoIdItem extends Item { 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 { * @return a ProtoIdItem for the given values, and that has been interned into * the given DexFile */ - 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 ProtoIdItem from the given DexFile for the given + * values + * @param dexFile the Dexfile to find the type in + * @param returnType the return type + * @param parameters a TypeListItem containing a list of the parameter types + * @return a ProtoIdItem from the given DexFile 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()); diff --git a/dexlib/src/main/java/org/jf/dexlib/Section.java b/dexlib/src/main/java/org/jf/dexlib/Section.java index a9e1cc48..cb3ad439 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Section.java +++ b/dexlib/src/main/java/org/jf/dexlib/Section.java @@ -178,7 +178,7 @@ public abstract class Section { return null; } T internedItem = getInternedItem(item); - if (internedItem == null && !item.dexFile.getInterningDisabled()) { + if (internedItem == null) { uniqueItems.put(item, item); items.add(item); return item; diff --git a/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java b/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java index ff94c5ab..f7f43d2c 100644 --- a/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java @@ -62,11 +62,24 @@ public class StringDataItem extends Item { * @return a StringDataItem for the given values, and that has been interned into * the given DexFile */ - 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 StringDataItem from the given DexFile for the given + * string value + * @param dexFile the Dexfile to find the string value in + * @param value The string value to look up + * @return a StringDataItem from the given DexFile 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 diff --git a/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java b/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java index 95f3dbfa..84716b53 100644 --- a/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java @@ -61,8 +61,8 @@ public class StringIdItem extends Item { * @return a StringIdItem for the given values, and that has been interned into * the given DexFile */ - 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 { return dexFile.StringIdsSection.intern(stringIdItem); } + /** + * Looks up the StringIdItem from the given DexFile for the given + * string value + * @param dexFile the Dexfile to find the string value in + * @param stringValue The string value to look up + * @return a StringIdItem from the given DexFile 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(); diff --git a/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java b/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java index 800f6c15..411e6087 100644 --- a/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java @@ -62,7 +62,7 @@ public class TypeIdItem extends Item { * @return a TypeIdItem for the given values, and that has been interned into * the given DexFile */ - 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 { * @return a TypeIdItem for the given values, and that has been interned into * the given DexFile */ - 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 { return dexFile.TypeIdsSection.intern(typeIdItem); } + /** + * Looks up the TypeIdItem from the given DexFile for the given + * type descriptor + * @param dexFile the Dexfile to find the type in + * @param typeDescriptor The string containing the type descriptor to look up + * @return a TypeIdItem from the given DexFile 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(); diff --git a/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java b/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java index c601a081..b947962a 100644 --- a/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java @@ -66,13 +66,28 @@ public class TypeListItem extends Item { * @return a TypeListItem for the given values, and that has been interned into * the given DexFile */ - public static TypeListItem getInternedTypeListItem(DexFile dexFile, List typeList) { + public static TypeListItem internTypeListItem(DexFile dexFile, List typeList) { TypeIdItem[] typeArray = new TypeIdItem[typeList.size()]; typeList.toArray(typeArray); TypeListItem typeListItem = new TypeListItem(dexFile, typeArray); return dexFile.TypeListsSection.intern(typeListItem); } + /** + * Looks up the TypeListItem from the given DexFile for the given + * list of types + * @param dexFile the Dexfile to find the type in + * @param typeList A list of the types that the TypeListItem represents + * @return a TypeListItem from the given DexFile for the given + * list of types, or null if it doesn't exist + */ + public static TypeListItem lookupTypeListItem(DexFile dexFile, List 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(); diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java b/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java index 1d4ecba1..d4e81e29 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java +++ b/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java @@ -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 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)); } } } diff --git a/smali/src/main/antlr3/org/jf/smali/smaliTreeWalker.g b/smali/src/main/antlr3/org/jf/smali/smaliTreeWalker.g index a7eb6b71..d870bd9b 100644 --- a/smali/src/main/antlr3/org/jf/smali/smaliTreeWalker.g +++ b/smali/src/main/antlr3/org/jf/smali/smaliTreeWalker.g @@ -158,7 +158,7 @@ smali_file $methods.parameterAnnotations != null || $fields.fieldAnnotations != null || $annotations.annotationSetItem != null) { - annotationDirectoryItem = AnnotationDirectoryItem.getInternedAnnotationDirectoryItem( + annotationDirectoryItem = AnnotationDirectoryItem.internAnnotationDirectoryItem( dexFile, $annotations.annotationSetItem, $fields.fieldAnnotations, @@ -168,11 +168,11 @@ smali_file if ($fields.staticFields.size() != 0 || $fields.instanceFields.size() != 0 || $methods.directMethods.size() != 0 || $methods.virtualMethods.size()!= 0) { - classDataItem = ClassDataItem.getInternedClassDataItem(dexFile, $fields.staticFields, $fields.instanceFields, + classDataItem = ClassDataItem.internClassDataItem(dexFile, $fields.staticFields, $fields.instanceFields, $methods.directMethods, $methods.virtualMethods); } - classDefItem = ClassDefItem.getInternedClassDefItem(dexFile, $header.classType, $header.accessFlags, + classDefItem = ClassDefItem.internClassDefItem(dexFile, $header.classType, $header.accessFlags, $header.superType, $header.implementsList, $header.sourceSpec, annotationDirectoryItem, classDataItem, $fields.staticFieldInitialValues); }; @@ -220,7 +220,7 @@ implements_list returns[TypeListItem implementsList] (implements_spec {typeList.add($implements_spec.type);} )* { if (typeList.size() > 0) { - $implementsList = TypeListItem.getInternedTypeListItem(dexFile, typeList); + $implementsList = TypeListItem.internTypeListItem(dexFile, typeList); } else { $implementsList = null; } @@ -228,7 +228,7 @@ implements_list returns[TypeListItem implementsList] source_spec returns[StringIdItem source] : {$source = null;} - ^(I_SOURCE string_literal {$source = StringIdItem.getInternedStringIdItem(dexFile, $string_literal.value);}) + ^(I_SOURCE string_literal {$source = StringIdItem.internStringIdItem(dexFile, $string_literal.value);}) | ; @@ -314,10 +314,10 @@ methods returns[List directMethods, field returns [ClassDataItem.EncodedField encodedField, EncodedValue encodedValue, AnnotationSetItem fieldAnnotationSet] :^(I_FIELD MEMBER_NAME access_list ^(I_FIELD_TYPE nonvoid_type_descriptor) field_initial_value annotations?) { - StringIdItem memberName = StringIdItem.getInternedStringIdItem(dexFile, $MEMBER_NAME.text); + StringIdItem memberName = StringIdItem.internStringIdItem(dexFile, $MEMBER_NAME.text); TypeIdItem fieldType = $nonvoid_type_descriptor.type; - FieldIdItem fieldIdItem = FieldIdItem.getInternedFieldIdItem(dexFile, classType, fieldType, memberName); + FieldIdItem fieldIdItem = FieldIdItem.internFieldIdItem(dexFile, classType, fieldType, memberName); $encodedField = new ClassDataItem.EncodedField(fieldIdItem, $access_list.value); if ($field_initial_value.encodedValue != null) { @@ -348,7 +348,7 @@ literal returns[EncodedValue encodedValue] | float_literal { $encodedValue = new FloatEncodedValue($float_literal.value); } | double_literal { $encodedValue = new DoubleEncodedValue($double_literal.value); } | char_literal { $encodedValue = new CharEncodedValue($char_literal.value); } - | string_literal { $encodedValue = new StringEncodedValue(StringIdItem.getInternedStringIdItem(dexFile, $string_literal.value)); } + | string_literal { $encodedValue = new StringEncodedValue(StringIdItem.internStringIdItem(dexFile, $string_literal.value)); } | bool_literal { $encodedValue = $bool_literal.value?BooleanEncodedValue.TrueValue:BooleanEncodedValue.FalseValue; } | NULL_LITERAL { $encodedValue = NullEncodedValue.NullValue; } | type_descriptor { $encodedValue = new TypeEncodedValue($type_descriptor.type); } @@ -537,7 +537,7 @@ method returns[ ClassDataItem.EncodedMethod encodedMethod, " parameters."); } - codeItem = CodeItem.getInternedCodeItem(dexFile, + codeItem = CodeItem.internCodeItem(dexFile, totalMethodRegisters, methodParameterRegisters, $statements.maxOutRegisters, @@ -565,20 +565,20 @@ method_prototype returns[ProtoIdItem protoIdItem] List parameterTypes = $field_type_list.types; TypeListItem parameterTypeListItem = null; if (parameterTypes != null && parameterTypes.size() > 0) { - parameterTypeListItem = TypeListItem.getInternedTypeListItem(dexFile, parameterTypes); + parameterTypeListItem = TypeListItem.internTypeListItem(dexFile, parameterTypes); } - $protoIdItem = ProtoIdItem.getInternedProtoIdItem(dexFile, returnType, parameterTypeListItem); + $protoIdItem = ProtoIdItem.internProtoIdItem(dexFile, returnType, parameterTypeListItem); }; method_name_and_prototype returns[MethodIdItem methodIdItem] : MEMBER_NAME method_prototype { String methodNameString = $MEMBER_NAME.text; - StringIdItem methodName = StringIdItem.getInternedStringIdItem(dexFile, methodNameString); + StringIdItem methodName = StringIdItem.internStringIdItem(dexFile, methodNameString); ProtoIdItem protoIdItem = $method_prototype.protoIdItem; - $methodIdItem = MethodIdItem.getInternedMethodIdItem(dexFile, classType, protoIdItem, methodName); + $methodIdItem = MethodIdItem.internMethodIdItem(dexFile, classType, protoIdItem, methodName); }; field_type_list returns[List types] @@ -598,18 +598,18 @@ fully_qualified_method returns[MethodIdItem methodIdItem] : reference_type_descriptor MEMBER_NAME method_prototype { TypeIdItem classType = $reference_type_descriptor.type; - StringIdItem methodName = StringIdItem.getInternedStringIdItem(dexFile, $MEMBER_NAME.text); + StringIdItem methodName = StringIdItem.internStringIdItem(dexFile, $MEMBER_NAME.text); ProtoIdItem prototype = $method_prototype.protoIdItem; - $methodIdItem = MethodIdItem.getInternedMethodIdItem(dexFile, classType, prototype, methodName); + $methodIdItem = MethodIdItem.internMethodIdItem(dexFile, classType, prototype, methodName); }; fully_qualified_field returns[FieldIdItem fieldIdItem] : reference_type_descriptor MEMBER_NAME nonvoid_type_descriptor { TypeIdItem classType = $reference_type_descriptor.type; - StringIdItem fieldName = StringIdItem.getInternedStringIdItem(dexFile, $MEMBER_NAME.text); + StringIdItem fieldName = StringIdItem.internStringIdItem(dexFile, $MEMBER_NAME.text); TypeIdItem fieldType = $nonvoid_type_descriptor.type; - $fieldIdItem = FieldIdItem.getInternedFieldIdItem(dexFile, classType, fieldType, fieldName); + $fieldIdItem = FieldIdItem.internFieldIdItem(dexFile, classType, fieldType, fieldName); }; registers_directive returns[boolean isLocalsDirective, int registers] @@ -706,7 +706,7 @@ parameters returns[AnnotationSetRefList parameterAnnotations] { if ($parameter.parameterAnnotationSet != null) { while (annotationSetItems.size() < parameterCount) { - annotationSetItems.add(AnnotationSetItem.getInternedAnnotationSetItem(dexFile, null)); + annotationSetItems.add(AnnotationSetItem.internAnnotationSetItem(dexFile, null)); } annotationSetItems.add($parameter.parameterAnnotationSet); } @@ -717,9 +717,9 @@ parameters returns[AnnotationSetRefList parameterAnnotations] { if (annotationSetItems.size() > 0) { while (annotationSetItems.size() < parameterCount) { - annotationSetItems.add(AnnotationSetItem.getInternedAnnotationSetItem(dexFile, null)); + annotationSetItems.add(AnnotationSetItem.internAnnotationSetItem(dexFile, null)); } - $parameterAnnotations = AnnotationSetRefList.getInternedAnnotationSetRefList(dexFile, annotationSetItems); + $parameterAnnotations = AnnotationSetRefList.internAnnotationSetRefList(dexFile, annotationSetItems); } }; @@ -941,7 +941,7 @@ instruction[int totalMethodRegisters, int methodParameterRegisters, List 0) { - $annotationSetItem = AnnotationSetItem.getInternedAnnotationSetItem(dexFile, annotationList); + $annotationSetItem = AnnotationSetItem.internAnnotationSetItem(dexFile, annotationList); } }; @@ -1385,13 +1385,13 @@ annotation returns[AnnotationItem annotationItem] AnnotationVisibility visibility = AnnotationVisibility.valueOf($ANNOTATION_VISIBILITY.text.toUpperCase()); AnnotationEncodedSubValue encodedAnnotation = new AnnotationEncodedSubValue($subannotation.annotationType, $subannotation.elementNames, $subannotation.elementValues); - $annotationItem = AnnotationItem.getInternedAnnotationItem(dexFile, visibility, encodedAnnotation); + $annotationItem = AnnotationItem.internAnnotationItem(dexFile, visibility, encodedAnnotation); }; annotation_element returns[StringIdItem elementName, EncodedValue elementValue] : ^(I_ANNOTATION_ELEMENT MEMBER_NAME literal) { - $elementName = StringIdItem.getInternedStringIdItem(dexFile, $MEMBER_NAME.text); + $elementName = StringIdItem.internStringIdItem(dexFile, $MEMBER_NAME.text); $elementValue = $literal.encodedValue; };