Package net.bytebuddy.utility.visitor
Class StackAwareMethodVisitor
- java.lang.Object
-
- org.objectweb.asm.MethodVisitor
-
- net.bytebuddy.utility.visitor.StackAwareMethodVisitor
-
public class StackAwareMethodVisitor extends org.objectweb.asm.MethodVisitorA method visitor that is aware of the current size of the operand stack at all times. Additionally, this method takes care of maintaining an index for the next currently unused index of the local variable array.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<StackSize>currentA list of the current elements on the operand stack.private intfreeIndexThe next index of the local variable array that is available.private static int[]SIZE_CHANGEAn array mapping any opcode to its size impact onto the operand stack.private java.util.Map<org.objectweb.asm.Label,java.util.List<StackSize>>sizesA mapping of labels to the operand stack size that is expected at this label.
-
Constructor Summary
Constructors Constructor Description StackAwareMethodVisitor(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription instrumentedMethod)Creates a new stack aware method visitor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidadjustStack(int delta)Adjusts the current state of the operand stack.private voidadjustStack(int delta, int offset)Adjusts the current state of the operand stack.private voiddoDrain(java.util.List<StackSize> stackSizes)Drains all supplied elements of the operand stack.voiddrainStack()Pops all values currently on the stack.intdrainStack(int store, int load, StackSize size)Drains the stack to only contain the top value.voidregister(org.objectweb.asm.Label label, java.util.List<StackSize> stackSizes)Explicitly registers a label to define a given stack state.voidvisitFieldInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor)voidvisitInsn(int opcode)voidvisitIntInsn(int opcode, int operand)voidvisitInvokeDynamicInsn(java.lang.String name, java.lang.String descriptor, org.objectweb.asm.Handle bootstrap, java.lang.Object... bootstrapArguments)voidvisitJumpInsn(int opcode, org.objectweb.asm.Label label)voidvisitLabel(org.objectweb.asm.Label label)voidvisitLdcInsn(java.lang.Object value)voidvisitLineNumber(int line, org.objectweb.asm.Label start)voidvisitLookupSwitchInsn(org.objectweb.asm.Label defaultOption, int[] key, org.objectweb.asm.Label[] option)voidvisitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor, boolean isInterface)voidvisitMultiANewArrayInsn(java.lang.String descriptor, int dimension)voidvisitTableSwitchInsn(int minimum, int maximum, org.objectweb.asm.Label defaultOption, org.objectweb.asm.Label... option)voidvisitTryCatchBlock(org.objectweb.asm.Label start, org.objectweb.asm.Label end, org.objectweb.asm.Label handler, java.lang.String type)voidvisitTypeInsn(int opcode, java.lang.String type)voidvisitVarInsn(int opcode, int variable)-
Methods inherited from class org.objectweb.asm.MethodVisitor
visitAnnotableParameterCount, visitAnnotation, visitAnnotationDefault, visitAttribute, visitCode, visitEnd, visitFrame, visitIincInsn, visitInsnAnnotation, visitLocalVariable, visitLocalVariableAnnotation, visitMaxs, visitMethodInsn, visitParameter, visitParameterAnnotation, visitTryCatchAnnotation, visitTypeAnnotation
-
-
-
-
Field Detail
-
SIZE_CHANGE
private static final int[] SIZE_CHANGE
An array mapping any opcode to its size impact onto the operand stack. This mapping is taken fromFramewith the difference that theOpcodes.JSRinstruction is mapped to a size of0as it does not impact the stack after returning from the instruction.
-
current
private java.util.List<StackSize> current
A list of the current elements on the operand stack.
-
sizes
private final java.util.Map<org.objectweb.asm.Label,java.util.List<StackSize>> sizes
A mapping of labels to the operand stack size that is expected at this label. Lists stored in this map must not be mutated.
-
freeIndex
private int freeIndex
The next index of the local variable array that is available.
-
-
Constructor Detail
-
StackAwareMethodVisitor
public StackAwareMethodVisitor(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription instrumentedMethod)Creates a new stack aware method visitor.- Parameters:
methodVisitor- The method visitor to delegate operations to.instrumentedMethod- The method description for which this method visitor is applied.
-
-
Method Detail
-
adjustStack
private void adjustStack(int delta)
Adjusts the current state of the operand stack.- Parameters:
delta- The change of the current operation of the operand stack. Must not be larger than2.
-
adjustStack
private void adjustStack(int delta, int offset)Adjusts the current state of the operand stack.- Parameters:
delta- The change of the current operation of the operand stack. Must not be larger than2.offset- The offset of the value within the operand stack. Must be bigger then0and smaller than the current stack size. Only permitted if the supplieddeltais positive.
-
drainStack
public void drainStack()
Pops all values currently on the stack.
-
drainStack
public int drainStack(int store, int load, StackSize size)Drains the stack to only contain the top value. For this, the value on top of the stack is temporarily stored in the local variable array until all values on the stack are popped off. Subsequently, the top value is pushed back onto the operand stack.- Parameters:
store- The opcode used for storing the top value.load- The opcode used for loading the top value.size- The size of the value on top of the operand stack.- Returns:
- The minimal size of the local variable array that is required to perform the operation.
-
doDrain
private void doDrain(java.util.List<StackSize> stackSizes)
Drains all supplied elements of the operand stack.- Parameters:
stackSizes- The stack sizes of the elements to drain.
-
register
public void register(org.objectweb.asm.Label label, java.util.List<StackSize> stackSizes)Explicitly registers a label to define a given stack state.- Parameters:
label- The label to register a stack state for.stackSizes- The stack sizes to assume when reaching the supplied label.
-
visitInsn
public void visitInsn(int opcode)
- Overrides:
visitInsnin classorg.objectweb.asm.MethodVisitor
-
visitIntInsn
public void visitIntInsn(int opcode, int operand)- Overrides:
visitIntInsnin classorg.objectweb.asm.MethodVisitor
-
visitVarInsn
public void visitVarInsn(int opcode, int variable)- Overrides:
visitVarInsnin classorg.objectweb.asm.MethodVisitor
-
visitTypeInsn
public void visitTypeInsn(int opcode, java.lang.String type)- Overrides:
visitTypeInsnin classorg.objectweb.asm.MethodVisitor
-
visitFieldInsn
public void visitFieldInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor)- Overrides:
visitFieldInsnin classorg.objectweb.asm.MethodVisitor
-
visitMethodInsn
public void visitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor, boolean isInterface)- Overrides:
visitMethodInsnin classorg.objectweb.asm.MethodVisitor
-
visitInvokeDynamicInsn
public void visitInvokeDynamicInsn(java.lang.String name, java.lang.String descriptor, org.objectweb.asm.Handle bootstrap, java.lang.Object... bootstrapArguments)- Overrides:
visitInvokeDynamicInsnin classorg.objectweb.asm.MethodVisitor
-
visitLdcInsn
public void visitLdcInsn(java.lang.Object value)
- Overrides:
visitLdcInsnin classorg.objectweb.asm.MethodVisitor
-
visitMultiANewArrayInsn
public void visitMultiANewArrayInsn(java.lang.String descriptor, int dimension)- Overrides:
visitMultiANewArrayInsnin classorg.objectweb.asm.MethodVisitor
-
visitJumpInsn
public void visitJumpInsn(int opcode, org.objectweb.asm.Label label)- Overrides:
visitJumpInsnin classorg.objectweb.asm.MethodVisitor
-
visitLabel
public void visitLabel(org.objectweb.asm.Label label)
- Overrides:
visitLabelin classorg.objectweb.asm.MethodVisitor
-
visitLineNumber
public void visitLineNumber(int line, org.objectweb.asm.Label start)- Overrides:
visitLineNumberin classorg.objectweb.asm.MethodVisitor
-
visitTableSwitchInsn
public void visitTableSwitchInsn(int minimum, int maximum, org.objectweb.asm.Label defaultOption, org.objectweb.asm.Label... option)- Overrides:
visitTableSwitchInsnin classorg.objectweb.asm.MethodVisitor
-
visitLookupSwitchInsn
public void visitLookupSwitchInsn(org.objectweb.asm.Label defaultOption, int[] key, org.objectweb.asm.Label[] option)- Overrides:
visitLookupSwitchInsnin classorg.objectweb.asm.MethodVisitor
-
visitTryCatchBlock
public void visitTryCatchBlock(org.objectweb.asm.Label start, org.objectweb.asm.Label end, org.objectweb.asm.Label handler, java.lang.String type)- Overrides:
visitTryCatchBlockin classorg.objectweb.asm.MethodVisitor
-
-