org.ow2.asmdex.structureWriter
Class Method

java.lang.Object
  extended by org.ow2.asmdex.structureWriter.Method
All Implemented Interfaces:
java.lang.Comparable<Method>, IAnnotationsHolder

public class Method
extends java.lang.Object
implements java.lang.Comparable<Method>, IAnnotationsHolder

Simple class representing a Method. It's a mix between the method_id_item, but also encoded_method structures. The equals and hashCode methods have been overridden in order to detect easily duplicates in HashMaps. A Method is identified by its methodName, className and prototype. Access and ExceptionIndexes are NOT taken in account to differentiate the methods. Access may be ACC_UNKNOWN if the method created is referenced, but not yet parsed (like when an instruction refers to a not yet parsed method). A Method has a code_item. However, it may not if the method is abstract or interface.

Author:
Julien Névo

Constructor Summary
Method(java.lang.String methodName, java.lang.String className, Prototype methodDescriptor)
           
 
Method Summary
 void addAnnotationItem(AnnotationItem annotationItem)
          Adds an annotation_item to the annotations_set_items.
 void addLocalVariable(LocalVariable localVariable)
          Adds a Local Variable to this Method.
 void addParameterAnnotationItem(int parameterIndex, AnnotationItem annotationItem)
          Adds an annotation_item to a parameter of this Method.
static int calculateHashCode(java.lang.String methodName, java.lang.String className, int prototypeHashCode)
          Calculates the hashcode of the Method according to its given identifiers.
