Top Description Inners Fields Constructors Methods
com.sun.org.apache.bcel.internal.generic

public Class MethodGen

extends FieldGenOrMethodGen
Class Inheritance
Known Direct Subclasses
com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator
Imports
java.util.ArrayList, .Arrays, .Collections, .Comparator, .HashMap, .List, .Objects, .Stack, com.sun.org.apache.bcel.internal.Const, com.sun.org.apache.bcel.internal.classfile.AnnotationEntry, .Annotations, .Attribute, .Code, .CodeException, .ExceptionTable, .LineNumber, .LineNumberTable, .LocalVariable, .LocalVariableTable, .LocalVariableTypeTable, .Method, .ParameterAnnotationEntry, .ParameterAnnotations, .RuntimeVisibleParameterAnnotations, .Utility, com.sun.org.apache.bcel.internal.util.BCELComparator

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.
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 Type[]
private static BCELComparator
private String
private final List<Attribute>
private final List<CodeExceptionGen>
private boolean
private boolean
private InstructionList
private final List<LineNumberGen>
private LocalVariableTypeTable
private int
private int
private List<MethodObserver>
private List<AnnotationEntryGen>[]
private boolean
private final List<String>
private final List<LocalVariableGen>
Inherited from com.sun.org.apache.bcel.internal.generic.FieldGenOrMethodGen:
cpnametype

Constructor Summary

AccessConstructor and Description
public
MethodGen(final int
access qualifiers
accessFlags
,
final Type
method type
returnType
,
final Type[]
argument types
argTypes
,
String[]
argument names (if this is null, default names will be provided for them)
argNames
,
final String
name of method
methodName
,
final String
class name containing this method (may be null, if you don't care)
className
,
final InstructionList
instruction list associated with this method, may be null only for abstract or native methods
il
,
final ConstantPoolGen
constant pool
cp
)

Declare method.

public
MethodGen(final Method
method
method
,
final String
class name containing this method
className
,
final ConstantPoolGen
constant pool
cp
)

Instantiate from existing method.

Method Summary

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

Add an attribute to the code.

public void
addException(final String
(fully qualified) name of exception
className
)

Add an exception possibly thrown by this method.

public CodeExceptionGen

Returns:

new exception handler object
addExceptionHandler
(final InstructionHandle
Start of region (inclusive)
startPc
,
final InstructionHandle
End of region (inclusive)
endPc
,
final InstructionHandle
Where handling is done
handlerPc
,
final ObjectType
class type of handled exception or null if any exception is handled
catchType
)

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
(final InstructionHandle
instruction to tag
ih
,
final int srcLine)

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

public LocalVariableGen

Returns:

new local variable object
addLocalVariable
(final String
variable name
name
,
final Type
variable type
type
,
final InstructionHandle
from where the variable is valid, if this is null, it is valid from the start
start
,
final 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 LocalVariableGen

Returns:

new local variable object
addLocalVariable
(final String
variable name
name
,
final Type
variable type
type
,
final int
the index of the local variable, if type is long or double, the next available index is slot+2
slot
,
final InstructionHandle
from where the variable is valid
start
,
final InstructionHandle
until where the variable is valid
end
)

Adds a local variable to this method.

public LocalVariableGen

Returns:

new local variable object
addLocalVariable
(final String
variable name
name
,
final Type
variable type
type
,
final int
the index of the local variable, if type is long or double, the next available index is slot+2
slot
,
final InstructionHandle
from where the variable is valid
start
,
final InstructionHandle
until where the variable is valid
end
,
final int
the index of the local variable prior to any modifications
origIndex
)

Adds a local variable to this method.

public void
addObserver(final MethodObserver o)

Add observer for this object.

public void
addParameterAnnotation(final int parameterIndex, final AnnotationEntryGen annotation)

public void
private Attribute[]
private Attribute[]
private void
public MethodGen

Returns:

deep copy of this method
copy
(final String className, final ConstantPoolGen cp)

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 boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Return value as defined by given BCELComparator strategy.
public List<AnnotationEntryGen>
getAnnotationsOnParameter(final int i)

Return a list of AnnotationGen objects representing parameter annotations

public String
getArgumentName(final int i)

public String[]
public Type
getArgumentType(final int i)

public Type[]
private static byte[]
getByteCodes(final Method method)

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 static BCELComparator

Returns:

Comparison strategy object
getComparator
()

public CodeExceptionGen[]
public String[]
private ExceptionTable

Returns:

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

public InstructionList
public LineNumberGen[]
public LineNumberTable

Returns:

'LineNumberTable' attribute of all the local variables of this method.
getLineNumberTable
(final ConstantPoolGen cp)

public LocalVariableGen[]
public LocalVariableTable

Returns:

'LocalVariableTable' attribute of all the local variables of this method.
getLocalVariableTable
(final ConstantPoolGen cp)

public LocalVariableTypeTable

Returns:

'LocalVariableTypeTable' attribute of this method.
getLocalVariableTypeTable
()

public int
public static int

Returns:

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

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

public int
public Method

Returns:

method object
getMethod
()

Get method object.

public Type
public String
public int
hashCode()

Overrides java.lang.Object.hashCode.

Return value as defined by given BCELComparator strategy.
private List<AnnotationEntryGen>
makeMutableVersion(final AnnotationEntry[] mutableArray)

public void
removeCodeAttribute(final Attribute a)

Remove a code attribute.

public void
removeCodeAttributes()

Remove all code attributes.

public void
removeException(final String c)

Remove an exception.

public void
removeExceptionHandler(final CodeExceptionGen c)

Remove an exception handler.

public void
removeExceptionHandlers()

Remove all line numbers.

public void
removeExceptions()

Remove all exceptions.

public void
removeLineNumber(final LineNumberGen l)

Remove a line number.

public void
removeLineNumbers()

Remove all line numbers.

public void
removeLocalVariable(final 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
removeLocalVariableTypeTable()

Remove the LocalVariableTypeTable

public void
removeNOPs()

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

public void
removeObserver(final MethodObserver o)

Remove observer for this object.

public void
removeRuntimeAttributes(final Attribute[] attrs)

Would prefer to make this private, but need a way to test if client is using BCEL version 6.5.0 or later that contains fix for BCEL-329.

public void
setArgumentName(final int i, final String name)

public void
setArgumentNames(final String[] argNames)

public void
setArgumentType(final int i, final Type type)

public void
setArgumentTypes(final Type[] argTypes)

public void
setClassName(final String className)

public static void
setComparator(final BCELComparator
Comparison strategy object
comparator
)

public void
public void
setMaxLocals()

Compute maximum number of local variables.

public void
setMaxLocals(final int m)

Set maximum number of local variables.

public void
setMaxStack()

Computes max.

public void
setMaxStack(final int m)

Set maximum stack size for this method.

public void
setReturnType(final Type returnType)

public void
stripAttributes(final boolean flag)

Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O

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.
public void
update()

Call notify() method on all observers.

private void
Inherited from com.sun.org.apache.bcel.internal.generic.FieldGenOrMethodGen:
addAlladdAnnotationEntryaddAttributeclonegetAnnotationEntriesgetAttributesgetConstantPoolgetNamegetTyperemoveAnnotationEntriesremoveAnnotationEntryremoveAttributeremoveAttributessetConstantPoolsetNamesetType