org.ow2.asmdex.structureWriter
Class ConstantPool

java.lang.Object
  extended by org.ow2.asmdex.structureWriter.ConstantPool

public class ConstantPool
extends java.lang.Object

Constant Pool of the Application. It has a link to every element the Application holds. Dalvik requires some structures to be sorted. It is faster to sort them once the Application is parsed than adding elements one after one, so we manage first simple collections, and then the prepareGeneration method builds the sorted ones. Also, we use Symbolic elements from Strings, Fields, Types and Methods so that we can generate the bytecode right after each method is parsed. This is useful to be able to destroy the elements related the Methods when their parsing is over. The indexes are then remapped correctly after the Application is fully parsed (see CodeItem.mapResolvedIndexes()). NOTES : - Empty Strings can be added to the Constant Pool, but only if useful. Empty parameters aren't encoded in the Constant Pool (yet they are declared in the Debug Item). This is tested by the Debug Item encoder though, not the Constant Pool itself. - a "this" String is added by dx, I don't know exactly where it comes from. I add it when a non-static/non-constructor Method is found, it seems logical.

Author:
Julien Névo

Constructor Summary
ConstantPool()
           
 
Method Summary
 void addAnnotationDirectoryItem(AnnotationDirectoryItem annotationDirectoryItem)
          Adds an annotation_directory_item to the Constant Pool.
 void addAnnotationItemToConstantPool(AnnotationItem annotationItem)
          Adds an annotation_item to the Constant Pool.
 void addAnnotationSetItemNoSizeCheck(AnnotationSetItem annotationSetItem)
          Adds an annotation_set_item to the Constant Pool.
 void addAnnotationSetItemToConstantPool(AnnotationSetItem annotationSetItem)
          Adds an annotation_set_item to the Constant Pool.
 void addAnnotationSetRefListToConstantPool(AnnotationSetRefList annotationSetRefList)
          Adds an annotation_set_ref_list to the Constant Pool.
 ClassDefinitionItem addClassToConstantPool(java.lang.String className, java.lang.String superName, int access, java.lang.String[] interfaceNames, java.lang.String[] signature)
          Adds a class to the constant pool.
 Field addFieldToConstantPool(java.lang.String fieldName, java.lang.String desc, java.lang.String classOwningName, int access, java.lang.String[] signature, java.lang.Object value)
          Adds a Field in the Constant Pool.
 Method addMethodToConstantPool(java.lang.String methodName, java.lang.String className, java.lang.String methodDescriptor, int access, java.lang.String[] signature, java.lang.String[] exceptionNames)
          Adds a Method to the Constant Pool being built.
 void addOffsetForStaticValuesEncodedArrayItemOfClass(ClassDefinitionItem cdi, int offset)
          Adds an offset for the encoded_array_item of a static_values_offset for one Class.
 void addPrototypeToConstantPool(Prototype prototype)
          Adds a Prototype to the Constant Pool.
 Prototype addPrototypeToConstantPool(java.lang.String descriptor)
          Creates a Prototype from the descriptor and adds it to the Constant Pool, as well as the Strings inside.
 void addStringsToConstantPool(java.lang.String[] strings)
          Adds Strings to the Constant pool.
 void addStringToConstantPool(java.lang.String string)
          Adds a String to the Constant Pool.
 void addTypeListToConstantPool(TypeList typeList)
          Adds a TypeList to the Constant Pool, as well as the Strings inside.
 void addTypesToConstantPool(java.lang.String[] types)
          Adds Types to the Constant Pool.
 void addTypeToConstantPool(java.lang.String type)
          Adds a Type to the Constant Pool.
 int getAnnotationDirectoryItemCount()
          Returns the count of the annotation_directory_item.
 int getAnnotationDirectoryItemOffset(AnnotationDirectoryItem annotationDirectoryItem)
          Returns the offset of an annotation_directory_item, or 0 if the Class doesn't have an annotation_directory_item.
 java.util.HashSet<AnnotationDirectoryItem> getAnnotationDirectoryItems()
          Returns the annotation_directory_items, unique but non-ordered.
 int getAnnotationItemCount()
          Returns the count of annotation_items.
 int getAnnotationItemOffset(AnnotationItem annotationItem)
          Returns the offset of an annotation_item.
 java.util.TreeSet<AnnotationItem> getAnnotationItems()
          Returns the Set of annotation_items.
 int getAnnotationSetItemCount()
          Returns the count of annotation_set_items.
 int getAnnotationSetItemOffset(AnnotationSetItem annotationSetItem)
          Returns the offset of an annotation_set_item.
 java.util.TreeSet<AnnotationSetItem> getAnnotationSetItems()
          Returns the set of annotation_set_item.
 int getAnnotationSetRefListOffset(AnnotationSetRefList annotationSetRefList)
          Returns the offset of an annotation_set_ref_list.
 java.util.HashSet<AnnotationSetRefList> getAnnotationSetRefLists()
          Returns the set of annotation_set_ref_list.
 int getAnnotationSetRefListsCount()
          Returns the count of annotation_set_ref_lists.
 int getClassDefinitionCount()
          Returns the number of Classes currently parsed.
 java.util.ArrayList<ClassDefinitionItem> getClasses()
          Returns an ordered list of the classes of the Application.
 int getFieldCount()
          Returns the number of Fields currently parsed.
 int getFieldIndex(Field field)
          Returns the Index of a Field.
 java.util.TreeSet<Field> getFields()
          Returns the ordered Fields of the Application.
 int getMethodCount()
          Returns the number of Methods currently parsed.
 int getMethodIndex(Method method)
          Returns the Index of a Method.
 java.util.TreeSet<Method> getMethods()
          Returns the ordered Methods of the Application.
 int getOffsetOfStaticValuesEncodedArrayItemOfClass(ClassDefinitionItem cdi)
          Returns the offset of the encoded_array_item of a static_values_offset for one Class if it exists.
 int getPrototypeCount()
          Returns the number of Prototypes currently parsed.
 int getPrototypeIndex(Prototype prototype)
          Returns the Index of a Prototype.
 java.util.TreeSet<Prototype> getPrototypes()
          Returns the ordered Prototypes of the Application.
 int getResolvedFieldIndexFromSymbolicFieldIndex(int fieldIndex)
          Returns the resolved Field index from the symbolic index.
 int getResolvedMethodIndexFromSymbolicMethodIndex(int methodIndex)
          Returns the resolved method index from the symbolic index.
 int getResolvedStringIndexFromSymbolicStringIndex(int stringIndex)
          Returns the resolved String index from the symbolic index.
 int getResolvedTypeIndexFromSymbolicTypeIndex(int typeIndex)
          Returns the resolved Type index from the symbolic index.
 int getStringCount()
          Returns the number of Strings currently parsed.
 int getStringIndex(java.lang.String string)
          Returns the index of a String.
 java.util.TreeSet<java.lang.String> getStrings()
          Returns the ordered Strings of the Application.
 int getTypeCount()
          Returns the number of Types currently parsed.
 int getTypeIndex(java.lang.String type)
          Returns the Index of a Type.
 java.util.TreeSet<TypeList> getTypeList()
          Returns the ordered TypeLists of the Application.
 int getTypeListCount()
          Returns the number of TypeList currently parsed.
 int getTypeListOffset(TypeList typeList)
          Returns the Offset of a TypeList.
 java.util.TreeSet<java.lang.String> getTypes()
          Returns the ordered Types of the Application.
 void prepareGeneration()
          Prepares the various elements to be generated.
 void setAnnotationDirectoryItemOffset(AnnotationDirectoryItem annotationDirectoryItem, int offset)
          Sets the offset of an annotation_directory_item.
 void setAnnotationItemOffset(AnnotationItem annotationItem, int offset)
          Sets the offset of an annotation_item.
 void setAnnotationSetItemOffset(AnnotationSetItem annotationSetItem, int offset)
          Sets the offset of an annotation_set_item.
 void setAnnotationSetRefListOffset(AnnotationSetRefList annotationSetRefList, int offset)
          Sets the offset of an annotation_set_ref_list.
 void setTypeListOffset(TypeList typeList, int offset)
          Sets an Offset to a TypeList.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConstantPool

