Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.bcel.generic
Class MethodGen  view MethodGen download MethodGen.java

java.lang.Object
  extended byorg.apache.bcel.classfile.AccessFlags
      extended byorg.apache.bcel.generic.FieldGenOrMethodGen
          extended byorg.apache.bcel.generic.MethodGen
All Implemented Interfaces:
java.lang.Cloneable, NamedAndTyped, java.io.Serializable

public class MethodGen
extends FieldGenOrMethodGen

Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local variables and attributes, whereas the `LocalVariableTable' and `LineNumberTable' attributes will be set automatically for the code. Use stripAttributes() if you don't like this. While generating code it may be necessary to insert NOP operations. You can use the `removeNOPs' method to get rid off them. The resulting method object can be obtained via the `getMethod()' method.

Version:
$Id: MethodGen.java 386056 2006-03-15 11:31:56Z tcurdt $

Nested Class Summary
(package private) static class MethodGen.BranchStack
           
(package private) static class MethodGen.BranchTarget
           
 
Field Summary
private static org.apache.bcel.util.BCELComparator _cmp
           
private  java.lang.String[] arg_names
           
private  Type[] arg_types
           
private  java.lang.String class_name
           
private  java.util.List code_attrs_vec
           
private  java.util.List exception_vec
           
private  InstructionList il
           
private  java.util.List line_number_vec
           
private  int max_locals
           
private  int max_stack
           
private  java.util.List observers
           
private  boolean strip_attributes
           
private  java.util.List throws_vec
           
private  java.util.List variable_vec
           
 
Fields inherited from class org.apache.bcel.generic.FieldGenOrMethodGen
cp, name, type
 
Fields inherited from class org.apache.bcel.classfile.AccessFlags
access_flags
 
Constructor Summary
MethodGen(int access_flags, Type return_type, Type[] arg_types, java.lang.String[] arg_names, java.lang.String method_name, java.lang.String class_name, InstructionList il, ConstantPoolGen cp)
          Declare method.
MethodGen(org.apache.bcel.classfile.Method m, java.lang.String class_name, ConstantPoolGen cp)
          Instantiate from existing method.
 
Method Summary
 void addCodeAttribute(org.apache.bcel.classfile.Attribute a)
          Add an attribute to the code.
 void addException(java.lang.String class_name)
          Add an exception possibly thrown by this method.
 CodeExceptionGen addExceptionHandler(InstructionHandle start_pc, InstructionHandle end_pc, InstructionHandle handler_pc, ObjectType catch_type)
          Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.
 LineNumberGen addLineNumber(InstructionHandle ih, int src_line)
          Give an instruction a line number corresponding to the source code line.
 LocalVariableGen addLocalVariable(java.lang.String name, Type type, InstructionHandle start, InstructionHandle end)
          Adds a local variable to this method and assigns an index automatically.
 LocalVariableGen addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
          Adds a local variable to this method.
 void addObserver(MethodObserver o)
          Add observer for this object.
 MethodGen copy(java.lang.String class_name, ConstantPoolGen cp)
           
 boolean equals(java.lang.Object obj)
          Return value as defined by given BCELComparator strategy.
 java.lang.String getArgumentName(int i)
           
 java.lang.String[] getArgumentNames()
           
 Type getArgumentType(int i)
           
 Type[] getArgumentTypes()
           
 java.lang.String getClassName()
           
 org.apache.bcel.classfile.Attribute[] getCodeAttributes()
           
private  org.apache.bcel.classfile.CodeException[] getCodeExceptions()
           
static org.apache.bcel.util.BCELComparator getComparator()
           
 CodeExceptionGen[] getExceptionHandlers()
           
 java.lang.String[] getExceptions()
           
private  org.apache.bcel.classfile.ExceptionTable getExceptionTable(ConstantPoolGen cp)
           
 InstructionList getInstructionList()
           
 LineNumberGen[] getLineNumbers()
           
 org.apache.bcel.classfile.LineNumberTable getLineNumberTable(ConstantPoolGen cp)
           
 LocalVariableGen[] getLocalVariables()
           
 org.apache.bcel.classfile.LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
           
 int getMaxLocals()
           
 int getMaxStack()
           
