org.ow2.asmdex
Class MethodWriter

java.lang.Object
  extended by org.ow2.asmdex.MethodVisitor
      extended by org.ow2.asmdex.MethodWriter

public class MethodWriter
extends MethodVisitor

Class that takes care of visiting all the elements of a method.

RegisterSize, Incoming Arguments Size, Outgoing Arguments Size
--------------------------------------------------------------
The RegisterSize should be calculated like that :

But we can't calculate it because we don't know if "this" or the parameters are used. So we have to rely on visitMaxs() to give us the RegisterSize.

The Incoming Argument Size : calculating it consists in parsing the descriptor, minus the return type, and counting how many bytes are allocated to each type (I = 2 (bytes), D = 4...). Add 2 for "this", unless it's a Static method.

The Outgoing Arguments Size : a bit the same. Each time a Method is called inside the current Method, calculates how many bytes are allocated to each type (I = 2 bytes, D = 4, L = 4...) in the descriptor, minus the return type. Add 2 for "this", unless the called method is Static.

Labels Management
-----------------

Author:
Julien Névo

Field Summary
 
Fields inherited from class org.ow2.asmdex.MethodVisitor
api, mv
 
Constructor Summary
MethodWriter(ClassWriter classWriter, int access, java.lang.String name, java.lang.String desc, java.lang.String[] signature, java.lang.String[] exceptions)
          Constructor of the Method Writer.
 
Method Summary
 void addInstruction(Instruction insn)
          Adds an instruction to the current Code Item.
 void addLabel(Label label)
          Adds a label to the set of used labels.
 int addPadding()
          Adds padding at the end of the file, using an alignment of 4, if necessary.
 void closeAnnotations()
          Closes and registers the annotation_set_items and annotation_set_ref_items of this Method.
 ClassWriter getClassWriter()
          Returns the ClassWriter of this MethodWriter.
 CodeItem getCodeItem()
          Returns the Code Item linked to this Writer.
 Method getMethod()
          Returns the Method linked to this Writer.
 void setStartBytecodeToCopy(int start)
          Sets the start of the bytecode to copy from the input Dex file to the output.
 AnnotationVisitor visitAnnotation(java.lang.String desc, boolean visible)
          Visits an annotation of this method.
 AnnotationVisitor visitAnnotationDefault()
          Visits the default value of this annotation interface method.
 void visitArrayLengthInsn(int destinationRegister, int arrayReferenceBearing)
          Visits an ARRAY-LENGTH instruction.
 void visitArrayOperationInsn(int opcode, int valueRegister, int arrayRegister, int indexRegister)
          Visits an instruction that performs an operation on an array.
 void visitAttribute(java.lang.Object attr)
          Visits a non standard attribute of this method.
 void visitCode()
          Starts the visit of the method's code, if any (i.e.
 void visitEnd()
          Visits the end of the method.
 void visitFieldInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String desc, int valueRegister, int objectRegister)
          Visits a field instruction.
 void visitFillArrayDataInsn(int arrayReference, java.lang.Object[] arrayData)
          Visits a Fill Array Data instruction.
 void visitFrame(int type, int nLocal, java.lang.Object[] local, int nStack, java.lang.Object[] stack)
          Visits the current state of the local variables and operand stack elements.
 void visitInsn(int opcode)
          Visits a zero operand instruction.
 void visitIntInsn(int opcode, int register)
          Visits an instruction with a single register.
 void visitJumpInsn(int opcode, Label label, int registerA, int registerB)
          Visits a jump operation, conditional or not.
 void visitLabel(Label label)
          Visits a label.
 void visitLineNumber(int line, Label start)
          Visits a line number declaration.
 void visitLocalVariable(java.lang.String name, java.lang.String desc, java.lang.String signature, Label start, Label end, int index)
          Visits a local variable declaration.
 void visitLocalVariable(java.lang.String name, java.lang.String desc, java.lang.String signature, Label start, java.util.List<Label> ends, java.util.List<Label> restarts, int index)
          Visits a local variable declaration.
 void visitLookupSwitchInsn(int register, Label dflt, int[] keys, Label[] labels)
          Visits a LOOKUPSWITCH instruction.
 void visitMaxs(int maxStack, int maxLocals)
          Visits the maximum stack size and the maximum number of local variables of the method.
 void visitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String desc, int[] arguments)
          Visits a method instruction.
 void visitMultiANewArrayInsn(java.lang.String desc, int[] registers)
          Visits a MULTIANEWARRAY instruction.
 void visitOperationInsn(int opcode, int destinationRegister, int firstSourceRegister, int secondSourceRegister, int value)
          Visits an instruction that performs an unary or binary operation on one or two sources or one array, possibly a literal value, to a destination register.
 AnnotationVisitor visitParameterAnnotation(int parameter, java.lang.String desc, boolean visible)
          Visits an annotation of a parameter this method.
 void visitParameters(java.lang.String[] parameters)
          Visits the parameters of the method, if any.
 void visitStringInsn(int opcode, int destinationRegister, java.lang.String string)
          Visits a string instruction.
 void visitTableSwitchInsn(int register, int min, int max, Label dflt, Label[] labels)
          Visits a TABLESWITCH instruction.
 void visitTryCatchBlock(Label start, Label end, Label handler, java.lang.String type)
          Visits a try catch block.
 void visitTypeInsn(int opcode, int destinationRegister, int referenceBearingRegister, int sizeRegister, java.lang.String type)
          Visits a type instruction.
 void visitVarInsn(int opcode, int destinationRegister, int var)
          Visits a local variable instruction.
 void visitVarInsn(int opcode, int destinationRegister, long var)
          Visits a local variable instruction, using a Long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodWriter

