Package org.aspectj.apache.bcel.generic
Class MethodGen
java.lang.Object
org.aspectj.apache.bcel.classfile.Modifiers
org.aspectj.apache.bcel.generic.FieldGenOrMethodGen
org.aspectj.apache.bcel.generic.MethodGen
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,v 1.17 2011/05/19 23:23:46 aclement Exp $
- Author:
- M. Dahm, Patrick C. Beard [setMaxStack()]
- See Also:
InstructionList
,Method
-
Field Summary
Fields inherited from class org.aspectj.apache.bcel.generic.FieldGenOrMethodGen
annotationList, cp, name, type
-
Constructor Summary
ConstructorDescriptionMethodGen(int access_flags, Type return_type, Type[] arg_types, String[] arg_names, String method_name, String class_name, InstructionList il, ConstantPool cp)
Declare method.MethodGen(Method m, String class_name, ConstantPool cp)
Instantiate from existing method.MethodGen(Method m, String class_name, ConstantPool cp, boolean useTags)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add an attribute to the code.void
addException(String class_name)
Add an exception possibly thrown by this method.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.addLineNumber(InstructionHandle ih, int src_line)
Give an instruction a line number corresponding to the source code line.addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.void
addParameterAnnotation(int parameterIndex, AnnotationGen annotation)
void
getAnnotationsOnParameter(int i)
Return a list of AnnotationGen objects representing parameter annotationsgetArgumentName(int i)
String[]
getArgumentType(int i)
Type[]
String[]
int
int
int
static int
getMaxStack(ConstantPool cp, InstructionList il, CodeExceptionGen[] et)
Computes stack usage of an instruction list by performing control flow analysis.Get method object.void
Remove a code attribute.void
Remove all code attributes.void
Remove an exception.void
Remove an exception handler.void
Remove all line numbers.void
Remove all exceptions.void
Remove a line number.void
Remove all line numbers.void
Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument.void
Remove all local variables.void
setArgumentName(int i, String name)
void
setArgumentNames(String[] arg_names)
void
setArgumentType(int i, Type type)
void
setArgumentTypes(Type[] arg_types)
void
setClassName(String class_name)
void
void
Compute maximum number of local variables based on the parameter count and bytecode usage of variables.void
setMaxLocals(boolean respectLocalVariableTable)
Compute maximum number of local variables.void
setMaxLocals(int m)
Set maximum number of local variables.void
Computes max. stack size by performing control flow analysis.void
setMaxStack(int m)
Set maximum stack size for this method.void
setReturnType(Type return_type)
void
stripAttributes(boolean flag)
toString()
Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.Methods inherited from class org.aspectj.apache.bcel.generic.FieldGenOrMethodGen
addAnnotation, addAnnotationsAsAttribute, addAttribute, getAnnotations, getAttributes, getAttributesImmutable, getConstantPool, getName, getType, removeAnnotation, removeAnnotations, removeAttribute, removeAttributes, setConstantPool, setName, setType
Methods inherited from class org.aspectj.apache.bcel.classfile.Modifiers
getModifiers, isAbstract, isBridge, isFinal, isInterface, isNative, isPrivate, isProtected, isPublic, isStatic, isStrictfp, isSynchronized, isTransient, isVarargs, isVolatile, setModifiers
-
Constructor Details
-
MethodGen
public MethodGen(int access_flags, Type return_type, Type[] arg_types, String[] arg_names, String method_name, String class_name, InstructionList il, ConstantPool 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.- Parameters:
access_flags
- access qualifiersreturn_type
- method typearg_types
- argument typesarg_names
- argument names (if this is null, default names will be provided for them)method_name
- name of methodclass_name
- class name containing this method (may be null, if you don't care)il
- instruction list associated with this method, may be null only for abstract or native methodscp
- constant pool
-
MethodGen
Instantiate from existing method.- Parameters:
m
- methodclass_name
- class name containing this methodcp
- constant pool
-
MethodGen
-
-
Method Details
-
getHighestlinenumber
public int getHighestlinenumber() -
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) -
addLocalVariable
public LocalVariableGen addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end)Adds a local variable to this method and assigns an index automatically.- Parameters:
name
- variable nametype
- variable typestart
- from where the variable is valid, if this is null, it is valid from the startend
- until where the variable is valid, if this is null, it is valid to the end- Returns:
- new local variable object
- See Also:
LocalVariable
-
removeLocalVariable
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. -
getLocalVariables
-
getLocalVariableTable
- Returns:
- `LocalVariableTable' attribute of all the local variables of this method.
-
addLineNumber
Give an instruction a line number corresponding to the source code line.- Parameters:
ih
- instruction to tag- Returns:
- new line number object
- See Also:
LineNumber
-
removeLineNumber
Remove a line number. -
removeLineNumbers
public void removeLineNumbers()Remove all line numbers. -
getLineNumbers
-
getLineNumberTable
- Returns:
- 'LineNumberTable' attribute for all the local variables of this method.
-
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.- Parameters:
start_pc
- Start of region (inclusive)end_pc
- End of region (inclusive)handler_pc
- Where handling is donecatch_type
- class type of handled exception or null if any exception is handled- Returns:
- new exception handler object
-
removeExceptionHandler
Remove an exception handler. -
removeExceptionHandlers
public void removeExceptionHandlers()Remove all line numbers. -
getExceptionHandlers
-
addException
Add an exception possibly thrown by this method.- Parameters:
class_name
- (fully qualified) name of exception
-
removeException
Remove an exception. -
removeExceptions
public void removeExceptions()Remove all exceptions. -
getExceptions
-
addCodeAttribute
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.- Parameters:
a
- attribute to be added
-
addParameterAnnotationsAsAttribute
-
removeCodeAttribute
Remove a code attribute. -
removeCodeAttributes
public void removeCodeAttributes()Remove all code attributes. -
getCodeAttributes
- Returns:
- all attributes of this method.
-
getMethod
Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before calling this method (the same applies for max locals).- Returns:
- method object
-
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
- Returns:
- class that contains this method
-
setClassName
-
setReturnType
-
getReturnType
-
setArgumentTypes
-
getArgumentTypes
-
setArgumentType
-
getArgumentType
-
setArgumentNames
-
getArgumentNames
-
setArgumentName
-
getArgumentName
-
getInstructionList
-
setInstructionList
-
getSignature
- Specified by:
getSignature
in classFieldGenOrMethodGen
-
setMaxStack
public void setMaxStack()Computes max. stack size by performing control flow analysis. -
setMaxLocals
public void setMaxLocals()Compute maximum number of local variables based on the parameter count and bytecode usage of variables. -
setMaxLocals
public void setMaxLocals(boolean respectLocalVariableTable)Compute maximum number of local variables.- Parameters:
respectLocalVariableTable
- if true and the local variable table indicates more are in use than the code suggests, respect the higher value from the local variable table data.
-
stripAttributes
public void stripAttributes(boolean flag) -
getMaxStack
Computes stack usage of an instruction list by performing control flow analysis.- Returns:
- maximum stack depth used by method
-
toString
Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g. -
getAnnotationsOnParameter
Return a list of AnnotationGen objects representing parameter annotations -
addParameterAnnotation
-