public ConstantPool()
Method Detail

getStringCount

public int getStringCount()
Returns the number of Strings currently parsed.

Returns:
the number of Strings currently parsed.

getTypeCount

public int getTypeCount()
Returns the number of Types currently parsed.

Returns:
the number of Types currently parsed.

getPrototypeCount

public int getPrototypeCount()
Returns the number of Prototypes currently parsed.

Returns:
the number of Prototypes currently parsed.

getFieldCount

public int getFieldCount()
Returns the number of Fields currently parsed.

Returns:
the number of Fields currently parsed.

getMethodCount

public int getMethodCount()
Returns the number of Methods currently parsed.

Returns:
the number of Methods currently parsed.

getClassDefinitionCount

public int getClassDefinitionCount()
Returns the number of Classes currently parsed.

Returns:
the number of Classes currently parsed.

getTypeListCount

public int getTypeListCount()
Returns the number of TypeList currently parsed.

Returns:
the number of TypeList currently parsed.

getStrings

public java.util.TreeSet<java.lang.String> getStrings()
Returns the ordered Strings of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered Strings of the Application.

getTypes

public java.util.TreeSet<java.lang.String> getTypes()
Returns the ordered Types of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered Types of the Application.

getFields

public java.util.TreeSet<Field> getFields()
Returns the ordered Fields of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered Fields of the Application.

