org.objectweb.asm

Class CodeWriter

Implemented Interfaces:
CodeVisitor

public class CodeWriter
extends Object
implements CodeVisitor

A 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

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

Method Summary

byte[]
getCode()
Returns the current bytecode of this method.
int
getCodeSize()
Returns the current size of the bytecode of this method.
protected void
init(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.
void
visitAttribute(Attribute attr)
void
visitFieldInsn(int opcode, String owner, String name, String desc)
void
visitIincInsn(int var, int increment)
void
visitInsn(int opcode)
void
visitIntInsn(int opcode, int operand)
void
visitJumpInsn(int opcode, Label label)
void
visitLabel(Label label)
void
visitLdcInsn(Object cst)
void
visitLineNumber(int line, Label start)
void
visitLocalVariable(String name, String desc, Label start, Label end, int index)
void
visitLookupSwitchInsn(Label dflt, keys[] , labels[] )
void
visitMaxs(int maxStack, int maxLocals)
void
visitMethodInsn(int opcode, String owner, String name, String desc)
void
visitMultiANewArrayInsn(String desc, int dims)
void
visitTableSwitchInsn(int min, int max, Label dflt, labels[] )
void
visitTryCatchBlock(Label start, Label end, Label handler, String type)
void
visitTypeInsn(int opcode, String desc)
void
visitVarInsn(int opcode, int var)

Constructor Details

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 Details

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.

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 Constants).
name - the method's name.
desc - the method's descriptor (see 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)
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 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)
Specified by:
visitAttribute in interface CodeVisitor

visitFieldInsn

public void visitFieldInsn(int opcode,
                           String owner,
                           String name,
                           String desc)
Specified by:
visitFieldInsn in interface CodeVisitor

visitIincInsn

public void visitIincInsn(int var,
                          int increment)
Specified by:
visitIincInsn in interface CodeVisitor

visitInsn

public void visitInsn(int opcode)
Specified by:
visitInsn in interface CodeVisitor

visitIntInsn

public void visitIntInsn(int opcode,
                         int operand)
Specified by:
visitIntInsn in interface CodeVisitor

visitJumpInsn

public void visitJumpInsn(int opcode,
                          Label label)
Specified by:
visitJumpInsn in interface CodeVisitor

visitLabel

public void visitLabel(Label label)
Specified by:
visitLabel in interface CodeVisitor

visitLdcInsn

public void visitLdcInsn(Object cst)
Specified by:
visitLdcInsn in interface CodeVisitor

visitLineNumber

public void visitLineNumber(int line,
                            Label start)
Specified by:
visitLineNumber in interface CodeVisitor

visitLocalVariable

public void visitLocalVariable(String name,
                               String desc,
                               Label start,
                               Label end,
                               int index)
Specified by:
visitLocalVariable in interface CodeVisitor

visitLookupSwitchInsn

public void visitLookupSwitchInsn(Label dflt,
                                  keys[] ,
                                  labels[] )
Specified by:
visitLookupSwitchInsn in interface CodeVisitor

visitMaxs

public void visitMaxs(int maxStack,
                      int maxLocals)
Specified by:
visitMaxs in interface CodeVisitor

visitMethodInsn

public void visitMethodInsn(int opcode,
                            String owner,
                            String name,
                            String desc)
Specified by:
visitMethodInsn in interface CodeVisitor

visitMultiANewArrayInsn

public void visitMultiANewArrayInsn(String desc,
                                    int dims)
Specified by:
visitMultiANewArrayInsn in interface CodeVisitor

visitTableSwitchInsn

public void visitTableSwitchInsn(int min,
                                 int max,
                                 Label dflt,
                                 labels[] )
Specified by:
visitTableSwitchInsn in interface CodeVisitor

visitTryCatchBlock

public void visitTryCatchBlock(Label start,
                               Label end,
                               Label handler,
                               String type)
Specified by:
visitTryCatchBlock in interface CodeVisitor

visitTypeInsn

public void visitTypeInsn(int opcode,
                          String desc)
Specified by:
visitTypeInsn in interface CodeVisitor

visitVarInsn

public void visitVarInsn(int opcode,
                         int var)
Specified by:
visitVarInsn in interface CodeVisitor