org.curjent.impl.asm
Class MethodWriter

java.lang.Object
  extended by org.curjent.impl.asm.MethodWriter

public final class MethodWriter
extends Object

A { @link MethodVisitor} that generates methods in bytecode form. 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, Eugene Kuleshov

Field Summary
private  int access
          Access flags of this method.
private  ByteVector code
          The bytecode of this method.
private  ClassWriter cw
          The class writer to which this method must be added.
private  int desc
          The index of the constant pool item that contains the descriptor of this method.
private  int exceptionCount
          Number of exceptions that can be thrown by this method.
private  int[] exceptions
          The exceptions that can be thrown by this method.
private  Handler firstHandler
          The first element in the exception handler list.
private  int handlerCount
          Number of elements in the exception handler list.
private  Handler lastHandler
          The last element in the exception handler list.
private  int maxLocals
          Maximum number of local variables for this method.
private  int maxStack
          Maximum stack size of this method.
private  int name
          The index of the constant pool item that contains the name of this method.
(package private)  MethodWriter next
          Next method writer (see { @link ClassWriter#firstMethod firstMethod}).
 
Constructor Summary
MethodWriter(ClassWriter cw, int access, String name, String desc, String signature, String[] exceptions, boolean computeMaxs, boolean computeFrames)
          Constructs a new { @link MethodWriter}.
 
Method Summary
private static int getArgumentsAndReturnSizes(String desc)
          Computes the size of the arguments and of the return value of a method.
(package private)  int getSize()
          Returns the size of the bytecode of this method.
(package private)  void put(ByteVector out)
          Puts the bytecode of this method in the given byte vector.
 void visitCode()
           
 void visitEnd()
           
 void visitFieldInsn(int opcode, String owner, String name, String desc)
           
 void visitInsn(int opcode)
           
 void visitIntConstInsn(int value)
          Pushes an integer constant.
 void visitIntInsn(int opcode, int operand)
           
 void visitJumpInsn(int opcode, Label label)
           
 void visitLabel(Label label)
           
 void visitLdcInsn(Object cst)
           
 void visitMaxs(int maxStack, int maxLocals)
           
 void visitMethodInsn(int opcode, String owner, String name, String desc)
           
 void visitReturnInsn(Class<?> returnType)
          Calls visitInsn(int) with the JVM opcode for returning a value based on its type.
 void visitTryCatchBlock(Label start, Label end, Label handler, String type)
           
 void visitTypeInsn(int opcode, String type)
           
 void visitVarInsn(int opcode, int var)
           
 void visitVarLoadInsn(Class<?> type, int var)
          Calls visitVarInsn(int, int) with the JVM opcode for loading a value based on its type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

next

MethodWriter next
Next method writer (see { @link ClassWriter#firstMethod firstMethod}).


cw

private final ClassWriter cw
The class writer to which this method must be added.


access

private final int access
Access flags of this method.


name

private final int name
The index of the constant pool item that contains the name of this method.


desc

private final int desc
The index of the constant pool item that contains the descriptor of this method.


exceptionCount

private int exceptionCount
Number of exceptions that can be thrown by this method.


exceptions

private int[] exceptions
The exceptions that can be thrown by this method. More precisely, this array contains the indexes of the constant pool items that contain the internal names of these exception classes.


code

private final ByteVector code
The bytecode of this method.


maxStack

private int maxStack
Maximum stack size of this method.


maxLocals

private int maxLocals
Maximum number of local variables for this method.


handlerCount

private int handlerCount
Number of elements in the exception handler list.


firstHandler

private Handler firstHandler
The first element in the exception handler list.


lastHandler

private Handler lastHandler
The last element in the exception handler list.

Constructor Detail

MethodWriter

MethodWriter(ClassWriter cw,
             int access,
             String name,
             String desc,
             String signature,
             String[] exceptions,
             boolean computeMaxs,
             boolean computeFrames)
Constructs a new { @link MethodWriter}.

Parameters:
cw - the class writer in which the method must be added.
access - the method's access flags (see { @link Opcodes}).
name - the method's name.
desc - the method's descriptor (see { @link Type}).
signature - the method's signature. May be null.
exceptions - the internal names of the method's exceptions. May be null.
computeMaxs - true if the maximum stack size and number of local variables must be automatically computed.
computeFrames - true if the stack map tables must be recomputed from scratch.
Method Detail

visitIntConstInsn

public void visitIntConstInsn(int value)
Pushes an integer constant.


visitVarLoadInsn

public void visitVarLoadInsn(Class<?> type,
                             int var)
Calls visitVarInsn(int, int) with the JVM opcode for loading a value based on its type. For example, visits ILOAD for Boolean.TYPE.


visitReturnInsn

public void visitReturnInsn(Class<?> returnType)
Calls visitInsn(int) with the JVM opcode for returning a value based on its type. For example, visits IRETURN for Boolean.TYPE.


visitCode

public void visitCode()

visitInsn

public void visitInsn(int opcode)

visitIntInsn

public void visitIntInsn(int opcode,
                         int operand)

visitVarInsn

public void visitVarInsn(int opcode,
                         int var)

visitTypeInsn

public void visitTypeInsn(int opcode,
                          String type)

visitFieldInsn

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

visitMethodInsn

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

visitJumpInsn

public void visitJumpInsn(int opcode,
                          Label label)

visitLabel

public void visitLabel(Label label)

visitLdcInsn

public void visitLdcInsn(Object cst)

visitTryCatchBlock

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

visitMaxs

public void visitMaxs(int maxStack,
                      int maxLocals)

visitEnd

public void visitEnd()

getArgumentsAndReturnSizes

private static int getArgumentsAndReturnSizes(String desc)
Computes the size of the arguments and of the return value of a method.

Parameters:
desc - the descriptor of a method.
Returns:
the size of the arguments of the method (plus one for the implicit this argument), argSize, and the size of its return value, retSize, packed into a single int i = (argSize << 2) | retSize (argSize is therefore equal to i >> 2, and retSize to i & 0x03).

getSize

int getSize()
Returns the size of the bytecode of this method.

Returns:
the size of the bytecode of this method.

put

void put(ByteVector out)
Puts the bytecode of this method in the given byte vector.

Parameters:
out - the byte vector into which the bytecode of this method must be copied.


Copyright 2009-2011 Tom Landon
Apache License 2.0