getPrototypes

public java.util.TreeSet<Prototype> getPrototypes()
Returns the ordered Prototypes of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered Prototypes of the Application.

getMethods

public java.util.TreeSet<Method> getMethods()
Returns the ordered Methods of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered Methods of the Application.

getTypeList

public java.util.TreeSet<TypeList> getTypeList()
Returns the ordered TypeLists of the Application. It is only set when the Application has been fully parsed.

Returns:
the ordered TypeLists of the Application.

getStringIndex

public int getStringIndex(java.lang.String string)
Returns the index of a String. The Symbolic index is used as long as the application hasn't been fully parsed.

Parameters:
string - the String we want the index of.
Returns:
the index of the String.

getTypeListOffset

public int getTypeListOffset(TypeList typeList)
Returns the Offset of a TypeList. The Dex file must have been fully parsed before.

Parameters:
typeList - the TypeList to find.
Returns:
the offset of the TypeList, from the beginning of the file.

setTypeListOffset

public void setTypeListOffset(TypeList typeList,
                              int offset)
Sets an Offset to a TypeList.

Parameters:
typeList - the TypeList to give the Offset.
offset - the Offset to set.

getTypeIndex

public int getTypeIndex(java.lang.String type)
Returns the Index of a Type. The Symbolic index is used as long as the application hasn't been fully parsed.

Parameters:
type - name of the Type.
Returns:
the index of the Type.

getPrototypeIndex

public int getPrototypeIndex(Prototype prototype)
Returns the Index of a Prototype. The Dex file must have been fully parsed before.

Parameters:
prototype - the Prototype.
Returns:
the index of the Prototype.

getFieldIndex

public int getFieldIndex(Field field)
Returns the Index of a Field. The Symbolic index is used as long as the application hasn't been fully parsed.

Parameters:
field - the Field.
Returns:
the index of the Field.

getMethodIndex

public int getMethodIndex(Method method)
Returns the Index of a Method. The Symbolic index is used as long as the application hasn't been fully parsed.

Parameters:
method - the Method.
Returns:
the index of the Method.

getAnnotationItemOffset

public int getAnnotationItemOffset(AnnotationItem annotationItem)
Returns the offset of an annotation_item.

Parameters:
annotationItem - the offset of the annotation_item.
Returns:
the offset of the annotation_item.

setAnnotationItemOffset

public void setAnnotationItemOffset(AnnotationItem annotationItem,
                                    int offset)