static int calculateHashCode(java.lang.String methodName, java.lang.String className, Prototype prototype)
          Calculates the hashcode of the Method according to its given identifiers.
 void closeAnnotations(ConstantPool constantPool)
          Closes and registers the annotation_set_items and annotation_set_ref_items of this Method.
 int compareTo(Method method)
           
 void completeInformation(int access, java.lang.String[] exceptionNames, java.lang.String[] signature, ConstantPool constantPool)
          Adds information to the current Method.
 boolean equals(java.lang.Object obj)
           
 void free()
          Frees all the structures (list of instructions...) of the code_item and the debug_info_item so that they don't consume memory.
 void generateCodeItemCode()
          Generates the Instructions code, as Dalvik bytecode, in the codeItem buffer, as well as its header.
 int getAccess()
          Returns the access flags.
 AnnotationSetRefList getAnnotatedParameterSetRefList()
          Return the annotation_set_ref_list of this Method, indicating what are the annotations on each parameters.
 AnnotationSetItem getAnnotationSetItem()
          Returns the annotation_set_item this structure currently contains.
 java.lang.String getClassName()
          Returns the name of the Class owning the Method.
 CodeItem getCodeItem()
          Returns the Code Item linked to this Method.
 ByteVector getCodeItemCode()
          Returns the code_item code (including the code_item header and the bytecode), without the try/catch fields after the insns field.
 ByteVector getCodeItemTryCatch()
          Returns the try/catch section of the code_item, beginning by the possible padding after the insns field, or Null if no try/catch is present.
 java.lang.String[] getExceptionNames()
          Returns the exception names.
 java.util.List<LocalVariable> getLocalVariables()
          Returns the Local Variable list.
 java.lang.String getMethodName()
          Returns the name of the Method.
 int getNbAnnotations()
          Returns the number of annotation_items this structure currently contains.
 int getNbParameterAnnotations()
          Returns the number of annotation_set_items the Method has.
 int getNbParameters()
          Returns the number of parameters of this Method (doesn't count the Return parameter).
 java.lang.String[] getParameters()
          Returns the name of the parameters of this Method (excluding the Return parameter).
 Prototype getPrototype()
          Returns the prototype of the Method, in TypeDescriptor format.
 java.lang.String[] getSignature()
          Returns the Signature of the Method.
 int getStartBytecodeToCopy()
          Gets the start in byte of the bytecode to copy from the input Dex file.
 int getStartDebugInfoToCopy()
          Gets the start in byte of the debug_info_item to copy from the input Dex file.
 int hashCode()
           
 void init(int access, java.lang.String[] signature, java.lang.String[] exceptionNames, ConstantPool constantPool)
          Full init if this is the witness kept.
 boolean isAbstractOrInterface()
          Indicates whether the Method is abstract or interface.
 boolean isDirect()
          Indicates whether the Method is direct (static and/or private and/or constructor).
 boolean isStatic()
          Indicates whether the Method is static.
 boolean isUnknown()
          Indicates whether the Method is unknown (referred to, but not yet parsed).
 boolean isUsingThis()
          Indicates whether the Method uses the "this" parameter.
 boolean isVirtual()
          Indicates whether the Method is virtual (not static, private, or constructor).
 void setFirstLineNumberIfNeeded(int firstLineNumber)
          Sets the first line number of the Method, but only if none has been found before.
 void setParameters(java.lang.String[] parameters)
          Sets the name of the parameters used by this Method.
 void setStartBytecodeToCopy(int start)
          Sets the start of the bytecode to copy from the input Dex file to the output.
 void setStartDebugInfoToCopy(int start)
          Sets the start of the debug_info_item to copy from the input Dex file to the output.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Method

public Method(java.lang.String methodName,
              java.lang.String className,
              Prototype methodDescriptor)
Parameters:
methodName - name of the Method.
className - name of the Class owning the Method.
methodDescriptor - Prototype of the Method, in TypeDescriptor format.
Method Detail

init

public void init(int access,
                 java.lang.String[] signature,
                 java.lang.String[] exceptionNames,
                 ConstantPool constantPool)
Full init if this is the witness kept.

Parameters:
access - The access flags of the Field.
signature - the signature of the method. May be Null.
exceptionNames - the names of the exceptions. May be Null.
constantPool - the Constant Pool of the Application.

addLocalVariable

public void addLocalVariable(LocalVariable localVariable)
Adds a Local Variable to this Method.


completeInformation

public void completeInformation(int access,
                                java.lang.String[] exceptionNames,
                                java.lang.String[] signature,
                                ConstantPool constantPool)
Adds information to the current Method. This is useful only if this Method has the ACC_UNKNOWN flag, which means Instructions referred to it while it has not yet been visited, declaring it but not giving all the information it should have.


calculateHashCode

public static int calculateHashCode(java.lang.String methodName,
                                    java.lang.String className,
                                    Prototype prototype)
Calculates the hashcode of the Method according to its given identifiers.

Parameters:
methodName - the name of the Method.
className - the Class it belongs to.
prototype - the Prototype of the Method.
Returns:
the hashcode of the Method.

calculateHashCode

public static int calculateHashCode(java.lang.String methodName,
                                    java.lang.String className,
                                    int prototypeHashCode)
Calculates the hashcode of the Method according to its given identifiers.

Parameters:
methodName - the name of the Method.
className - the Class it belongs to.
prototypeHashCode - the hashcode of the Prototype.
Returns:
the hashcode of the Method.

isVirtual

public boolean isVirtual()
Indicates whether the Method is virtual (not static, private, or constructor).

Returns:
true if the Method is virtual.

isDirect

public boolean isDirect()
Indicates whether the Method is direct (static and/or private and/or constructor).

Returns:
true if the Method is direct.

isStatic

public boolean isStatic()
Indicates whether the Method is static.

Returns:
true if the Method is static.

isAbstractOrInterface

public boolean isAbstractOrInterface()
Indicates whether the Method is abstract or interface.

Returns:
true if the Method is abstract or interface.

isUnknown

public boolean isUnknown()
Indicates whether the Method is unknown (referred to, but not yet parsed).

Returns:
true if the Method is unknown.

isUsingThis

public boolean isUsingThis()
Indicates whether the Method uses the "this" parameter.

Returns:
true if the Method uses the "this" parameter.

addAnnotationItem

public void addAnnotationItem(AnnotationItem annotationItem)
Adds an annotation_item to the annotations_set_items.

Parameters:
annotationItem - the Annotation Item to add.

addParameterAnnotationItem

public void addParameterAnnotationItem(int parameterIndex,
                                       AnnotationItem annotationItem)
Adds an annotation_item to a parameter of this Method.

Parameters:
parameterIndex - zero-based index of the argument.
annotationItem - the Annotation Item to add.

closeAnnotations

public void closeAnnotations(ConstantPool constantPool)
Closes and registers the annotation_set_items and annotation_set_ref_items of this Method. This must only be done once when the Method has been fully visited.

Parameters:
constantPool - the Constant Pool of the Application.

generateCodeItemCode

public void generateCodeItemCode()
Generates the Instructions code, as Dalvik bytecode, in the codeItem buffer, as well as its header. It uses symbolic references, so must be parsed again to link them to the "real" elements. Also fills the debug_code_item in this instance. Note however that the debug_info_offset field in the code_item header is not set, because we don't know where the debug_info_item is encoded for now. Note that the debug_info_item is not written here, only built. The alignment is not managed here, but must be by the calling method.


free

public void free()
Frees all the structures (list of instructions...) of the code_item and the debug_info_item so that they don't consume memory. This MUST be done after having generated the bytecode, once the method has been parsed and its end visited.


getCodeItemCode

public ByteVector getCodeItemCode()
Returns the code_item code (including the code_item header and the bytecode), without the try/catch fields after the insns field. It uses symbolic references, so must be parsed again to link them to the "real" elements.

Returns:
the code_item code, without the try/catch fields.

getCodeItemTryCatch

public ByteVector getCodeItemTryCatch()
Returns the try/catch section of the code_item, beginning by the possible padding after the insns field, or Null if no try/catch is present. It uses symbolic references, so must be parsed again to link them to the "real" elements.

Returns:
the try/catch code, or Null.

setStartBytecodeToCopy

public void setStartBytecodeToCopy(int start)
Sets the start of the bytecode to copy from the input Dex file to the output. This is only useful when using the optimization that consists in copying part of the Constant Pool and the bytecode of methods that doesn't change, if the Reader is linked to the Writer with no Adapter to modify the methods in between.

Parameters:
start - start in bytes from the beginning of the Dex file where the bytecode is. This includes the code_item header.

getStartBytecodeToCopy

public int getStartBytecodeToCopy()
Gets the start in byte of the bytecode to copy from the input Dex file. This is only useful when using the optimization that consists in copying part of the Constant Pool and the bytecode of methods that doesn't change, if the Reader is linked to the Writer with no Adapter to modify the methods in between.

Returns:
the start in byte of the bytecode, or 0 if the optimization is not used.

setStartDebugInfoToCopy

public void setStartDebugInfoToCopy(int start)
Sets the start of the debug_info_item to copy from the input Dex file to the output. This is only useful when using the optimization that consists in copying part of the Constant Pool and the bytecode of methods that doesn't change, if the Reader is linked to the Writer with no Adapter to modify the methods in between.

Parameters:
start - start in bytes from the beginning of the Dex file where the debug_info_item is.

getStartDebugInfoToCopy

public int getStartDebugInfoToCopy()
Gets the start in byte of the debug_info_item to copy from the input Dex file. This is only useful when using the optimization that consists in copying part of the Constant Pool and the bytecode of methods that doesn't change, if the Reader is linked to the Writer with no Adapter to modify the methods in between.

Returns:
the start in byte of the debug_info_item, or 0 if the optimization is not used.

getClassName

public java.lang.String getClassName()
Returns the name of the Class owning the Method.

Returns:
the name of the Class owning the Method.

getPrototype

public Prototype getPrototype()
Returns the prototype of the Method, in TypeDescriptor format.

Returns:
the prototype of the Method, in TypeDescriptor format.

getMethodName

public java.lang.String getMethodName()
Returns the name of the Method.

Returns:
the name of the Method.

getAccess

public int getAccess()
Returns the access flags.

Returns:
the access flags.

getExceptionNames

public java.lang.String[] getExceptionNames()
Returns the exception names. May be Null.

Returns:
the exception names, or Null.

getCodeItem

public CodeItem getCodeItem()
Returns the Code Item linked to this Method. May be Null if it hasn't any (if abstract or interface).

Returns:
the Code Item linked to this Method or Null.

getAnnotationSetItem

public AnnotationSetItem getAnnotationSetItem()
Returns the annotation_set_item this structure currently contains.

Specified by:
getAnnotationSetItem in interface IAnnotationsHolder
Returns:
the annotation_set_item this structure currently contains.

getNbAnnotations

public int getNbAnnotations()
Returns the number of annotation_items this structure currently contains. Does not concern the parameter annotations.

Specified by:
getNbAnnotations in interface IAnnotationsHolder
Returns:
the number of annotation_items this structure currently contains.

getAnnotatedParameterSetRefList

public AnnotationSetRefList getAnnotatedParameterSetRefList()
Return the annotation_set_ref_list of this Method, indicating what are the annotations on each parameters.

Returns:
the annotation_set_ref_list of this Method.

getNbParameterAnnotations

public int getNbParameterAnnotations()
Returns the number of annotation_set_items the Method has. They are only used by the parameter annotations (one annotation_set_item per argument, even if this one in particular is not annotated).

Returns:
the number of annotation_set_items the Method has.

getNbParameters

public int getNbParameters()
Returns the number of parameters of this Method (doesn't count the Return parameter). May be 0.

Returns:
the number of parameters of this Method.

getParameters

public java.lang.String[] getParameters()
Returns the name of the parameters of this Method (excluding the Return parameter). May be null if the Debug Info didn't provide them or was not visited.

Returns:
the name of the parameters of this Method, or null.

setParameters

public void setParameters(java.lang.String[] parameters)
Sets the name of the parameters used by this Method.

Parameters:
parameters - name of the parameters.

setFirstLineNumberIfNeeded

public void setFirstLineNumberIfNeeded(int firstLineNumber)
Sets the first line number of the Method, but only if none has been found before.

Parameters:
firstLineNumber - the first line number of this Method.

getSignature

public java.lang.String[] getSignature()
Returns the Signature of the Method. May be Null.

Returns:
the Signature of the Method. May be Null.

getLocalVariables

public java.util.List<LocalVariable> getLocalVariables()
Returns the Local Variable list. It may be empty.

Returns:
the Local Variable list.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(Method method)
Specified by:
compareTo in interface java.lang.Comparable<Method>