static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et)
          Computes stack usage of an instruction list by performing control flow analysis.
 org.apache.bcel.classfile.Method getMethod()
          Get method object.
 Type getReturnType()
           
 java.lang.String getSignature()
           
 int hashCode()
          Return value as defined by given BCELComparator strategy.
 void removeCodeAttribute(org.apache.bcel.classfile.Attribute a)
          Remove a code attribute.
 void removeCodeAttributes()
          Remove all code attributes.
 void removeException(java.lang.String c)
          Remove an exception.
 void removeExceptionHandler(CodeExceptionGen c)
          Remove an exception handler.
 void removeExceptionHandlers()
          Remove all line numbers.
 void removeExceptions()
          Remove all exceptions.
 void removeLineNumber(LineNumberGen l)
          Remove a line number.
 void removeLineNumbers()
          Remove all line numbers.
 void removeLocalVariable(LocalVariableGen l)
          Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.
 void removeLocalVariables()
          Remove all local variables.
 void removeNOPs()
          Remove all NOPs from the instruction list (if possible) and update every object refering to them, i.e., branch instructions, local variables and exception handlers.
 void removeObserver(MethodObserver o)
          Remove observer for this object.
 void setArgumentName(int i, java.lang.String name)
           
 void setArgumentNames(java.lang.String[] arg_names)
           
 void setArgumentType(int i, Type type)
           
 void setArgumentTypes(Type[] arg_types)
           
 void setClassName(java.lang.String class_name)
           
static void setComparator(org.apache.bcel.util.BCELComparator comparator)
           
 void setInstructionList(InstructionList il)
           
 void setMaxLocals()
          Compute maximum number of local variables.
 void setMaxLocals(int m)
          Set maximum number of local variables.
 void setMaxStack()
          Computes max.
 void setMaxStack(int m)
          Set maximum stack size for this method.
 void setReturnType(Type return_type)
           
private static void sort(LocalVariableGen[] vars, int l, int r)
          Sort local variables by index
 void stripAttributes(boolean flag)
          Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O
 java.lang.String toString()
          Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.
 void update()
          Call notify() method on all observers.
 
Methods inherited from class org.apache.bcel.generic.FieldGenOrMethodGen
addAttribute, clone, getAttributes, getConstantPool, getName, getType, removeAttribute, removeAttributes, setConstantPool, setName, setType
 
Methods inherited from class org.apache.bcel.classfile.AccessFlags
getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVolatile, isVolatile, setAccessFlags, setModifiers
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

class_name

private java.lang.String class_name

arg_types

private Type[] arg_types

arg_names

private java.lang.String[] arg_names

max_locals

private int max_locals

max_stack

private int max_stack

il

private InstructionList il

strip_attributes

private boolean strip_attributes

variable_vec

private java.util.List variable_vec

line_number_vec

private java.util.List line_number_vec

exception_vec

private java.util.List exception_vec

throws_vec

private java.util.List throws_vec

code_attrs_vec

private java.util.List code_attrs_vec

_cmp

private static org.apache.bcel.util.BCELComparator _cmp

observers

private java.util.List observers
Constructor Detail

MethodGen

public MethodGen(int access_flags,
                 Type return_type,
                 Type[] arg_types,
                 java.lang.String[] arg_names,
                 java.lang.String method_name,
                 java.lang.String class_name,
                 InstructionList il,
                 ConstantPoolGen cp)
Declare method. If the method is non-static the constructor automatically declares a local variable `$this' in slot 0. The actual code is contained in the `il' parameter, which may further manipulated by the user. But he must take care not to remove any instruction (handles) that are still referenced from this object. For example one may not add a local variable and later remove the instructions it refers to without causing havoc. It is safe however if you remove that local variable, too.


MethodGen

public MethodGen(org.apache.bcel.classfile.Method m,
                 java.lang.String class_name,
                 ConstantPoolGen cp)
Instantiate from existing method.

Method Detail

addLocalVariable

public LocalVariableGen addLocalVariable(java.lang.String name,
                                         Type type,
                                         int slot,
                                         InstructionHandle start,
                                         InstructionHandle end)
Adds a local variable to this method.


addLocalVariable

public LocalVariableGen addLocalVariable(java.lang.String name,
                                         Type type,
                                         InstructionHandle start,
                                         InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.


removeLocalVariable

public void removeLocalVariable(LocalVariableGen l)
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.


removeLocalVariables

public void removeLocalVariables()
Remove all local variables.


sort

private static final void sort(LocalVariableGen[] vars,
                               int l,
                               int r)
Sort local variables by index


getLocalVariables

public LocalVariableGen[] getLocalVariables()

getLocalVariableTable

public org.apache.bcel.classfile.LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)

addLineNumber

public LineNumberGen addLineNumber(InstructionHandle ih,
                                   int src_line)
Give an instruction a line number corresponding to the source code line.


removeLineNumber

public void removeLineNumber(LineNumberGen l)
Remove a line number.


removeLineNumbers

public void removeLineNumbers()
Remove all line numbers.


