Top Description Inners Fields Constructors Methods
org.aspectj.apache.bcel.generic

public Class MethodGen

extends FieldGenOrMethodGen
Class Inheritance
Imports
java.util.ArrayList, .Hashtable, .List, .Map, .Stack, org.aspectj.apache.bcel.Constants, org.aspectj.apache.bcel.classfile.Attribute, .Code, .CodeException, .ConstantPool, .ExceptionTable, .LineNumber, .LineNumberTable, .LocalVariable, .LocalVariableTable, .Method, .Utility, org.aspectj.apache.bcel.classfile.annotation.AnnotationGen, .RuntimeAnnos, .RuntimeParamAnnos

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.
Authors
M. Dahm, Patrick C. Beard [setMaxStack()]
Version
$Id: MethodGen.java,v 1.17 2011/05/19 23:23:46 aclement Exp $
See Also
InstructionList, Method

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private String
private List<Attribute>
private ArrayList<CodeExceptionGen>
private ArrayList<String>
private boolean
private boolean
private int
private InstructionList
private List<LineNumberGen>
private List<LocalVariableGen>
private int
private int
private List<AnnotationGen>[]
private String[]
private Type[]
private boolean
Inherited from org.aspectj.apache.bcel.generic.FieldGenOrMethodGen:
annotationListcpnametype

Constructor Summary