public MethodWriter(ClassWriter classWriter,
                    int access,
                    java.lang.String name,
                    java.lang.String desc,
                    java.lang.String[] signature,
                    java.lang.String[] exceptions)
Constructor of the Method Writer. Requires the data of the Method invocation.

Parameters:
classWriter - the classWriter of the method.
access - the access flags of the method.
name - the name of the method.
desc - the descriptor of the method.
signature - the signature of the method. May be Null.
exceptions - the exceptions of the method. May be Null.
Method Detail

getClassWriter

public ClassWriter getClassWriter()
Returns the ClassWriter of this MethodWriter.


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.

getMethod

public Method getMethod()
Returns the Method linked to this Writer.

Returns:
the Method linked to this Writer.

getCodeItem

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

Returns:
the Code Item linked to this Writer, or Null.

addLabel

public void addLabel(Label label)
Adds a label to the set of used labels. If Null, nothing is done.

Parameters:
label - the label to add.

addInstruction

public void addInstruction(Instruction insn)
Adds an instruction to the current Code Item. Also adds the Line Number to the Instruction, and resets it.

Parameters:
insn - instruction to add to the Code Item.

addPadding

public int addPadding()
Adds padding at the end of the file, using an alignment of 4, if necessary. The padding is done using a NOP Instruction.

Returns:
the padding added, in bytes.

visitCode

public void visitCode()
Description copied from class: MethodVisitor
Starts the visit of the method's code, if any (i.e. non abstract method).

Overrides:
visitCode in class MethodVisitor

visitEnd

public void visitEnd()
Description copied from class: MethodVisitor
Visits the end of the method. This method, which is the last one to be called, is used to inform the visitor that all the annotations and attributes of the method have been visited.

Overrides:
visitEnd in class MethodVisitor

visitMethodInsn

public void visitMethodInsn(int opcode,
                            java.lang.String owner,
                            java.lang.String name,
                            java.lang.String desc,
                            int[] arguments)
Description copied from class: MethodVisitor
Visits a method instruction. A method instruction is an instruction that invokes a method.

Overrides:
visitMethodInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either INVOKE-VIRTUAL, INVOKE-SUPER, INVOKE-DIRECT, INVOKE-STATIC or INVOKE-INTERFACE.
owner - the internal name of the method's owner class.
name - the method's name.
desc - the method's descriptor.
arguments - the arguments to give to the method.

visitParameters

public void visitParameters(java.lang.String[] parameters)
Description copied from class: MethodVisitor
Visits the parameters of the method, if any.

Overrides:
visitParameters in class MethodVisitor
Parameters:
parameters - a list of parameters.

visitVarInsn

public void visitVarInsn(int opcode,
                         int destinationRegister,
                         int var)
Description copied from class: MethodVisitor
Visits a local variable instruction. A local variable instruction is an instruction that loads or stores the value of a local variable.

Overrides:
visitVarInsn in class MethodVisitor
Parameters:
opcode - the opcode of the local variable instruction to be visited. This opcode is either MOVE, MOVE/FROM16, MOVE/16, MOVE-WIDE, MOVE-WIDE/FROM16, MOVE-WIDE/16, MOVE-OBJECT, MOVE-OBJECT/FROM16, MOVE-OBJECT/16, CONST/4, CONST/16, CONST, CONST/HIGH16, CONST-WIDE/16, CONST-WIDE/32.
destinationRegister - the destination register.
var - the operand of the instruction to be visited. This operand is either a value or a source Register.

