org.unitils.mock.argumentmatcher
Class ArgumentMatcherPositionFinder.MethodInterpreter

java.lang.Object
  extended by org.objectweb.asm.tree.analysis.BasicInterpreter
      extended by org.unitils.mock.argumentmatcher.ArgumentMatcherPositionFinder.MethodInterpreter
All Implemented Interfaces:
org.objectweb.asm.Opcodes, org.objectweb.asm.tree.analysis.Interpreter
Enclosing class:
ArgumentMatcherPositionFinder

protected static class ArgumentMatcherPositionFinder.MethodInterpreter
extends org.objectweb.asm.tree.analysis.BasicInterpreter

Interpreter that implements the argument matcher finder behavior. The analyzer simulates the processing of instructions by the VM and calls methods on this class to determine the result of the processing of an instruction. During this processing, the analyzer simulates the maintenance of the operand stack. For example:

Suppose you have following statement: 1 + 2 The analyzer will first simulate the instruction to load constant 1 on the operand stack, then it does the same for constant 2, finally it simulates the sum instruction on both operands, removes both operands from the operand stack and puts the result back on the stack.

All these instructions will pass through this interpreter to determine the result values to put on the operand stack.

This interpreter works as follows to find the argument matchers: if a method call instruction is found that is an argument matcher we return an ArugmentMatcherValue. For other instructions an ArugmentMatcherValue is returned if one of its operands was a an ArugmentMatcherValue. When the actual invoked method is found, we then just have to look at the operands: if one of the operands is an ArugmentMatcherValue, we've found the index of the argument matcher.

For example:
mock.methodCall(0, gt(2)) would give
1) load 0
.... stack ( NotAnArgumentMatcherValue )
2) load 2
.... stack ( NotAnArgumentMatcherValue, NotAnArgumentMatcherValue)
3) invoke argment matcher (pops last operand)
.... stack ( NotAnArgumentMatcherValue, ArgumentMatcherValue)
4) invoke mock method using last 2 operands => we've found an argument matcher as second operand


Field Summary
protected  int currentIndex
           
protected  int currentLineNr
           
protected  Method currentMatcherMethod
           
protected  int fromLineNr
           
protected  Set<org.objectweb.asm.tree.MethodInsnNode> handledMethodInsnNodes
           
protected  int index
           
protected  Class<?> interpretedClass
           
protected  String interpretedMethodName
           
protected  String invokedMethodDescriptor
           
protected  String invokedMethodName
           
protected  List<Integer> resultArgumentMatcherIndexes
           
protected  int toLineNr
           
 
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DOUBLE, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F_APPEND, F_CHOP, F_FULL, F_NEW, F_SAME, F_SAME1, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAT, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INTEGER, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LONG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, NULL, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, TOP, UNINITIALIZED_THIS, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6
 
Constructor Summary
ArgumentMatcherPositionFinder.MethodInterpreter(Class<?> interpretedClass, String interpretedMethodName, String invokedMethodName, String invokedMethodDescriptor, int fromLineNr, int toLineNr, int index)
          Creates an interpreter.
 
Method Summary
 org.objectweb.asm.tree.analysis.Value binaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn, org.objectweb.asm.tree.analysis.Value value1, org.objectweb.asm.tree.analysis.Value value2)
           
 org.objectweb.asm.tree.analysis.Value copyOperation(org.objectweb.asm.tree.AbstractInsnNode insn, org.objectweb.asm.tree.analysis.Value value)
           
protected  ArgumentMatcherPositionFinder.ArgumentMatcherValue createArgumentMatcherValue(org.objectweb.asm.tree.analysis.Value resultValue)
           
protected  List<Integer> getArgumentMatcherIndexes(org.objectweb.asm.tree.MethodInsnNode methodInsnNode, List values)
           
protected  Method getMethod(org.objectweb.asm.tree.MethodInsnNode methodNode)
          Finds a method using the ASM method node
protected  int getNrOfArgumentMacherValues(List<org.objectweb.asm.tree.analysis.Value> values)
           
 List<Integer> getResultArgumentMatcherIndexes()
          Gets the result after the analysis was performed.
protected  org.objectweb.asm.tree.analysis.Value getValue(org.objectweb.asm.tree.analysis.Value resultValue, List<org.objectweb.asm.tree.analysis.Value> values)
           
protected  org.objectweb.asm.tree.analysis.Value getValue(org.objectweb.asm.tree.analysis.Value resultValue, org.objectweb.asm.tree.analysis.Value... values)
           
