Class StackAwareMethodVisitor
A 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.
Important: It is not always possible to apply this method visitor if it is applied to a class file compiled
for Java 5 or earlier, or if frames are computed by ASM and not passed to this visitor, if a method also contains
Opcodes.GOTO instructions. In the latter case, the stack is assumed empty after the instruction. If this
is a problem, stack adjustment can be disabled by setting UNADJUSTED_PROPERTY to
true. With this setting, Byte Buddy does no longer attempt draining non-empty stacks and skips this visitor
in all cases. This might however lead to verification problems if stacks are left non-empty. As the latter happens
more common and since this visitor is applied defensively, using this wrapper is considered the more sensible default.
-
Field Summary
FieldsModifier and TypeFieldDescriptionA list of the current elements on the operand stack.private intThe next index of the local variable array that is available.private static final int[]An array mapping any opcode to its size impact onto the operand stack.A mapping of labels to the operand stack size that is expected at this label.static final booleantrueif stack adjustment is disabled.static final StringA property to disable stack adjustment.Fields inherited from class org.objectweb.asm.MethodVisitor
api, mv -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStackAwareMethodVisitor(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription instrumentedMethod) Creates a new stack aware method visitor. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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 voidDrains all supplied elements of the operand stack.private static <T> TdoPrivileged(PrivilegedAction<T> action) A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.voidPops all values currently on the stack.intdrainStack(int store, int load, StackSize size) Drains the stack to only contain the top value.static org.objectweb.asm.MethodVisitorof(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription instrumentedMethod) Wraps the provided method visitor within a stack aware method visitor.voidExplicitly registers a label to define a given stack state.voidvisitFieldInsn(int opcode, String owner, String name, String descriptor) voidvisitFrame(int type, int localVariableLength, Object[] localVariable, int stackSize, Object[] stack) voidvisitInsn(int opcode) voidvisitIntInsn(int opcode, int operand) voidvisitInvokeDynamicInsn(String name, String descriptor, org.objectweb.asm.Handle bootstrap, Object... bootstrapArguments) voidvisitJumpInsn(int opcode, org.objectweb.asm.Label label) voidvisitLabel(org.objectweb.asm.Label label) voidvisitLdcInsn(Object value) voidvisitLookupSwitchInsn(org.objectweb.asm.Label defaultOption, int[] key, org.objectweb.asm.Label[] option) voidvisitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) voidvisitMultiANewArrayInsn(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, String type) voidvisitTypeInsn(int opcode, String type) voidvisitVarInsn(int opcode, int variable) Methods inherited from class org.objectweb.asm.MethodVisitor
getDelegate, visitAnnotableParameterCount, visitAnnotation, visitAnnotationDefault, visitAttribute, visitCode, visitEnd, visitIincInsn, visitInsnAnnotation, visitLineNumber, visitLocalVariable, visitLocalVariableAnnotation, visitMaxs, visitMethodInsn, visitParameter, visitParameterAnnotation, visitTryCatchAnnotation, visitTypeAnnotation
-
Field Details
-
UNADJUSTED_PROPERTY
A property to disable stack adjustment. Stack adjustment is typically needed when instrumenting other generated code that leaves excess values on the stack. This is also often the case when byte code obfuscation is used.- See Also:
-
UNADJUSTED
public static final boolean UNADJUSTEDtrueif stack adjustment is disabled. -
SIZE_CHANGE
private static final int[] SIZE_CHANGEAn 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
-
sizes
-
freeIndex
private int freeIndexThe next index of the local variable array that is available.
-
-
Constructor Details
-
StackAwareMethodVisitor
protected 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 Details
-
of
public static org.objectweb.asm.MethodVisitor of(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription instrumentedMethod) Wraps the provided method visitor within a stack aware method visitor.- Parameters:
methodVisitor- The method visitor to delegate operations to.instrumentedMethod- The method description for which this method visitor is applied.- Returns:
- An appropriate
-
doPrivileged
A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.- Type Parameters:
T- The type of the action's resolved value.- Parameters:
action- The action to execute from a privileged context.- Returns:
- The action's resolved value.
-
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
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
-
register
-
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
- Overrides:
visitTypeInsnin classorg.objectweb.asm.MethodVisitor
-
visitFieldInsn
-
visitMethodInsn
-
visitInvokeDynamicInsn
-
visitLdcInsn
- Overrides:
visitLdcInsnin classorg.objectweb.asm.MethodVisitor
-
visitMultiANewArrayInsn
- 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
-
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, @MaybeNull String type) - Overrides:
visitTryCatchBlockin classorg.objectweb.asm.MethodVisitor
-
visitFrame
public void visitFrame(int type, int localVariableLength, @MaybeNull Object[] localVariable, int stackSize, @MaybeNull Object[] stack) - Overrides:
visitFramein classorg.objectweb.asm.MethodVisitor
-