visitVarInsn

public void visitVarInsn(int opcode,
                         int destinationRegister,
                         long var)
Description copied from class: MethodVisitor
Visits a local variable instruction, using a Long. A local variable instruction is an instruction that loads or stores the value of a local variable.

Overrides:
visitVarInsn in class MethodVisitor
Parameters:
opcode - the opcode of the local variable instruction to be visited. This opcode is either CONST-WIDE, CONST-WIDE/HIGH16.
destinationRegister - the destination register.
var - the operand of the instruction to be visited. This operand is either a value or a source Register.

visitInsn

public void visitInsn(int opcode)
Description copied from class: MethodVisitor
Visits a zero operand instruction.

Overrides:
visitInsn in class MethodVisitor
Parameters:
opcode - the opcode of the instruction to be visited. This opcode is either NOP, RETURN-VOID, or unused.

visitLabel

public void visitLabel(Label label)
Description copied from class: MethodVisitor
Visits a label. A label designates the instruction that will be visited just after it.

Overrides:
visitLabel in class MethodVisitor
Parameters:
label - a Label object.

visitOperationInsn

public void visitOperationInsn(int opcode,
                               int destinationRegister,
                               int firstSourceRegister,
                               int secondSourceRegister,
                               int value)
Description copied from class: MethodVisitor
Visits an instruction that performs an unary or binary operation on one or two sources or one array, possibly a literal value, to a destination register.

Overrides:
visitOperationInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is in either of these categories : UNOP, BINOP, BINOP/2ADDR, BINOP/LIT16, BINOP/LIT8.
destinationRegister - the destination register or pair. For BINOP/2ADDR, it should be the same as firstSourceRegister.
firstSourceRegister - the first source register or pair. For BINOP/2ADDR, it should be the same as destinationRegister.
secondSourceRegister - the second source register or pair. Ignored for UNOP, BINOP/LIT16, BINOP/LIT8.
value - literal value. Only useful for BINOP/LIT16, BINOP/LIT8.

visitIntInsn

public void visitIntInsn(int opcode,
                         int register)
Description copied from class: MethodVisitor
Visits an instruction with a single register.

Overrides:
visitIntInsn in class MethodVisitor
Parameters:
opcode - the opcode of the instruction to be visited. This opcode is either RETURN, RETURN-WIDE, RETURN-OBJECT, MOVE-RESULT, MOVE-RESULT-WIDE, MOVE-RESULT-OBJECT, MOVE-EXCEPTION, THROW vAA.
register - the operand of the instruction to be visited.

visitJumpInsn

public void visitJumpInsn(int opcode,
                          Label label,
                          int registerA,
                          int registerB)
Description copied from class: MethodVisitor
Visits a jump operation, conditional or not. A jump instruction is an instruction that may jump to another instruction.

Overrides:
visitJumpInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either GOTO, IF-EQ, IF-NE, IF-LT, IF-GE, IF-GT, IF-LE, or IF-EQZ, IF-NEZ, IF-LTZ, IF-GEZ, IF-GTZ, IF-LEZ.
label - the operand of the instruction to be visited. This operand is a label that designates the instruction to which the jump instruction may jump.
registerA - (4 bits). Ignored for unconditional jump.
registerB - (4 bits). Ignored for unconditional jump or zero tests (IF-EQZ, IF-NEZ...)

visitFillArrayDataInsn

public void visitFillArrayDataInsn(int arrayReference,
                                   java.lang.Object[] arrayData)
Description copied from class: MethodVisitor
Visits a Fill Array Data instruction. It is only used for primitive areas. Reference areas are filled programmatically.

Overrides:
visitFillArrayDataInsn in class MethodVisitor
Parameters:
arrayReference - array reference.
arrayData - array of the primitives to encode.

visitTypeInsn

public void visitTypeInsn(int opcode,
                          int destinationRegister,
                          int referenceBearingRegister,
                          int sizeRegister,
                          java.lang.String type)
Description copied from class: MethodVisitor
Visits a type instruction. A type instruction is an instruction that takes the internal name of a class as parameter.

Overrides:
visitTypeInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either CONST-CLASS, NEW-ARRAY, NEW-INSTANCE, CHECK-CAST or INSTANCE-OF.
destinationRegister - the destination register. Used only by CONST-CLASS, INSTANCE-OF, NEW-INSTANCE, NEW-ARRAY.
referenceBearingRegister - reference bearing register. Only used for CHECK-CAST, INSTANCE-OF instructions.
sizeRegister - size register. Used only for NEW-ARRAY.
type - the operand of the instruction to be visited. This operand must be the internal name of an object or array class.