protected  boolean instructionAlreadyHandled(org.objectweb.asm.tree.MethodInsnNode methodInsnNode)
           
protected  boolean instructionOutOfRange()
           
protected  boolean isArgumentMatcherMethod(Method method)
           
protected  boolean isInvokedMethod(org.objectweb.asm.tree.MethodInsnNode methodInsnNode)
           
protected  boolean isMatcherMethod(Method method)
           
 org.objectweb.asm.tree.analysis.Value merge(org.objectweb.asm.tree.analysis.Value value1, org.objectweb.asm.tree.analysis.Value value2)
          Merges two values.
 org.objectweb.asm.tree.analysis.Value naryOperation(org.objectweb.asm.tree.AbstractInsnNode instructionNode, List values)
          Handles an instruction of a method call.
 void setCurrentLineNr(int currentLineNr)
          Sets the line nr that is being analyzed.
 org.objectweb.asm.tree.analysis.Value ternaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn, org.objectweb.asm.tree.analysis.Value value1, org.objectweb.asm.tree.analysis.Value value2, org.objectweb.asm.tree.analysis.Value value3)
           
protected  void throwUnitilsException(String errorMessage)
          Throws a UnitilsException with the given error message.
 org.objectweb.asm.tree.analysis.Value unaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn, org.objectweb.asm.tree.analysis.Value value)
           
 
Methods inherited from class org.objectweb.asm.tree.analysis.BasicInterpreter
newOperation, newValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

interpretedClass

protected Class<?> interpretedClass

interpretedMethodName

protected String interpretedMethodName

invokedMethodName

protected String invokedMethodName

invokedMethodDescriptor

protected String invokedMethodDescriptor

fromLineNr

protected int fromLineNr

toLineNr

protected int toLineNr

index

protected int index

currentLineNr

protected int currentLineNr

currentIndex

protected int currentIndex

currentMatcherMethod

protected Method currentMatcherMethod

handledMethodInsnNodes

protected Set<org.objectweb.asm.tree.MethodInsnNode> handledMethodInsnNodes

resultArgumentMatcherIndexes

protected List<Integer> resultArgumentMatcherIndexes
Constructor Detail

ArgumentMatcherPositionFinder.MethodInterpreter

public ArgumentMatcherPositionFinder.MethodInterpreter(Class<?> interpretedClass,
                                                       String interpretedMethodName,
                                                       String invokedMethodName,
                                                       String invokedMethodDescriptor,
                                                       int fromLineNr,
                                                       int toLineNr,
                                                       int index)
Creates an interpreter.

Parameters:
interpretedClass - The current class, not null
interpretedMethodName - The current method name, not null
invokedMethodName - The method to look for, not null
invokedMethodDescriptor - The signature of the method to look for, not null
fromLineNr - The begin line-nr of the invocation
toLineNr - The end line-nr of the invocation (could be different from the begin line-nr if the invocation is written on more than 1 line)
index - The index of the matcher on that line, 1 for the first, 2 for the second etc
Method Detail

getResultArgumentMatcherIndexes

public List<Integer> getResultArgumentMatcherIndexes()
Gets the result after the analysis was performed.

Returns:
The argument indexes, null if method was not found, empty if method found but there are no matchers

setCurrentLineNr

public void setCurrentLineNr(int currentLineNr)
Sets the line nr that is being analyzed.

Parameters:
currentLineNr - The line nr

copyOperation

public org.objectweb.asm.tree.analysis.Value copyOperation(org.objectweb.asm.tree.AbstractInsnNode insn,
                                                           org.objectweb.asm.tree.analysis.Value value)
                                                    throws org.objectweb.asm.tree.analysis.AnalyzerException
Specified by:
copyOperation in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
copyOperation in class org.objectweb.asm.tree.analysis.BasicInterpreter
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException

unaryOperation

public org.objectweb.asm.tree.analysis.Value unaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn,
                                                            org.objectweb.asm.tree.analysis.Value value)
                                                     throws org.objectweb.asm.tree.analysis.AnalyzerException
Specified by:
unaryOperation in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
unaryOperation in class org.objectweb.asm.tree.analysis.BasicInterpreter
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException

binaryOperation

public org.objectweb.asm.tree.analysis.Value binaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn,
                                                             org.objectweb.asm.tree.analysis.Value value1,
                                                             org.objectweb.asm.tree.analysis.Value value2)
                                                      throws org.objectweb.asm.tree.analysis.AnalyzerException