Sets the offset of an annotation_item.

Parameters:
annotationItem - the annotation_item.
offset - the Offset of the annotation_item.

getAnnotationSetItemOffset

public int getAnnotationSetItemOffset(AnnotationSetItem annotationSetItem)
Returns the offset of an annotation_set_item.

Parameters:
annotationSetItem - the annotation_set_item.
Returns:
the offset of the annotation_set_item.

setAnnotationSetItemOffset

public void setAnnotationSetItemOffset(AnnotationSetItem annotationSetItem,
                                       int offset)
Sets the offset of an annotation_set_item.

Parameters:
annotationSetItem - the annotation_set_item.
offset - the offset of the annotation_set_item.

getAnnotationSetRefListOffset

public int getAnnotationSetRefListOffset(AnnotationSetRefList annotationSetRefList)
Returns the offset of an annotation_set_ref_list.

Parameters:
annotationSetRefList - the annotation_set_ref_list.
Returns:
offset the offset of the annotation_set_ref_list.

setAnnotationSetRefListOffset

public void setAnnotationSetRefListOffset(AnnotationSetRefList annotationSetRefList,
                                          int offset)
Sets the offset of an annotation_set_ref_list.

Parameters:
annotationSetRefList - the annotation_set_ref_list.
offset - the offset of the annotation_set_ref_list.

getAnnotationDirectoryItemOffset

public int getAnnotationDirectoryItemOffset(AnnotationDirectoryItem annotationDirectoryItem)
Returns the offset of an annotation_directory_item, or 0 if the Class doesn't have an annotation_directory_item.

Parameters:
annotationDirectoryItem - the annotation_directory_item.
Returns:
the offset of the annotation_directory_item, or 0.

setAnnotationDirectoryItemOffset

public void setAnnotationDirectoryItemOffset(AnnotationDirectoryItem annotationDirectoryItem,
                                             int offset)
Sets the offset of an annotation_directory_item.

Parameters:
annotationDirectoryItem - the annotation_directory_item belongs to.
offset - the offset of the annotation_directory_item.

getAnnotationItems

public java.util.TreeSet<AnnotationItem> getAnnotationItems()
Returns the Set of annotation_items.

Returns:
the Set of annotation_items.

getAnnotationSetItems

public java.util.TreeSet<AnnotationSetItem> getAnnotationSetItems()
Returns the set of annotation_set_item.

Returns:
the set of annotation_set_item.

getAnnotationSetRefLists

public java.util.HashSet<AnnotationSetRefList> getAnnotationSetRefLists()
Returns the set of annotation_set_ref_list.

Returns:
the set of annotation_set_ref_list.

getAnnotationItemCount

public int getAnnotationItemCount()
Returns the count of annotation_items.

Returns:
the count of annotation_items.

getAnnotationSetItemCount

public int getAnnotationSetItemCount()
Returns the count of annotation_set_items.

Returns:
the count of annotation_set_items.

getAnnotationSetRefListsCount

public int getAnnotationSetRefListsCount()
Returns the count of annotation_set_ref_lists.

Returns:
the count of annotation_set_ref_lists.

getAnnotationDirectoryItemCount

public int getAnnotationDirectoryItemCount()
Returns the count of the annotation_directory_item.

Returns:
the count of the annotation_directory_item.

getOffsetOfStaticValuesEncodedArrayItemOfClass

public int getOffsetOfStaticValuesEncodedArrayItemOfClass(ClassDefinitionItem cdi)
Returns the offset of the encoded_array_item of a static_values_offset for one Class if it exists.

Parameters:
cdi - the Class.
Returns:
the offset of the encoded_array_item of a static_values_offset for one Class, or 0 if no offset has been found.

getClasses

public java.util.ArrayList<ClassDefinitionItem> getClasses()
Returns an ordered list of the classes of the Application. It is only completed when the Application has been fully parsed.

Returns:
an ordered list of the classes of the Application.

getAnnotationDirectoryItems

