Package org.apache.bcel.generic
Class MethodGen
- java.lang.Object
-
- org.apache.bcel.classfile.AccessFlags
-
- org.apache.bcel.generic.FieldGenOrMethodGen
-
- org.apache.bcel.generic.MethodGen
-
- All Implemented Interfaces:
java.lang.Cloneable
,NamedAndTyped
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.- See Also:
InstructionList
,Method
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
MethodGen.BranchStack
(package private) static class
MethodGen.BranchTarget
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
argNames
private Type[]
argTypes
private static BCELComparator
bcelComparator
private java.lang.String
className
private java.util.List<Attribute>
codeAttrsList
private java.util.List<CodeExceptionGen>
exceptionList
private boolean
hasParameterAnnotations
private boolean
haveUnpackedParameterAnnotations
private InstructionList
il
private java.util.List<LineNumberGen>
lineNumberList
private LocalVariableTypeTable
localVariableTypeTable
private int
maxLocals
private int
maxStack
private java.util.List<MethodObserver>
observers
private java.util.List<AnnotationEntryGen>[]
paramAnnotations
private boolean
stripAttributes
private java.util.List<java.lang.String>
throwsList
private java.util.List<LocalVariableGen>
variableList
-
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
Constructors Constructor Description MethodGen(int accessFlags, Type returnType, Type[] argTypes, java.lang.String[] argNames, java.lang.String methodName, java.lang.String className, InstructionList il, ConstantPoolGen cp)
Declare method.MethodGen(Method method, java.lang.String className, ConstantPoolGen cp)
Instantiate from existing method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAnnotationsAsAttribute(ConstantPoolGen cp)
void
addCodeAttribute(Attribute a)
Add an attribute to the code.void
addException(java.lang.String className)
Add an exception possibly thrown by this method.CodeExceptionGen
addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType)
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 srcLine)
Give an instruction a line number corresponding to the source code line.LocalVariableGen
addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method.LocalVariableGen
addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex)
Adds a local variable to this method.LocalVariableGen
addLocalVariable(java.lang.String name, Type type, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method and assigns an index automatically.void
addObserver(MethodObserver o)
Add observer for this object.void
addParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation)
void
addParameterAnnotationsAsAttribute(ConstantPoolGen cp)
private Attribute[]
addRuntimeAnnotationsAsAttribute(ConstantPoolGen cp)
private Attribute[]
addRuntimeParameterAnnotationsAsAttribute(ConstantPoolGen cp)
private void
adjustLocalVariableTypeTable(LocalVariableTable lvt)
MethodGen
copy(java.lang.String className, 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.boolean
equals(java.lang.Object obj)
Return value as defined by given BCELComparator strategy.java.util.List<AnnotationEntryGen>
getAnnotationsOnParameter(int i)
Return a list of AnnotationGen objects representing parameter annotationsjava.lang.String
getArgumentName(int i)
java.lang.String[]
getArgumentNames()
Type
getArgumentType(int i)
Type[]
getArgumentTypes()
private static byte[]
getByteCodes(Method method)
java.lang.String
getClassName()
Attribute[]
getCodeAttributes()
private CodeException[]
getCodeExceptions()
static BCELComparator
getComparator()
CodeExceptionGen[]
getExceptionHandlers()
java.lang.String[]
getExceptions()
private ExceptionTable
getExceptionTable(ConstantPoolGen cp)
InstructionList
getInstructionList()
LineNumberGen[]
getLineNumbers()
LineNumberTable
getLineNumberTable(ConstantPoolGen cp)
LocalVariableGen[]
getLocalVariables()
LocalVariableTable
getLocalVariableTable(ConstantPoolGen cp)
LocalVariableTypeTable
getLocalVariableTypeTable()
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.Method
getMethod()
Get method object.Type
getReturnType()
java.lang.String
getSignature()
int
hashCode()
Return value as defined by given BCELComparator strategy.private java.util.List<AnnotationEntryGen>
makeMutableVersion(AnnotationEntry[] mutableArray)
void
removeCodeAttribute(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
removeLocalVariableTypeTable()
Remove the LocalVariableTypeTablevoid
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.void
removeObserver(MethodObserver o)
Remove observer for this object.void
removeRuntimeAttributes(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.void
setArgumentName(int i, java.lang.String name)
void
setArgumentNames(java.lang.String[] argNames)
void
setArgumentType(int i, Type type)
void
setArgumentTypes(Type[] argTypes)
void
setClassName(java.lang.String className)
static void
setComparator(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 returnType)
void
stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -Ojava.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.private void
updateLocalVariableTable(LocalVariableTable a)
-
Methods inherited from class org.apache.bcel.generic.FieldGenOrMethodGen
addAll, addAnnotationEntry, addAttribute, clone, getAnnotationEntries, getAttributes, getConstantPool, getName, getType, removeAnnotationEntries, removeAnnotationEntry, 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, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiers
-
-
-
-
Field Detail
-
bcelComparator
private static BCELComparator bcelComparator
-
className
private java.lang.String className
-
argTypes
private Type[] argTypes
-
argNames
private java.lang.String[] argNames
-
maxLocals
private int maxLocals
-
maxStack
private int maxStack
-
il
private InstructionList il
-
stripAttributes
private boolean stripAttributes
-
localVariableTypeTable
private LocalVariableTypeTable localVariableTypeTable
-
variableList
private final java.util.List<LocalVariableGen> variableList
-
lineNumberList
private final java.util.List<LineNumberGen> lineNumberList
-
exceptionList
private final java.util.List<CodeExceptionGen> exceptionList
-
throwsList
private final java.util.List<java.lang.String> throwsList
-
codeAttrsList
private final java.util.List<Attribute> codeAttrsList
-
paramAnnotations
private java.util.List<AnnotationEntryGen>[] paramAnnotations
-
hasParameterAnnotations
private boolean hasParameterAnnotations
-
haveUnpackedParameterAnnotations
private boolean haveUnpackedParameterAnnotations
-
observers
private java.util.List<MethodObserver> observers
-
-
Constructor Detail
-
MethodGen
public MethodGen(int accessFlags, Type returnType, Type[] argTypes, java.lang.String[] argNames, java.lang.String methodName, java.lang.String className, 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 they 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:
accessFlags
- access qualifiersreturnType
- method typeargTypes
- argument typesargNames
- argument names (if this is null, default names will be provided for them)methodName
- name of methodclassName
- 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
public MethodGen(Method method, java.lang.String className, ConstantPoolGen cp)
Instantiate from existing method.- Parameters:
method
- methodclassName
- class name containing this methodcp
- constant pool
-
-
Method Detail
-
getByteCodes
private static byte[] getByteCodes(Method method)
-
getComparator
public static BCELComparator getComparator()
- Returns:
- Comparison strategy object
-
getMaxStack
public static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et)
Computes stack usage of an instruction list by performing control flow analysis.- Returns:
- maximum stack depth used by method
-
setComparator
public static void setComparator(BCELComparator comparator)
- Parameters:
comparator
- Comparison strategy object
-
addAnnotationsAsAttribute
public void addAnnotationsAsAttribute(ConstantPoolGen cp)
- Since:
- 6.0
-
addCodeAttribute
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 to be added
-
addException
public void addException(java.lang.String className)
Add an exception possibly thrown by this method.- Parameters:
className
- (fully qualified) name of exception
-
addExceptionHandler
public CodeExceptionGen addExceptionHandler(InstructionHandle startPc, InstructionHandle endPc, InstructionHandle handlerPc, ObjectType catchType)
Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is done.- Parameters:
startPc
- Start of region (inclusive)endPc
- End of region (inclusive)handlerPc
- Where handling is donecatchType
- class type of handled exception or null if any exception is handled- Returns:
- new exception handler object
-
addLineNumber
public LineNumberGen addLineNumber(InstructionHandle ih, int srcLine)
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
-
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.- 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
-
addLocalVariable
public LocalVariableGen addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end)
Adds a local variable to this method.- Parameters:
name
- variable nametype
- variable typeslot
- the index of the local variable, if type is long or double, the next available index is slot+2start
- from where the variable is validend
- until where the variable is valid- Returns:
- new local variable object
- See Also:
LocalVariable
-
addLocalVariable
public LocalVariableGen addLocalVariable(java.lang.String name, Type type, int slot, InstructionHandle start, InstructionHandle end, int origIndex)
Adds a local variable to this method.- Parameters:
name
- variable nametype
- variable typeslot
- the index of the local variable, if type is long or double, the next available index is slot+2start
- from where the variable is validend
- until where the variable is validorigIndex
- the index of the local variable prior to any modifications- Returns:
- new local variable object
- See Also:
LocalVariable
-
addObserver
public void addObserver(MethodObserver o)
Add observer for this object.
-
addParameterAnnotation
public void addParameterAnnotation(int parameterIndex, AnnotationEntryGen annotation)
-
addParameterAnnotationsAsAttribute
public void addParameterAnnotationsAsAttribute(ConstantPoolGen cp)
- Since:
- 6.0
-
addRuntimeAnnotationsAsAttribute
private Attribute[] addRuntimeAnnotationsAsAttribute(ConstantPoolGen cp)
-
addRuntimeParameterAnnotationsAsAttribute
private Attribute[] addRuntimeParameterAnnotationsAsAttribute(ConstantPoolGen cp)
-
adjustLocalVariableTypeTable
private void adjustLocalVariableTypeTable(LocalVariableTable lvt)
-
copy
public MethodGen copy(java.lang.String className, ConstantPoolGen cp)
- Returns:
- deep copy of this method
-
ensureExistingParameterAnnotationsUnpacked
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).
-
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.- Overrides:
equals
in classjava.lang.Object
- See Also:
Object.equals(Object)
-
getAnnotationsOnParameter
public java.util.List<AnnotationEntryGen> getAnnotationsOnParameter(int i)
Return a list of AnnotationGen objects representing parameter annotations- Since:
- 6.0
-
getArgumentName
public java.lang.String getArgumentName(int i)
-
getArgumentNames
public java.lang.String[] getArgumentNames()
-
getArgumentType
public Type getArgumentType(int i)
-
getArgumentTypes
public Type[] getArgumentTypes()
-
getClassName
public java.lang.String getClassName()
- Returns:
- class that contains this method
-
getCodeAttributes
public Attribute[] getCodeAttributes()
- Returns:
- all attributes of this method.
-
getCodeExceptions
private CodeException[] getCodeExceptions()
- Returns:
- code exceptions for 'Code' attribute
-
getExceptionHandlers
public CodeExceptionGen[] getExceptionHandlers()
-
getExceptions
public java.lang.String[] getExceptions()
-
getExceptionTable
private ExceptionTable getExceptionTable(ConstantPoolGen cp)
- Returns:
- 'Exceptions' attribute of all the exceptions thrown by this method.
-
getInstructionList
public InstructionList getInstructionList()
-
getLineNumbers
public LineNumberGen[] getLineNumbers()
-
getLineNumberTable
public LineNumberTable getLineNumberTable(ConstantPoolGen cp)
- Returns:
- 'LineNumberTable' attribute of all the local variables of this method.
-
getLocalVariables
public LocalVariableGen[] getLocalVariables()
-
getLocalVariableTable
public LocalVariableTable getLocalVariableTable(ConstantPoolGen cp)
- Returns:
- 'LocalVariableTable' attribute of all the local variables of this method.
-
getLocalVariableTypeTable
public LocalVariableTypeTable getLocalVariableTypeTable()
- Returns:
- 'LocalVariableTypeTable' attribute of this method.
-
getMaxLocals
public int getMaxLocals()
-
getMaxStack
public int getMaxStack()
-
getMethod
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 object
-
getReturnType
public Type getReturnType()
-
getSignature
public java.lang.String getSignature()
- Specified by:
getSignature
in classFieldGenOrMethodGen
- Returns:
- signature of method/field.
-
hashCode
public int hashCode()
Return value as defined by given BCELComparator strategy. By default return the hashcode of the method's name XOR signature.- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Object.hashCode()
-
makeMutableVersion
private java.util.List<AnnotationEntryGen> makeMutableVersion(AnnotationEntry[] mutableArray)
-
removeCodeAttribute
public void removeCodeAttribute(Attribute a)
Remove a code attribute.
-
removeCodeAttributes
public void removeCodeAttributes()
Remove all code attributes.
-
removeException
public void removeException(java.lang.String c)
Remove an exception.
-
removeExceptionHandler
public void removeExceptionHandler(CodeExceptionGen c)
Remove an exception handler.
-
removeExceptionHandlers
public void removeExceptionHandlers()
Remove all line numbers.
-
removeExceptions
public void removeExceptions()
Remove all exceptions.
-
removeLineNumber
public void removeLineNumber(LineNumberGen l)
Remove a line number.
-
removeLineNumbers
public void removeLineNumbers()
Remove all line numbers.
-
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.
-
removeLocalVariableTypeTable
public void removeLocalVariableTypeTable()
Remove the LocalVariableTypeTable
-
removeNOPs
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.
-
removeObserver
public void removeObserver(MethodObserver o)
Remove observer for this object.
-
removeRuntimeAttributes
public void removeRuntimeAttributes(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.- Since:
- 6.5.0
-
setArgumentName
public void setArgumentName(int i, java.lang.String name)
-
setArgumentNames
public void setArgumentNames(java.lang.String[] argNames)
-
setArgumentType
public void setArgumentType(int i, Type type)
-
setArgumentTypes
public void setArgumentTypes(Type[] argTypes)
-
setClassName
public void setClassName(java.lang.String className)
-
setInstructionList
public void setInstructionList(InstructionList il)
-
setMaxLocals
public void setMaxLocals()
Compute maximum number of local variables.
-
setMaxLocals
public void setMaxLocals(int m)
Set maximum number of local variables.
-
setMaxStack
public void setMaxStack()
Computes max. stack size by performing control flow analysis.
-
setMaxStack
public void setMaxStack(int m)
Set maximum stack size for this method.
-
setReturnType
public void setReturnType(Type returnType)
-
stripAttributes
public void stripAttributes(boolean flag)
Do not/Do produce attributes code attributesLineNumberTable and LocalVariableTable, like javac -O
-
toString
public final java.lang.String toString()
Return string representation close to declaration format, 'public static void main(String[]) throws IOException', e.g.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String representation of the method.
-
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 they have finished editing the object.
-
updateLocalVariableTable
private void updateLocalVariableTable(LocalVariableTable a)
-
-