visitMultiANewArrayInsn

public void visitMultiANewArrayInsn(java.lang.String desc,
                                    int[] registers)
Description copied from class: MethodVisitor
Visits a MULTIANEWARRAY instruction.

Overrides:
visitMultiANewArrayInsn in class MethodVisitor
Parameters:
desc - an array type descriptor.
registers - array of registers containing the values of the array.

visitTableSwitchInsn

public void visitTableSwitchInsn(int register,
                                 int min,
                                 int max,
                                 Label dflt,
                                 Label[] labels)
Description copied from class: MethodVisitor
Visits a TABLESWITCH instruction.

Overrides:
visitTableSwitchInsn in class MethodVisitor
Parameters:
register - the register to test.
min - the minimum key value.
max - the maximum key value (ignored by the Writer).
dflt - beginning of the default handler block (ignored by the Writer).
labels - beginnings of the handler blocks. labels[i] is the beginning of the handler block for the min + i key.

visitLookupSwitchInsn

public void visitLookupSwitchInsn(int register,
                                  Label dflt,
                                  int[] keys,
                                  Label[] labels)
Description copied from class: MethodVisitor
Visits a LOOKUPSWITCH instruction.

Overrides:
visitLookupSwitchInsn in class MethodVisitor
Parameters:
register - the register to test.
dflt - beginning of the default handler block (ignored by the Writer).
keys - the values of the keys.
labels - beginnings of the handler blocks. labels[i] is the beginning of the handler block for the keys[i] key.

visitTryCatchBlock

public void visitTryCatchBlock(Label start,
                               Label end,
                               Label handler,
                               java.lang.String type)
Description copied from class: MethodVisitor
Visits a try catch block.

Overrides:
visitTryCatchBlock in class MethodVisitor
Parameters:
start - beginning of the exception handler's scope (inclusive).
end - end of the exception handler's scope (exclusive).
handler - beginning of the exception handler's code.
type - internal name of the type of exceptions handled by the handler, or null to catch any exceptions (for "finally" blocks).

visitAnnotation

public AnnotationVisitor visitAnnotation(java.lang.String desc,
                                         boolean visible)
Description copied from class: MethodVisitor
Visits an annotation of this method.

Overrides:
visitAnnotation in class MethodVisitor
Parameters:
desc - the class descriptor of the annotation class.
visible - true if the annotation is visible at runtime.
Returns:
a visitor to visit the annotation values, or null if this visitor is not interested in visiting this annotation.

visitParameterAnnotation

public AnnotationVisitor visitParameterAnnotation(int parameter,
                                                  java.lang.String desc,
                                                  boolean visible)
Description copied from class: MethodVisitor
Visits an annotation of a parameter this method.

Overrides:
visitParameterAnnotation in class MethodVisitor
Parameters:
parameter - the parameter index.
desc - the class descriptor of the annotation class.
visible - true if the annotation is visible at runtime.
Returns:
a visitor to visit the annotation values, or null if this visitor is not interested in visiting this annotation.

visitArrayLengthInsn

public void visitArrayLengthInsn(int destinationRegister,
                                 int arrayReferenceBearing)
Description copied from class: MethodVisitor
Visits an ARRAY-LENGTH instruction.

Overrides:
visitArrayLengthInsn in class MethodVisitor
Parameters:
destinationRegister - the destination register.
arrayReferenceBearing - register referencing the array.

visitArrayOperationInsn

public void visitArrayOperationInsn(int opcode,
                                    int valueRegister,
                                    int arrayRegister,
                                    int indexRegister)
Description copied from class: MethodVisitor
Visits an instruction that performs an operation on an array.

Overrides:
visitArrayOperationInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either AGET(-xxx) or APUT(-xxx).
valueRegister - the value register. May be source or destination register/pair.
arrayRegister - the array register.
indexRegister - the index register.

visitStringInsn

public void visitStringInsn(int opcode,
                            int destinationRegister,
                            java.lang.String string)
Description copied from class: MethodVisitor
Visits a string instruction. A string instruction is an instruction that takes the internal name of a string as parameter.

Overrides:
visitStringInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either CONST-STRING or CONST-STRING/JUMBO.
destinationRegister - the destination register.
string - the string of the instruction to be visited.

visitFieldInsn