public java.util.HashSet<AnnotationDirectoryItem> getAnnotationDirectoryItems()
Returns the annotation_directory_items, unique but non-ordered.

Returns:
the annotation_directory_items.

getResolvedMethodIndexFromSymbolicMethodIndex

public int getResolvedMethodIndexFromSymbolicMethodIndex(int methodIndex)
Returns the resolved method index from the symbolic index. The application must have been fully parsed for the conversion to be possible.

Parameters:
methodIndex - the symbolic method index.
Returns:
the resolved method index.

getResolvedFieldIndexFromSymbolicFieldIndex

public int getResolvedFieldIndexFromSymbolicFieldIndex(int fieldIndex)
Returns the resolved Field index from the symbolic index. The application must have been fully parsed for the conversion to be possible.

Parameters:
fieldIndex - the symbolic Field index.
Returns:
the resolved Field index.

getResolvedTypeIndexFromSymbolicTypeIndex

public int getResolvedTypeIndexFromSymbolicTypeIndex(int typeIndex)
Returns the resolved Type index from the symbolic index. The application must have been fully parsed for the conversion to be possible.

Parameters:
typeIndex - the symbolic Type index.
Returns:
the resolved Type index.

getResolvedStringIndexFromSymbolicStringIndex

public int getResolvedStringIndexFromSymbolicStringIndex(int stringIndex)
Returns the resolved String index from the symbolic index. The application must have been fully parsed for the conversion to be possible.

Parameters:
stringIndex - the symbolic String index.
Returns:
the resolved String index.

addStringToConstantPool

public void addStringToConstantPool(java.lang.String string)
Adds a String to the Constant Pool. Nothing happens if the String already exists or is null. Empty strings are authorized.

Parameters:
string - the String to add.

addStringsToConstantPool

public void addStringsToConstantPool(java.lang.String[] strings)
Adds Strings to the Constant pool. Nothing happens if the String already exists or is null. Empty strings are authorized.

Parameters:
strings - the Strings to add.

addTypeToConstantPool

public void addTypeToConstantPool(java.lang.String type)
Adds a Type to the Constant Pool. Nothing happens if the Type already exists, is null or empty. The Type is added to both the Types and Strings sets.

Parameters:
type - the Type to add.

addTypesToConstantPool

public void addTypesToConstantPool(java.lang.String[] types)
Adds Types to the Constant Pool. Nothing happens if the Types already exists, are null or empty. The Type is added to both the Types and Strings sets.

Parameters:
types - the Types to add.

addPrototypeToConstantPool

public void addPrototypeToConstantPool(Prototype prototype)
Adds a Prototype to the Constant Pool. Adds the Strings inside, but also the Prototype to the Prototype Pool.

Parameters:
prototype - Prototype to add.

addPrototypeToConstantPool

public Prototype addPrototypeToConstantPool(java.lang.String descriptor)
Creates a Prototype from the descriptor and adds it to the Constant Pool, as well as the Strings inside. Returns the Prototype created.

Parameters:
descriptor - full descriptor in the TypeDescriptor format.
Returns:
the Prototype created.

addTypeListToConstantPool

public void addTypeListToConstantPool(TypeList typeList)
Adds a TypeList to the Constant Pool, as well as the Strings inside. If the TypeList is empty, it is not added.

Parameters:
typeList - the TypeList to add to the Constant Pool.

addMethodToConstantPool

public Method addMethodToConstantPool(java.lang.String methodName,
                                      java.lang.String className,
                                      java.lang.String methodDescriptor,
                                      int access,
                                      java.lang.String[] signature,
                                      java.lang.String[] exceptionNames)
Adds a Method to the Constant Pool being built. Does nothing if the Constant Pool already contains a similar item unless the stored Method was tagged Unknown, in which case it means that it was just referred by an instruction, but not actually visited, and so lacks information. This Method can be called when declaring a Method without knowing what is inside, for example when a reference to this Method is found. In that case, the information are added. The method name, descriptor, and class name are added to the constant pool if necessary. In return is received the Method related to the given parameters.