Specified by:
binaryOperation in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
binaryOperation in class org.objectweb.asm.tree.analysis.BasicInterpreter
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException

ternaryOperation

public org.objectweb.asm.tree.analysis.Value ternaryOperation(org.objectweb.asm.tree.AbstractInsnNode insn,
                                                              org.objectweb.asm.tree.analysis.Value value1,
                                                              org.objectweb.asm.tree.analysis.Value value2,
                                                              org.objectweb.asm.tree.analysis.Value value3)
                                                       throws org.objectweb.asm.tree.analysis.AnalyzerException
Specified by:
ternaryOperation in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
ternaryOperation in class org.objectweb.asm.tree.analysis.BasicInterpreter
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException

naryOperation

public org.objectweb.asm.tree.analysis.Value naryOperation(org.objectweb.asm.tree.AbstractInsnNode instructionNode,
                                                           List values)
                                                    throws org.objectweb.asm.tree.analysis.AnalyzerException
Handles an instruction of a method call.

Specified by:
naryOperation in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
naryOperation in class org.objectweb.asm.tree.analysis.BasicInterpreter
Parameters:
instructionNode - The instruction
values - The operands
Returns:
The merged values or an ArugmentMatcherValue if an argument matcher method was found
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException

instructionOutOfRange

protected boolean instructionOutOfRange()

instructionAlreadyHandled

protected boolean instructionAlreadyHandled(org.objectweb.asm.tree.MethodInsnNode methodInsnNode)

isInvokedMethod

protected boolean isInvokedMethod(org.objectweb.asm.tree.MethodInsnNode methodInsnNode)

getArgumentMatcherIndexes

protected List<Integer> getArgumentMatcherIndexes(org.objectweb.asm.tree.MethodInsnNode methodInsnNode,
                                                  List values)

isMatcherMethod

protected boolean isMatcherMethod(Method method)

isArgumentMatcherMethod

protected boolean isArgumentMatcherMethod(Method method)

throwUnitilsException

protected void throwUnitilsException(String errorMessage)
Throws a UnitilsException with the given error message. The stacktrace is modified, to make it point to the line of code that was analyzed by this class.

Parameters:
errorMessage - The error message

merge

public org.objectweb.asm.tree.analysis.Value merge(org.objectweb.asm.tree.analysis.Value value1,
                                                   org.objectweb.asm.tree.analysis.Value value2)
Merges two values.

Specified by:
merge in interface org.objectweb.asm.tree.analysis.Interpreter
Overrides:
merge in class org.objectweb.asm.tree.analysis.BasicInterpreter
Parameters:
value1 - The first value
value2 - The second value
Returns:
The merged value

getMethod

protected Method getMethod(org.objectweb.asm.tree.MethodInsnNode methodNode)
Finds a method using the ASM method node

Parameters:
methodNode - The ASM method node, not null
Returns:
The method, null if not found

getValue

protected org.objectweb.asm.tree.analysis.Value getValue(org.objectweb.asm.tree.analysis.Value resultValue,
                                                         org.objectweb.asm.tree.analysis.Value... values)
Parameters:
resultValue - The result value
values - The values that can be ArgumentMatcherValues
Returns:
The result value, or a ArgumentMatcherValue of the same type if one of the values is an ArgumentMatcherValue

getValue

protected org.objectweb.asm.tree.analysis.Value getValue(org.objectweb.asm.tree.analysis.Value resultValue,
                                                         List<org.objectweb.asm.tree.analysis.Value> values)
Parameters:
resultValue - The result value
values - The values that can be ArgumentMatcherValues
Returns:
The result value, or a ArgumentMatcherValue of the same type if one of the values is an ArgumentMatcherValue

getNrOfArgumentMacherValues

protected int getNrOfArgumentMacherValues(List<org.objectweb.asm.tree.analysis.Value> values)
Parameters:
values - The values that can be ArgumentMatcherValues
Returns:
The nr of values that are an ArgumentMatcherValue

createArgumentMatcherValue

protected ArgumentMatcherPositionFinder.ArgumentMatcherValue createArgumentMatcherValue(org.objectweb.asm.tree.analysis.Value resultValue)
Parameters:
resultValue - The result value
Returns:
An ArgumentMatcherValue of the same type


Copyright © 2011. All Rights Reserved.