public void visitFieldInsn(int opcode,
                           java.lang.String owner,
                           java.lang.String name,
                           java.lang.String desc,
                           int valueRegister,
                           int objectRegister)
Description copied from class: MethodVisitor
Visits a field instruction. A field instruction is an instruction that loads or stores the value of a field of an object.

Overrides:
visitFieldInsn in class MethodVisitor
Parameters:
opcode - the opcode of the type instruction to be visited. This opcode is either IinstanceOP (IGETxx/IPUTxx) and SstaticOP (SGETXX/IPUTxx).
owner - the internal name of the field's owner class.
name - the field's name.
desc - the field's descriptor.
valueRegister - the value register.
objectRegister - the objectRegister. Ignored for static operations.

visitLocalVariable

public void visitLocalVariable(java.lang.String name,
                               java.lang.String desc,
                               java.lang.String signature,
                               Label start,
                               Label end,
                               int index)
Description copied from class: MethodVisitor
Visits a local variable declaration.

Overrides:
visitLocalVariable in class MethodVisitor
Parameters:
name - the name of a local variable.
desc - the type descriptor of this local variable.
signature - the type signature of this local variable. May be null if the local variable type does not use generic types.
start - the first instruction corresponding to the scope of this local variable (inclusive).
end - the last instruction corresponding to the scope of this local variable (exclusive). May be null if we don't know it.
index - the local variable's index.

visitLocalVariable

public void visitLocalVariable(java.lang.String name,
                               java.lang.String desc,
                               java.lang.String signature,
                               Label start,
                               java.util.List<Label> ends,
                               java.util.List<Label> restarts,
                               int index)
Description copied from class: MethodVisitor
Visits a local variable declaration.

Overrides:
visitLocalVariable in class MethodVisitor
Parameters:
name - the name of a local variable.
desc - the type descriptor of this local variable.
signature - the type signature of this local variable. May be null if the local variable type does not use generic types.
start - the first instruction corresponding to the scope of this local variable (inclusive).
ends - the last instruction corresponding to the scope of this local variable (exclusive). May be null if we don't know it. As Variables can be restarted, we may have several ends.
restarts - the instruction where the local variable is restarted. May be null if it never happens. Variables can be restarted as many times as needed, hence the array.
index - the local variable's index.

visitLineNumber

public void visitLineNumber(int line,
                            Label start)
Description copied from class: MethodVisitor
Visits a line number declaration.

Overrides:
visitLineNumber in class MethodVisitor
Parameters:
line - a line number. This number refers to the source file from which the class was compiled.
start - the first instruction corresponding to this line number.

visitMaxs

public void visitMaxs(int maxStack,
                      int maxLocals)
Description copied from class: MethodVisitor
Visits the maximum stack size and the maximum number of local variables of the method.

Overrides:
visitMaxs in class MethodVisitor
Parameters:
maxStack - maximum stack size of the method.
maxLocals - ignored in AsmDex. Maximum number of local variables for the method.

visitAnnotationDefault

public AnnotationVisitor visitAnnotationDefault()
Description copied from class: MethodVisitor
Visits the default value of this annotation interface method.

Overrides:
visitAnnotationDefault in class MethodVisitor
Returns:
a visitor to the visit the actual default value of this annotation interface method, or null if this visitor is not interested in visiting this default value. The 'name' parameters passed to the methods of this annotation visitor are ignored. Moreover, exactly one visit method must be called on this annotation visitor, followed by visitEnd.

visitAttribute

public void visitAttribute(java.lang.Object attr)
Description copied from class: MethodVisitor
Visits a non standard attribute of this method. This visit is ignored as Attributes are not supported by AsmDex.

Overrides:
visitAttribute in class MethodVisitor
Parameters:
attr - an attribute.

visitFrame

public void visitFrame(int type,
                       int nLocal,
                       java.lang.Object[] local,
                       int nStack,
                       java.lang.Object[] stack)
Description copied from class: MethodVisitor
Visits the current state of the local variables and operand stack elements. Ignored by AsmDex. Only used for compatibility with ASM.

Overrides:
visitFrame in class MethodVisitor
Parameters:
type - the type of this stack map frame. Ignored by AsmDex.
nLocal - the number of local variables in the visited frame. Ignored by AsmDex.
local - the local variable types in this frame. Ignored by AsmDex.
nStack - the number of operand stack elements in the visited frame. Ignored by AsmDex.
stack - the operand stack types in this frame. Ignored by AsmDex.

closeAnnotations

public void closeAnnotations()
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.