org.objectweb.asm

Class CodeWriter

public class CodeWriter extends Object implements CodeVisitor

A {@link CodeVisitor CodeVisitor} that generates Java bytecode instructions. Each visit method of this class appends the bytecode corresponding to the visited instruction to a byte vector, in the order these methods are called.

Author: Eric Bruneton

Constructor Summary
protected CodeWriter(ClassWriter cw, boolean computeMaxs)
Constructs a CodeWriter.
Method Summary
byte[]getCode()
Returns the current bytecode of this method.
intgetCodeSize()
Returns the current size of the bytecode of this method.
protected voidinit(int access, String name, String desc, String[] exceptions, Attribute attrs)
Initializes this CodeWriter to define the bytecode of the specified method.
protected int[]resizeInstructions(int[] indexes, int[] sizes, int len)
Resizes the designated instructions, while keeping jump offsets and instruction addresses consistent.
voidvisitAttribute(Attribute attr)
voidvisitFieldInsn(int opcode, String owner, String name, String desc)
voidvisitIincInsn(int var, int increment)
voidvisitInsn(int opcode)
voidvisitIntInsn(int opcode, int operand)
voidvisitJumpInsn(int opcode, Label label)
voidvisitLabel(Label label)
voidvisitLdcInsn(Object cst)
voidvisitLineNumber(int line, Label start)
voidvisitLocalVariable(String name, String desc, Label start, Label end, int index)
voidvisitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels)
voidvisitMaxs(int maxStack, int maxLocals)
voidvisitMethodInsn(int opcode, String owner, String name, String desc)
voidvisitMultiANewArrayInsn(String desc, int dims)
voidvisitTableSwitchInsn(int min, int max, Label dflt, Label[] labels)
voidvisitTryCatchBlock(Label start, Label end, Label handler, String type)
voidvisitTypeInsn(int opcode, String desc)
voidvisitVarInsn(int opcode, int var)

Constructor Detail

CodeWriter

protected CodeWriter(ClassWriter cw, boolean computeMaxs)
Constructs a CodeWriter.

Parameters: cw the class writer in which the method must be added. computeMaxs true if the maximum stack size and number of local variables must be automatically computed.

Method Detail

getCode

public byte[] getCode()
Returns the current bytecode of this method. This bytecode only contains the instructions: it does not include the Exceptions, LocalVariableTable, LineNumberTable, Synthetic and Deprecated attributes, if present.

Returns: the current bytecode of this method. The bytecode is contained between the index 0 (inclusive) and the index {@link #getCodeSize getCodeSize} (exclusive).

getCodeSize

public int getCodeSize()
Returns the current size of the bytecode of this method. This size just includes the size of the bytecode instructions: it does not include the size of the Exceptions, LocalVariableTable, LineNumberTable, Synthetic and Deprecated attributes, if present.

Returns: the current size of the bytecode of this method.

init

protected void init(int access, String name, String desc, String[] exceptions, Attribute attrs)
Initializes this CodeWriter to define the bytecode of the specified method.

Parameters: access the method's access flags (see {@link Constants}). name the method's name. desc the method's descriptor (see {@link Type Type}). exceptions the internal names of the method's exceptions. May be null. attrs the non standard attributes of the method.

resizeInstructions

protected int[] resizeInstructions(int[] indexes, int[] sizes, int len)
Resizes the designated instructions, while keeping jump offsets and instruction addresses consistent. This may require to resize other existing instructions, or even to introduce new instructions: for example, increasing the size of an instruction by 2 at the middle of a method can increases the offset of an IFEQ instruction from 32766 to 32768, in which case IFEQ 32766 must be replaced with IFNEQ 8 GOTO_W 32765. This, in turn, may require to increase the size of another jump instruction, and so on... All these operations are handled automatically by this method.

This method must be called after all the method that is being built has been visited. In particular, the {@link Label Label} objects used to construct the method are no longer valid after this method has been called.

Parameters: indexes current positions of the instructions to be resized. Each instruction must be designated by the index of its last byte, plus one (or, in other words, by the index of the first byte of the next instruction). sizes the number of bytes to be added to the above instructions. More precisely, for each i < len, sizes[i] bytes will be added at the end of the instruction designated by indexes[i] or, if sizes[i] is negative, the last |sizes[i]| bytes of the instruction will be removed (the instruction size must not become negative or null). The gaps introduced by this method must be filled in "manually" in the array returned by the {@link #getCode getCode} method. len the number of instruction to be resized. Must be smaller than or equal to indexes.length and sizes.length.

Returns: the indexes array, which now contains the new positions of the resized instructions (designated as above).

visitAttribute

public void visitAttribute(Attribute attr)

visitFieldInsn

public void visitFieldInsn(int opcode, String owner, String name, String desc)

visitIincInsn

public void visitIincInsn(int var, int increment)

visitInsn

public void visitInsn(int opcode)

visitIntInsn

public void visitIntInsn(int opcode, int operand)

visitJumpInsn

public void visitJumpInsn(int opcode, Label label)

visitLabel

public void visitLabel(Label label)

visitLdcInsn

public void visitLdcInsn(Object cst)

visitLineNumber

public void visitLineNumber(int line, Label start)

visitLocalVariable

public void visitLocalVariable(String name, String desc, Label start, Label end, int index)

visitLookupSwitchInsn

public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels)

visitMaxs

public void visitMaxs(int maxStack, int maxLocals)

visitMethodInsn

public void visitMethodInsn(int opcode, String owner, String name, String desc)

visitMultiANewArrayInsn

public void visitMultiANewArrayInsn(String desc, int dims)

visitTableSwitchInsn

public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels)

visitTryCatchBlock

public void visitTryCatchBlock(Label start, Label end, Label handler, String type)

visitTypeInsn

public void visitTypeInsn(int opcode, String desc)

visitVarInsn

public void visitVarInsn(int opcode, int var)