getLineNumbers

public LineNumberGen[] getLineNumbers()

getLineNumberTable

public org.apache.bcel.classfile.LineNumberTable getLineNumberTable(ConstantPoolGen cp)

addExceptionHandler

public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc,
                                            InstructionHandle end_pc,
                                            InstructionHandle handler_pc,
                                            ObjectType catch_type)
Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.


removeExceptionHandler

public void removeExceptionHandler(CodeExceptionGen c)
Remove an exception handler.


removeExceptionHandlers

public void removeExceptionHandlers()
Remove all line numbers.


getExceptionHandlers

public CodeExceptionGen[] getExceptionHandlers()

getCodeExceptions

private org.apache.bcel.classfile.CodeException[] getCodeExceptions()

addException

public void addException(java.lang.String class_name)
Add an exception possibly thrown by this method.


removeException

public void removeException(java.lang.String c)
Remove an exception.


removeExceptions

public void removeExceptions()
Remove all exceptions.


getExceptions

public java.lang.String[] getExceptions()

getExceptionTable

private org.apache.bcel.classfile.ExceptionTable getExceptionTable(ConstantPoolGen cp)

addCodeAttribute

public void addCodeAttribute(org.apache.bcel.classfile.Attribute a)
Add an attribute to the code. Currently, the JVM knows about the LineNumberTable, LocalVariableTable and StackMap attributes, where the former two will be generated automatically and the latter is used for the MIDP only. Other attributes will be ignored by the JVM but do no harm.


removeCodeAttribute

public void removeCodeAttribute(org.apache.bcel.classfile.Attribute a)
Remove a code attribute.


removeCodeAttributes

public void removeCodeAttributes()
Remove all code attributes.


getCodeAttributes

public org.apache.bcel.classfile.Attribute[] getCodeAttributes()

getMethod

public org.apache.bcel.classfile.Method getMethod()
Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before calling this method (the same applies for max locals).


removeNOPs

public void removeNOPs()
Remove all NOPs from the instruction list (if possible) and update every object refering to them, i.e., branch instructions, local variables and exception handlers.


setMaxLocals

public void setMaxLocals(int m)
Set maximum number of local variables.


getMaxLocals

public int getMaxLocals()

setMaxStack

public void setMaxStack(int m)
Set maximum stack size for this method.


getMaxStack

public int getMaxStack()

getClassName

public java.lang.String getClassName()

setClassName

public void setClassName(java.lang.String class_name)

setReturnType

public void setReturnType(Type return_type)

getReturnType

public Type getReturnType()

setArgumentTypes

public void setArgumentTypes(Type[] arg_types)

getArgumentTypes

public Type[] getArgumentTypes()

setArgumentType

public void setArgumentType(int i,
                            Type type)

getArgumentType

public Type getArgumentType(int i)

setArgumentNames

public void setArgumentNames(java.lang.String[] arg_names)

getArgumentNames

public java.lang.String[] getArgumentNames()

setArgumentName

public void setArgumentName(int i,
                            java.lang.String name)

getArgumentName

public java.lang.String getArgumentName(int i)

getInstructionList

public InstructionList getInstructionList()

setInstructionList

public void setInstructionList(InstructionList il)

getSignature

public java.lang.String getSignature()
Specified by:
getSignature in class FieldGenOrMethodGen

setMaxStack

public void setMaxStack()
Computes max. stack size by performing control flow analysis.


setMaxLocals

public void setMaxLocals()
Compute maximum number of local variables.


stripAttributes

public void stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O


getMaxStack

public static int getMaxStack(ConstantPoolGen cp,
                              InstructionList il,
                              CodeExceptionGen[] et)
Computes stack usage of an instruction list by performing control flow analysis.


addObserver

public void addObserver(MethodObserver o)
Add observer for this object.


removeObserver

public void removeObserver(MethodObserver o)
Remove observer for this object.


update

public void update()
Call notify() method on all observers. This method is not called automatically whenever the state has changed, but has to be called by the user after he has finished editing the object.


toString

public final java.lang.String toString()
Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.


copy

public MethodGen copy(java.lang.String class_name,
                      ConstantPoolGen cp)

getComparator

public static org.apache.bcel.util.BCELComparator getComparator()

setComparator

public static void setComparator(org.apache.bcel.util.BCELComparator comparator)

equals

public boolean equals(java.lang.Object obj)
Return value as defined by given BCELComparator strategy. By default two MethodGen objects are said to be equal when their names and signatures are equal.


hashCode

public int hashCode()
Return value as defined by given BCELComparator strategy. By default return the hashcode of the method's name XOR signature.