AccessConstructor and Description
public
MethodGen(int
access qualifiers
access_flags
,
Type
method type
return_type
,
Type[]
argument types
arg_types
,
String[]
argument names (if this is null, default names will be provided for them)
arg_names
,
String
name of method
method_name
,
String
class name containing this method (may be null, if you don't care)
class_name
,
InstructionList
instruction list associated with this method, may be null only for abstract or native methods
il
,
ConstantPool
constant pool
cp
)

Declare method.

public
MethodGen(Method
method
m
,
String
class name containing this method
class_name
,
ConstantPool
constant pool
cp
)

Instantiate from existing method.

public
MethodGen(Method m, String class_name, ConstantPool cp, boolean useTags)

Method Summary

Modifier and TypeMethod and Description
public void
addCodeAttribute(Attribute
attribute to be added
a
)

Add an attribute to the code.

public void
addException(String
(fully qualified) name of exception
class_name
)

Add an exception possibly thrown by this method.

public CodeExceptionGen

Returns:

new exception handler object
addExceptionHandler
(InstructionHandle
Start of region (inclusive)
start_pc
,
InstructionHandle
End of region (inclusive)
end_pc
,
InstructionHandle
Where handling is done
handler_pc
,
ObjectType
class type of handled exception or null if any exception is handled
catch_type
)

Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.

public LineNumberGen

Returns:

new line number object
addLineNumber
(InstructionHandle
instruction to tag
ih
,
int src_line)

Give an instruction a line number corresponding to the source code line.

public LocalVariableGen
public LocalVariableGen

Returns:

new local variable object
addLocalVariable
(String
variable name
name
,
Type
variable type
type
,
InstructionHandle
from where the variable is valid, if this is null, it is valid from the start
start
,
InstructionHandle
until where the variable is valid, if this is null, it is valid to the end
end
)

Adds a local variable to this method and assigns an index automatically.

public void
addParameterAnnotation(int parameterIndex, AnnotationGen annotation)

public void
private void
ensureExistingParameterAnnotationsUnpacked()

Goes through the attributes on the method and identifies any that are RuntimeParameterAnnotations, extracting their contents and storing them as parameter annotations.

public List<AnnotationGen>
getAnnotationsOnParameter(int i)

Return a list of AnnotationGen objects representing parameter annotations

public String
public String[]
public Type
public Type[]
public String

Returns:

class that contains this method
getClassName
()

public Attribute[]

Returns:

all attributes of this method.
getCodeAttributes
()

private CodeException[]

Returns:

code exceptions for `Code' attribute
getCodeExceptions
()

public CodeExceptionGen[]
public String[]
private ExceptionTable

Returns:

`Exceptions' attribute of all the exceptions thrown by this method.
getExceptionTable
(ConstantPool cp)

public int
public InstructionList
public LineNumberGen[]
public LineNumberTable

Returns:

'LineNumberTable' attribute for all the local variables of this method.
getLineNumberTable
(ConstantPool cp)

public LocalVariableGen[]
public LocalVariableTable

Returns:

`LocalVariableTable' attribute of all the local variables of this method.
getLocalVariableTable
(ConstantPool cp)

public int
public int
public static int

Returns:

maximum stack depth used by method
getMaxStack
(ConstantPool cp, InstructionList il, CodeExceptionGen[] et)

Computes stack usage of an instruction list by performing control flow analysis.

public Method

Returns:

method object
getMethod
()

Get method object.

public Type
public String
private List<AnnotationGen>
public void
removeCodeAttribute(Attribute a)

Remove a code attribute.

public void
removeCodeAttributes()

Remove all code attributes.

public void
removeException(String c)

Remove an exception.

public void
removeExceptionHandler(CodeExceptionGen c)

Remove an exception handler.

public void
removeExceptionHandlers()

Remove all line numbers.

public void
removeExceptions()

Remove all exceptions.

public void
removeLineNumber(LineNumberGen l)

Remove a line number.

public void
removeLineNumbers()

Remove all line numbers.

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.

public void
removeLocalVariables()

Remove all local variables.

public void
setArgumentName(int i, String name)

public void
setArgumentNames(String[] arg_names)

public void
setArgumentType(int i, Type type)

public void
setArgumentTypes(Type[] arg_types)

public void
setClassName(String class_name)

public void
public void
setMaxLocals(int m)

Set maximum number of local variables.

public void
setMaxLocals()

Compute maximum number of local variables based on the parameter count and bytecode usage of variables.

public void
setMaxLocals(boolean
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.
respectLocalVariableTable
)

Compute maximum number of local variables.

public void
setMaxStack(int m)

Set maximum stack size for this method.

public void
setMaxStack()

Computes max.

public void
setReturnType(Type return_type)

private static final void
sort(LocalVariableGen[] vars, int l, int r)

Sort local variables by index

public void
stripAttributes(boolean flag)

public final String

Returns:

String representation of the method.
toString
()

Overrides java.lang.Object.toString.

Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g.
Inherited from org.aspectj.apache.bcel.generic.FieldGenOrMethodGen:
addAnnotationaddAnnotationsAsAttributeaddAttributegetAnnotationsgetAttributesgetAttributesImmutablegetConstantPoolgetNamegetTyperemoveAnnotationremoveAnnotationsremoveAttributeremoveAttributessetConstantPoolsetNamesetType

Field Detail

classnameback to summary
private String classname
codeAttributesListback to summary
private List<Attribute> codeAttributesList
exceptionsListback to summary
private ArrayList<CodeExceptionGen> exceptionsList
exceptionsThrownback to summary
private ArrayList<String> exceptionsThrown
hasParameterAnnotationsback to summary
private boolean hasParameterAnnotations
haveUnpackedParameterAnnotationsback to summary
private boolean haveUnpackedParameterAnnotations
highestLineNumberback to summary
private int highestLineNumber
ilback to summary
private InstructionList il
lineNumbersListback to summary
private List<LineNumberGen> lineNumbersList
localVariablesListback to summary
private List<LocalVariableGen> localVariablesList
maxLocalsback to summary
private int maxLocals
maxStackback to summary
private int maxStack
param_annotationsback to summary
private List<AnnotationGen>[] param_annotations
parameterNamesback to summary
private String[] parameterNames
parameterTypesback to summary
private Type[] parameterTypes
stripAttributesback to summary
private boolean stripAttributes

Constructor Detail

MethodGenback to summary
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:int

access qualifiers

return_type:Type

method type

arg_types:Type[]

argument types

arg_names:String[]

argument names (if this is null, default names will be provided for them)

method_name:String

name of method

class_name:String

class name containing this method (may be null, if you don't care)

il:InstructionList

instruction list associated with this method, may be null only for abstract or native methods

cp:ConstantPool

constant pool

MethodGenback to summary
public MethodGen(Method m, String class_name, ConstantPool cp)

Instantiate from existing method.

Parameters
m:Method

method

class_name:String

class name containing this method

cp:ConstantPool

constant pool

MethodGenback to summary
public MethodGen(Method m, String class_name, ConstantPool cp, boolean useTags)

Method Detail

addCodeAttributeback to summary
public void addCodeAttribute(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.

Parameters
a:Attribute

attribute to be added

addExceptionback to summary
public void addException(String class_name)

Add an exception possibly thrown by this method.

Parameters
class_name:String

(fully qualified) name of exception

addExceptionHandlerback to summary
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:InstructionHandle

Start of region (inclusive)

end_pc:InstructionHandle

End of region (inclusive)

handler_pc:InstructionHandle

Where handling is done

catch_type:ObjectType

class type of handled exception or null if any exception is handled

Returns:CodeExceptionGen

new exception handler object

addLineNumberback to summary
public LineNumberGen addLineNumber(InstructionHandle ih, int src_line)

Give an instruction a line number corresponding to the source code line.

Parameters
ih:InstructionHandle

instruction to tag

Returns:LineNumberGen

new line number object

See Also
LineNumber
addLocalVariableback to summary
public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
addLocalVariableback to summary
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:String

variable name

type:Type

variable type

start:InstructionHandle

from where the variable is valid, if this is null, it is valid from the start

end:InstructionHandle

until where the variable is valid, if this is null, it is valid to the end

Returns:LocalVariableGen

new local variable object

See Also
LocalVariable
addParameterAnnotationback to summary
public void addParameterAnnotation(int parameterIndex, AnnotationGen annotation)
addParameterAnnotationsAsAttributeback to summary
public void addParameterAnnotationsAsAttribute(ConstantPool cp)
ensureExistingParameterAnnotationsUnpackedback to summary
private void ensureExistingParameterAnnotationsUnpacked()

Goes through the attributes on the method and identifies any that are RuntimeParameterAnnotations, extracting their contents and storing them as parameter annotations. There are two kinds of parameter annotation - visible and invisible. Once they have been unpacked, these attributes are deleted. (The annotations will be rebuilt as attributes when someone builds a Method object out of this MethodGen object).

getAnnotationsOnParameterback to summary
public List<AnnotationGen> getAnnotationsOnParameter(int i)

Return a list of AnnotationGen objects representing parameter annotations

getArgumentNameback to summary
public String getArgumentName(int i)
getArgumentNamesback to summary
public String[] getArgumentNames()
getArgumentTypeback to summary
public Type getArgumentType(int i)
getArgumentTypesback to summary
public Type[] getArgumentTypes()
getClassNameback to summary
public String getClassName()
Returns:String

class that contains this method

getCodeAttributesback to summary
public Attribute[] getCodeAttributes()
Returns:Attribute[]

all attributes of this method.

getCodeExceptionsback to summary
private CodeException[] getCodeExceptions()
Returns:CodeException[]

code exceptions for `Code' attribute

getExceptionHandlersback to summary
public CodeExceptionGen[] getExceptionHandlers()
getExceptionsback to summary
public String[] getExceptions()
getExceptionTableback to summary
private ExceptionTable getExceptionTable(ConstantPool cp)
Returns:ExceptionTable

`Exceptions' attribute of all the exceptions thrown by this method.

getHighestlinenumberback to summary
public int getHighestlinenumber()
getInstructionListback to summary
public InstructionList getInstructionList()
getLineNumbersback to summary
public LineNumberGen[] getLineNumbers()
getLineNumberTableback to summary
public LineNumberTable getLineNumberTable(ConstantPool cp)
Returns:LineNumberTable

'LineNumberTable' attribute for all the local variables of this method.

getLocalVariablesback to summary
public LocalVariableGen[] getLocalVariables()
getLocalVariableTableback to summary
public LocalVariableTable getLocalVariableTable(ConstantPool cp)
Returns:LocalVariableTable

`LocalVariableTable' attribute of all the local variables of this method.

getMaxLocalsback to summary
public int getMaxLocals()
getMaxStackback to summary
public int getMaxStack()
getMaxStackback to summary
public static int getMaxStack(ConstantPool cp, InstructionList il, CodeExceptionGen[] et)

Computes stack usage of an instruction list by performing control flow analysis.

Returns:int

maximum stack depth used by method

getMethodback to summary
public 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

method object

getReturnTypeback to summary
public Type getReturnType()
getSignatureback to summary
public String getSignature()

Implements abstract org.aspectj.apache.bcel.generic.FieldGenOrMethodGen.getSignature.

Annotations
@Override
makeMutableVersionback to summary
private List<AnnotationGen> makeMutableVersion(AnnotationGen[] mutableArray)
removeCodeAttributeback to summary
public void removeCodeAttribute(Attribute a)

Remove a code attribute.

removeCodeAttributesback to summary
public void removeCodeAttributes()

Remove all code attributes.

removeExceptionback to summary
public void removeException(String c)

Remove an exception.

removeExceptionHandlerback to summary
public void removeExceptionHandler(CodeExceptionGen c)

Remove an exception handler.

removeExceptionHandlersback to summary
public void removeExceptionHandlers()

Remove all line numbers.

removeExceptionsback to summary
public void removeExceptions()

Remove all exceptions.

removeLineNumberback to summary
public void removeLineNumber(LineNumberGen l)

Remove a line number.

removeLineNumbersback to summary
public void removeLineNumbers()

Remove all line numbers.

removeLocalVariableback to summary
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.

removeLocalVariablesback to summary
public void removeLocalVariables()

Remove all local variables.

setArgumentNameback to summary
public void setArgumentName(int i, String name)
setArgumentNamesback to summary
public void setArgumentNames(String[] arg_names)
setArgumentTypeback to summary
public void setArgumentType(int i, Type type)
setArgumentTypesback to summary
public void setArgumentTypes(Type[] arg_types)
setClassNameback to summary
public void setClassName(String class_name)
setInstructionListback to summary
public void setInstructionList(InstructionList il)
setMaxLocalsback to summary
public void setMaxLocals(int m)

Set maximum number of local variables.

setMaxLocalsback to summary
public void setMaxLocals()

Compute maximum number of local variables based on the parameter count and bytecode usage of variables.

setMaxLocalsback to summary
public void setMaxLocals(boolean respectLocalVariableTable)

Compute maximum number of local variables.

Parameters
respectLocalVariableTable:boolean

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.

setMaxStackback to summary
public void setMaxStack(int m)

Set maximum stack size for this method.

setMaxStackback to summary
public void setMaxStack()

Computes max. stack size by performing control flow analysis.

setReturnTypeback to summary
public void setReturnType(Type return_type)
sortback to summary
private static final void sort(LocalVariableGen[] vars, int l, int r)

Sort local variables by index

stripAttributesback to summary
public void stripAttributes(boolean flag)
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

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

Returns:String

String representation of the method.

Annotations
@Override
org.aspectj.apache.bcel.generic back to summary

pack-priv final Class MethodGen.BranchStack

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv Stack<MethodGen.BranchTarget>
pack-priv Map<InstructionHandle, MethodGen.BranchTarget>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public MethodGen.BranchTarget
pop()

public void
push(InstructionHandle target, int stackDepth)

private final MethodGen.BranchTarget
visit(InstructionHandle target, int stackDepth)

private final boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

branchTargetsback to summary
pack-priv Stack<MethodGen.BranchTarget> branchTargets
visitedTargetsback to summary
pack-priv Map<InstructionHandle, MethodGen.BranchTarget> visitedTargets

Constructor Detail

BranchStackback to summary
pack-priv BranchStack()

Method Detail

popback to summary
public MethodGen.BranchTarget pop()
pushback to summary
public void push(InstructionHandle target, int stackDepth)
visitback to summary
private final MethodGen.BranchTarget visit(InstructionHandle target, int stackDepth)
visitedback to summary
private final boolean visited(InstructionHandle target)
org.aspectj.apache.bcel.generic back to summary

pack-priv final Class MethodGen.BranchTarget

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv InstructionHandle

Constructor Summary

AccessConstructor and Description
pack-priv
BranchTarget(InstructionHandle target, int stackDepth)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

stackDepthback to summary
pack-priv int stackDepth
targetback to summary
pack-priv InstructionHandle target

Constructor Detail

BranchTargetback to summary
pack-priv BranchTarget(InstructionHandle target, int stackDepth)