Parameters:
methodName - the method name.
className - the class name of the method.
methodDescriptor - the method descriptor.
access - the access flags of the method.
signature - the signature of the method. May be Null.
exceptionNames - the exceptions of the method. May be null.
Returns:
the Method related to the given parameters.

addFieldToConstantPool

public Field addFieldToConstantPool(java.lang.String fieldName,
                                    java.lang.String desc,
                                    java.lang.String classOwningName,
                                    int access,
                                    java.lang.String[] signature,
                                    java.lang.Object value)
Adds a Field in the Constant Pool. Does nothing if the constant pool already contains a similar item. The field name, type, and class owning name are added to the constant pool. In return is received the Field related to the given parameters. This method can be called when declaring a Field without knowing what is inside, for example when a reference to this Field is found. In that case, the Field access should be ACC_UNKNOWN.

Parameters:
fieldName - name of the Field
desc - type of the field in TypeDescriptor format.
classOwningName - name of the class owning the Field.
access - the access flags of the Field.
signature - the Signature of the field. May be Null.
value - the value of the Field. Null for non-final static Fields.
Returns:
the Field related to the given parameters.

addClassToConstantPool

public ClassDefinitionItem addClassToConstantPool(java.lang.String className,
                                                  java.lang.String superName,
                                                  int access,
                                                  java.lang.String[] interfaceNames,
                                                  java.lang.String[] signature)
Adds a class to the constant pool. Nothing happens if the Class already exists. The class name, superClass name, signature and interfaces name are added to the constant pool. In return is received the ClassDefinitionItem related to the class.

Parameters:
className - the name of the class.
superName - the name of the super class.
access - the access flags of the class.
interfaceNames - the name of the interfaces of the class.
signature - the signature of the class. Used only for generics. May be null.
Returns:
the ClassDefinitionItem related to the class.

addAnnotationItemToConstantPool

public void addAnnotationItemToConstantPool(AnnotationItem annotationItem)
Adds an annotation_item to the Constant Pool. It must have its full content known.

Parameters:
annotationItem - the annotation_item to add.

addAnnotationSetItemToConstantPool

public void addAnnotationSetItemToConstantPool(AnnotationSetItem annotationSetItem)
Adds an annotation_set_item to the Constant Pool. It must have its full content known. Doesn't add the annotation_set_item if it doesn't contain any annotation_item.

Parameters:
annotationSetItem - the annotation_set_item to add.

addAnnotationSetItemNoSizeCheck

public void addAnnotationSetItemNoSizeCheck(AnnotationSetItem annotationSetItem)
Adds an annotation_set_item to the Constant Pool. It must have its full content known. Even annotation_set_item with a size of 0 are added. This is especially useful for the Annotated Parameter, as annotation_set_ref_list will probably contain empty annotation_set_items.

Parameters:
annotationSetItem - the annotation_set_item to add.

addAnnotationSetRefListToConstantPool

public void addAnnotationSetRefListToConstantPool(AnnotationSetRefList annotationSetRefList)
Adds an annotation_set_ref_list to the Constant Pool. It also registers all the annotation_set_items it contains to the Constant Pool. It must have its full content known.

Parameters:
annotationSetRefList - the annotation_set_ref_list to add.

addOffsetForStaticValuesEncodedArrayItemOfClass

public void addOffsetForStaticValuesEncodedArrayItemOfClass(ClassDefinitionItem cdi,
                                                            int offset)
Adds an offset for the encoded_array_item of a static_values_offset for one Class.

Parameters:
cdi - the class.
offset - the offset to add.

addAnnotationDirectoryItem

public void addAnnotationDirectoryItem(AnnotationDirectoryItem annotationDirectoryItem)
Adds an annotation_directory_item to the Constant Pool.

Parameters:
annotationDirectoryItem - the annotation_directory_item to add.

prepareGeneration

public void prepareGeneration()
Prepares the various elements to be generated. This must be done only once, after the whole Application has been parsed. It consists in generating the sorted collections, and building the Index Maps.